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 - Windows 环境安装 Nginx
1. 访问 http://nginx.org/en/download.html,下载 Windows 版本的安装包 2. 解压安装包,双击 nginx.exe,启动 nginx 3. 访问 http: ...
- 仿QQ注册验证码的实现。
最近发现一些网站的验证码全部换成了“极验”和“点触”的,发现QQ的注册也是与“点触”的相似.就想尝试实现一个. 先上效果图: 下面贴上主要思路及代码: 第一步:得到常用汉字列表 public stat ...
- ViewPager的基本使用--可左右循环切换也可自动切换
ViewPager也算是Android自带的常用控件之一,但是有可能会无法直接调用,所以只需要将工程目录里/libs/android-support-v4.jar该jarAdd to Build Pa ...
- Factory Method模式
Factory Method(工厂方法)模式属于GoF设计模式中的创建型分组,它用于在不之制定待创建对象的具体类的情况下创建对象.--<asp.net设计模式> 说白了就是一个人想要出国旅 ...
- JAVA中的代理技术(静态代理和动态代理)
最近看书,有两个地方提到了动态代理,一是在Head First中的代理模式,二是Spring AOP中的AOP.所以有必要补充一下动态代理的相关知识. Spring采用JDK动态代理和CGLib动态代 ...
- 深入mongoDB(1)--mongod的线程模型与网络框架
最近工作需要开始研究mongoDB,我准备从其源代码角度,对于mongod和mongos服务的架构.sharding策略. replicaset策略.数据同步容灾.索引等机制做一个本质性的了解.其代码 ...
- Dapper full example
Skip to content Sign up Sign in This repository Explore Features Enterprise Blog Watch Star , Fork S ...
- Demo学习: Dialogs Anonymous Callback
Dialogs\Dialogs Anonymous Callback 窗体回调函数使用. 1. 标准回调函数 ShowMessage(const Msg: string; CallBack: TUni ...
- 数据可视化(三)- Seaborn简易入门
本文内容来源:https://www.dataquest.io/mission/133/creating-compelling-visualizations 本文数据来源:http://www.cdc ...
- 使用WebGL实现一个Viewer来显示STL文件
关键字:WebGL,STL,ThreeJS,Chrome,Viewer,Python3.4, HTML5,Canvas. OS:Windows 10. 本文介绍如何使用ThreeJS来实现一个WebG ...