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()

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

相关文章

Request的中断和ErrorHandler实例解析

概述 在view函数中,如果需要中断request,可以使用abort(500)或者直接raise exception。当然我们还需要返回一个出错信息给前端,所以需要定制一下ErrorH...

Python读写文件方法总结

本文实例总结了Python读写文件方法。分享给大家供大家参考。具体分析如下: 1.open 使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/final...

Pyinstaller 打包exe教程及问题解决

安装 pip insatll Pyinstaller 参数 pyinstaller -Fw main.py 参数 概述...

python模块导入的方法

模块在python编程中的地位举足轻重,熟练运用模块可以大大减少代码量,以最少的代码实现复杂的功能。 下面介绍一下在python编程中如何导入模块: (1)import 模块名:直接导入...

python中将\\uxxxx转换为Unicode字符串的方法

今天碰到一个很有意思的问题,需要将普通的 Unicode字符串转换为 Unicode编码的字符串,如下: 将 \\u9500\\u552e 转化为 \u9500\u552e 也就是 销售...