Python语言读取mnist
小标 2018-12-10 来源 : 阅读 1542 评论 0

摘要:本文主要向大家介绍了Python语言读取mnist,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言读取mnist,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

在做 TensorFlow和Python实现神经网络的时候,需要利用到一个MNIST数据集,数据集的格式是以.idx1-ubyte后缀,包含60000个训练图像。将这些图像展示出来,需要利用到[struct模块] iii.run。下载MNIST训练数据集手动下载下载链接为: //yann.lecun.com/exdb/mnist/  下载好之后解压就可以了,网站好像被墙了?mark使用tensorflow自带下载可以看到,这个地方是有监督学习   (有label这个东西嘛)from tensorflow.examples.tutorials.mnist import input_data# 下载mnist数据集mnist = input_data.read_data_sets('/tmp/', one_hot=True)# 数字(label)只能是0-9,神经网络使用10个出口节点就可以编码表示0-9;#  1 -> [0,1.0,0,0,0,0,0,0,0]   one_hot表示只有一个出口节点是hot#  2 -> [0,0.1,0,0,0,0,0,0,0]#  5 -> [0,0,0,0,0,1.0,0,0,0]#  /tmp是macOS的临时目录,重启系统数据丢失; Linux的临时目录也是/tmp详细步骤读取文件with open(filename ,'rb') as f1:
    buf1 = f1.read()还有另外一种常用的方法,两个方法目前来看没有什么区别。f1 = open(filename , 'rb')
buf = binfile.read() # 先使用二进制方式把文件都读进来跨过头部区域train-images-idx3-ubyteTRAINING SET IMAGE FILE (train-images-idx3-ubyte):

[offset] [type]          [value]          [description] 
0000     32 bit integer  0x00000803(2051) magic number 
0004     32 bit integer  60000            number of images 
0008     32 bit integer  28               number of rows 
0012     32 bit integer  28               number of columns 
0016     unsigned byte   ??               pixel 
0017     unsigned byte   ??               pixel 
........ 
xxxx     unsigned byte   ??               pixel可以看到头部有4个integer 类型,设置image_index += struct.calcsize('>IIII')计算4个integer 值的位置,然后image_index 直接跳过去。至于为什么用IIII,愿意的话可以点击了解。temp = struct.unpack_from('>784B', buf1, image_index) 
# '>784B'的意思就是用大端法读取784( 28*28 )个unsigned byteim = np.reshape(temp,(28,28))最后那句np.reshape(temp,(28,28))是以下两句的缩写im = np.array(im)
im = im.reshape(28,28)train-labels-idx1-ubyte可以看到头部有2个integer 类型,同理,label_index 直接跳过去。TRAINING SET LABEL FILE (train-labels-idx1-ubyte):[offset] [type]          [value]          [description] 0000     32 bit integer  0x00000801(2049) magic number (MSB first) 
0004     32 bit integer  60000            number of items 0008     unsigned byte   ??               label 0009     unsigned byte   ??               label ........ 
xxxx     unsigned byte   ??               labelThe labels values are 0 to 9.显示图片plt.imshow(im , cmap='gray')应该就可以看到图片了,是一张5,  当然头部文件还是要有的%matplotlib inlineimport numpy as npimport structimport matplotlib.pyplot as plt
path = 'E:\\Machine Learning\\train-images.idx3-ubyte'with open(path,'rb') as f1:
    buf1 = f1.read() 
image_index = 0image_index += struct.calcsize('>IIII')
temp = struct.unpack_from('>784B', buf1, image_index) 
# '>784B'的意思就是用大端法读取784( 28*28 )个unsigned byteim = np.reshape(temp,(28,28))
plt.imshow(im , cmap='gray')give me 5多张图片读取多张图片import numpy as npimport structimport matplotlib.pyplot as pltdef readfile():
    with open('E:\\Machine Learning\\train-images.idx3-ubyte','rb') as f1:
        buf1 = f1.read()    with open('E:\\Machine Learning\\train-labels.idx1-ubyte','rb') as f2:
        buf2 = f2.read()    return buf1, buf2def get_image(buf1):
    image_index = 0
    image_index += struct.calcsize('>IIII')
    im = []    for i in range(9):
        temp = struct.unpack_from('>784B', buf1, image_index) # '>784B'的意思就是用大端法读取784个unsigned byte
        im.append(np.reshape(temp,(28,28)))
        image_index += struct.calcsize('>784B')  # 每次增加784B
    return imdef get_label(buf2): # 得到标签数据
    label_index = 0
    label_index += struct.calcsize('>II')    return struct.unpack_from('>9B', buf2, label_index)if __name__ == "__main__":
    image_data, label_data = readfile()
    im = get_image(image_data)
    label = get_label(label_data)    for i in range(9):
        plt.subplot(3, 3, i + 1)
        title = u"标签对应为:"+ str(label[i])
        plt.title(title, fontproperties='SimHei')
        plt.imshow(im[i], cmap='gray')
    plt.show()遇到的一些坑:中文标题乱码的问题plt.title(title, fontproperties='SimHei') # 后边这个字体**SimHei**加上就好了标题内部不能用+在外部加好之后,赋值给新变量,然后放进title即可

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程