python读取二进制mnist实例详解


Posted in Python onMay 31, 2017

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 相关文章推荐
Python 字符串操作实现代码(截取/替换/查找/分割)
Jun 08 Python
Python PyQt5标准对话框用法示例
Aug 23 Python
Redis使用watch完成秒杀抢购功能的代码
May 07 Python
python 实现交换两个列表元素的位置示例
Jun 26 Python
Python facenet进行人脸识别测试过程解析
Aug 16 Python
python 如何将数据写入本地txt文本文件的实现方法
Sep 11 Python
python返回数组的索引实例
Nov 28 Python
使用Puppeteer爬取微信文章的实现
Feb 11 Python
Python Tornado实现WEB服务器Socket服务器共存并实现交互的方法
May 26 Python
Python爬虫获取豆瓣电影并写入excel
Jul 31 Python
Django权限控制的使用
Jan 07 Python
Django集成富文本编辑器summernote的实现步骤
May 31 Python
Python算术运算符实例详解
May 31 #Python
Python简单的制作图片验证码实例
May 31 #Python
详解python的webrtc库实现语音端点检测
May 31 #Python
python实现决策树C4.5算法详解(在ID3基础上改进)
May 31 #Python
基于ID3决策树算法的实现(Python版)
May 31 #Python
Python基础知识_浅谈用户交互
May 31 #Python
python数据类型_字符串常用操作(详解)
May 30 #Python
You might like
《OVERLORD》第四季,终于等到你!
2020/03/02 日漫
php中用文本文件做数据库的实现方法
2008/03/27 PHP
PHP中的按位与和按位或操作示例
2014/01/27 PHP
thinkphp验证码的实现(form、ajax实现验证)
2016/07/28 PHP
基于PHP制作验证码
2016/10/12 PHP
php实现图片以base64显示的方法
2016/10/13 PHP
Laravel框架实现多个视图共享相同数据的方法详解
2019/07/09 PHP
JS数组去重与取重的示例代码
2014/01/24 Javascript
jQuery选择器源码解读(五):tokenize的解析过程
2015/03/31 Javascript
Jquery为DIV添加click事件的简单实例
2016/06/02 Javascript
详解vue数据渲染出现闪烁问题
2017/06/29 Javascript
nodejs后台集成ueditor富文本编辑器的实例
2017/07/11 NodeJs
表格展示利器 Bootstrap Table实例代码
2017/09/06 Javascript
看看“疫苗查询”小程序有温度的代码
2018/07/31 Javascript
使用pm2部署node生产环境的方法步骤
2019/03/09 Javascript
微信小程序 动态修改页面数据及参数传递过程详解
2019/09/27 Javascript
利用webpack理解CommonJS和ES Modules的差异区别
2020/06/16 Javascript
Django1.7+python 2.78+pycharm配置mysql数据库教程
2014/11/18 Python
Python类的专用方法实例分析
2015/01/09 Python
python通过exifread模块获得图片exif信息的方法
2015/03/16 Python
python中numpy的矩阵、多维数组的用法
2018/02/05 Python
python删除某个字符
2018/03/19 Python
selenium在执行phantomjs的API并获取执行结果的方法
2018/12/17 Python
解决Pytorch训练过程中loss不下降的问题
2020/01/02 Python
python利用xlsxwriter模块 操作 Excel
2020/10/14 Python
IE8下CSS3选择器nth-child() 不兼容问题的解决方法
2016/11/16 HTML / CSS
师范应届生语文教师求职信
2013/10/29 职场文书
秋天的雨教学反思
2014/04/27 职场文书
人民调解员先进事迹材料
2014/05/08 职场文书
小学生竞选班干部演讲稿(5篇)
2014/09/12 职场文书
2016年清明节寄语
2015/12/04 职场文书
python自动化操作之动态验证码、滑动验证码的降噪和识别
2021/08/30 Python
CSS作用域(样式分割)的使用汇总
2021/11/07 HTML / CSS
升级 Win11 还是坚守 Win10?微软 Win11 新系统缺失功能大盘点
2022/04/05 数码科技
Elasticsearch6.2服务器升配后的bug(避坑指南)
2022/09/23 Servers
python 使用pandas读取csv文件的方法
2022/12/24 Python