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 = 0magic, 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 npimport structimport matplotlib.pyplot as pltfilename = 'train-images.idx3-ubyte'binfile = open(filename , 'rb')buf = binfile.read()index = 0magic, 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文件有如下内容: 百度 谷歌 现在想读取文件 ...
随机推荐
- Remoting的入门教程
注:<网摘自http://www.codesky.net/article/200411/48322.html> 基本原理 当客户端创建远程RemotableClass的一个实例,.NET框 ...
- Service通信详解
1.使用Intent进行异步通讯 在Service任务一旦完成后,就发送广播.开发者只需要实现一个BroadcastReceiver来监听响应既可. Activity.startService启动in ...
- Android之触屏事件
方法一: 新建"MyView"类 package onTouchEvent; import android.content.Context; import android.grap ...
- Sql Server关于text类型的替换
UPDATE Store SET Body=replace(convert(varchar(8000),Body),'http://120.89.46.68:8007','')
- 分布式系统怎样体现了CAP
`references:` 1. http://zh.wikipedia.org/wiki/CAP%E5%AE%9A%E7%90%86 2. http://en.wikipedia.org/wiki/ ...
- Linux 获取文件夹下的所有文件
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4129616.html #include <string> #include &l ...
- Excel数据复制到Winform控件ListView
先给窗体添加一个右键菜单contextMenuStrip 加一个下拉项[粘贴] 粘贴事件: private void tsmiPaste_Click(object sender, EventArgs ...
- PHP中刷新输出缓冲,立即输出数据
<script type="text/javascript"> function show_message(message) { document.getElement ...
- php 字母大小写转换的函数
分享下,在php编程中,将字母大小写进行转换的常用函数. 1.将字符串转换成小写strtolower(): 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字符串 2.将字符转成大 ...
- 几种placeholder替换项目参数的方法比较
引言:(引自:http://openwebx.org/docs/autoconfig.html) 在一个应用中,我们总是会遇到一些参数,例如: 数据库服务器IP地址.端口.用户名: 用来保存上传资料的 ...