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文件有如下内容: 百度 谷歌 现在想读取文件 ...
随机推荐
- NUI相关术语
分享一下微软资深企业架构师.应用开发专家余涛先生书中所谈到的相关术语,以便查阅,部分术语根据个人理解加入了细化内容: 1.波束形成算法(BeamformingAlgorithm) 基于现行阵列的阵列信 ...
- android中Json数据保存方式
package com.example.savejsonproject; import java.io.File; import java.io.FileNotFoundException; impo ...
- CDH离线安装之安装包下载地址
cloudermanager安装包地址:http://archive.cloudera.com/cm5/cm/5/cloudera-manager-el6-cm5.3.0_x86_64.tar.gz, ...
- Android之标签选项卡
TabWidget可以通过不同的标签进行切换并且显示不同的内容,相当于Button按钮实现不同的功能. TabHost的布局: (1):我们先在Layouts拖一个Vertical(纵向视图)的Lin ...
- PHP学习笔记 - 进阶篇(2)
PHP学习笔记 - 进阶篇(2) 函数 1.自定义函数 PHP内置了超过1000个函数,因此函数使得PHP成为一门非常强大的语言.大多数时候我们使用系统的内置函数就可以满足需求,但是自定义函数通过将一 ...
- jquery学习全面总结
本文仅针对jquery的部分知识点做总结,更为全面的可以去官网看中文文档.可以更为详细的了解jquery及其特性. window.onload 和$(document).ready() 我 windo ...
- (转)Hessian(C#)介绍及使用说明
什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...
- android 数据库的增删改查
主java package com.itheima.crud; import android.app.Activity; import android.content.Context; import ...
- 《RedHatlinux系统修复(通过FTP进行修复)》
比如我们删除了grub文件的initrd然后我们来修复 Linux系统下装的虚拟机boot options 位置,我们选网络修复,提前是我已经做好了ftpbootlaoder的配置. Win系统下以V ...
- TreeView递归取值
string jingyuan = ""; string jinghui = ""; private void DiGui(TreeNode tn) { if ...