python读取二进制mnist实例详解

yipeiwu_com6年前Python基础

python读取二进制mnist实例详解

training data 数据结构:

<br>[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
 

  将整个文件读入:

filename = 'train-images.idx3-ubyte'
binfile = open(filename , 'rb')
buf = binfile.read()

读取头四个32bit的interger:

index = 0
magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)
index += struct.calcsize('>IIII')

读取一个图片,784=28*28 :

im = struct.unpack_from('>784B' ,buf, index)
index += struct.calcsize('>784B')
 
im = np.array(im)
im = im.reshape(28,28)
 
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im , cmap='gray')
plt.show()

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

使用Python+wxpy 找出微信里把你删除的好友实例

使用Python+wxpy 找出微信里把你删除的好友实例

之前看到好友在发各种"群发"来检验对方是不是把自己删除了,好吧,其实那个没啥用处. 所以决定自己动手做一个 百度了一下,检测是否被删除,总结出大概网上的一些方法 第一种方法: 拉群法 就...

Python装饰器知识点补充

首先回顾一下关于Python装饰器以及装饰器模式 补全 根据Java实现装饰器模式的,我们可以写下面一段代码: import logging def use_logging(f...

Python tkinter实现的图片移动碰撞动画效果【附源码下载】

Python tkinter实现的图片移动碰撞动画效果【附源码下载】

本文实例讲述了Python tkinter实现的图片移动碰撞动画效果。分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码如下: #!/usr/bin/python #...

Python数据分析:手把手教你用Pandas生成可视化图表的教程

Python数据分析:手把手教你用Pandas生成可视化图表的教程

大家都知道,Matplotlib 是众多 Python 可视化包的鼻祖,也是Python最常用的标准可视化库,其功能非常强大,同时也非常复杂,想要搞明白并非易事。但自从Python进入3...

wxpython中Textctrl回车事件无效的解决方法

本文实例讲述了wxpython中Textctrl回车事件无效的解决方法。分享给大家供大家参考,具体如下: 今天使用wxptyhon的Textctrl控件开发客户端时遇到了一个问题, 按照...