python读取mnist
python读取mnist
其实就是python怎么读取binnary file
mnist的结构如下,选取train-images
TRAINING 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个 32 bit integer
试过很多方法,觉得最方便的,至少对我来说还是使用
struct.unpack_from()
filename = 'train-images.idx3-ubyte' binfile = open (filename , 'rb' ) buf = binfile.read() |
先使用二进制方式把文件都读进来
index = 0 magic, numImages , numRows , numColumns = struct.unpack_from( '>IIII' , buf , index) index + = struct.calcsize( '>IIII' ) |
然后使用struc.unpack_from
'>IIII'是说使用大端法读取4个unsinged int32
然后读取一个图片测试是否读取成功
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() |
'>784B'的意思就是用大端法读取784个unsigned byte
完整代码如下
import numpy as np import struct import matplotlib.pyplot as plt filename = 'train-images.idx3-ubyte' binfile = open (filename , 'rb' ) buf = binfile.read() index = 0 magic, numImages , numRows , numColumns = struct.unpack_from( '>IIII' , buf , index) index + = struct.calcsize( '>IIII' ) 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读取mnist的更多相关文章
- Python读取MNIST数据集
MNIST数据集获取 MNIST数据集是入门机器学习/模式识别的最经典数据集之一.最早于1998年Yan Lecun在论文: Gradient-based learning applied to do ...
- python读取,显示,保存mnist图片
python处理二进制 python的struct模块可以将整型(或者其它类型)转化为byte数组.看下面的代码. # coding: utf-8 from struct import * # 包装成 ...
- C++基于文件流和armadillo读取mnist
发现网上大把都是用python读取mnist的,用C++大都是用opencv读取的,但我不怎么用opencv,因此自己摸索了个使用文件流读取mnist的方法,armadillo仅作为储存矩阵的一种方式 ...
- python读取excel一例-------从工资表逐行提取信息
在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...
- python读取xml文件
关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...
- [转] Windows下使用Python读取Excel表格数据
http://www.python-excel.org/这个网站罗列了很多关于在Python下操作Excel文件的信息,这里选择了其介绍的第一个模块xlrd . xlrd 0.9.2版本跨平台同时支持 ...
- Python读取txt文件
Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...
- Python读取Yaml文件
近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...
- python读取中文文件编码问题
python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...
随机推荐
- Nginx - Rewrite Module
Initially, the purpose of this module (as the name suggests) is to perform URL rewriting. This mecha ...
- asp网站发布步骤总结
1.在VS2012中打开索要发布的网站,初始页可重命名为index.html或default.apx. 2.点击 生成>生成“网站”,然后“发布网站”. 3.进行发布设置: (1 配置文件 ( ...
- python学习day4--python基础--元组,字符串
1.元组 #只读列表,元组,当希望生成后不被修改则用元组 r=(1,2,3,4,5) 元组 2.字符串,python字符串操作非常丰富,编程时可先查询python本身是否已设计了相关函数 #移除空白 ...
- Spring boot 1.3.5 RELEASE 官方文档中文翻译--目录
说明: 打算利用闲暇时候翻译一下Spring boot的官方文档,翻译的版本是1.3.5 RELEASE. 做这件事的目的呢有四: 国内中文的Spring boot资料实在不多,希望能给后来人一点小小 ...
- JSONP VS CORS
What is JSONP ? http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about What is CORS? htt ...
- redis学习系列
redis学习系列 基本看完 最近在看redis的代码,简单记录下自己认为重要的点,自己写比较费时间的,我会把查到的资料贴出来方便查看 淘宝的redis内存分析 http://www.searchtb ...
- document.compatMode的CSS1compat
document.compatMode BackCompat:标准兼容模式关闭.浏览器宽度:document.body.clientWidth: CSS1Compat:标准兼容模式开启. 浏览器宽度: ...
- 利用Google GCM发送push通知到Android客户端
// 这个可以需要在google账号中申请,勾选gcm服务选项 $apiKey = 'AIzaSyC6h3ysrn2HDCBqONTo2vKIVVuktIFoxxx'; $headers = arra ...
- RHEL 7.2 安装Oracle XE-11.2.0
轻量快捷版本,适合开发 0. /etc/hosts 添加 本机hostname # hostnamepromote.cache-dns.local # cat /etc/hosts127.0.0.1 ...
- java日志框架与日志系统
日志框架:提供日志调用的接口,实际的日志输出委托给日志系统实现. JCL(Jakarta Commons Logging):比较流行的日志框架,很多框架都依赖JCL,例如Spring等. SLF4j: ...