python读取二进制mnist

training data 数据结构:

[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()
时间: 2024-08-05 23:05:28

python读取二进制mnist的相关文章

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 integ

python读取文件小结

python读取文件小结 你想通过python从文件中读取文本或数据. 一.最方便的方法是一次性读取文件中的所有内容并放置到一个大字符串中: all_the_text = open('thefile.txt').read( )     # 文本文件中的所有文本 all_the_data = open('abinfile','rb').read( )    # 二进制文件中的所有数据 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用的文件对象占用

Python读取文件

1.在Python中如何操作文件 2.如何读取大文件 文件内建函数:open(file_name,access_mode='r',buffering=-1),file() 文件访问模式:     r:以读方式打开(默认)     w:写     a:追加     b:以二进制方式打开     r+ w+ a+:读写方式打开     rb:以二进制读模式打开     wb:以二进制写模式打开     ab:以二进制追加模式打开 输入,输出 read() 读取给定数目个字节 readline() 读

解决Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte...

用Python在读取某个html文件时会遇到下面问题: 出问题的代码: 1 if __name__ == '__main__': 2 fileHandler = open('../report.html', mode='r') 3 4 report_lines = fileHandler.readlines() 5 for line in report_lines: 6 print(line.rstrip()) 修改方式是在open方法指定参数encoding='UTF-8': if __nam

python读取和生成excel文件

今天来看一下如何使用python处理excel文件,处理excel文件是在工作中经常用到的,python为我们考虑到了这一点,python中本身就自带csv模块. 1.用python读取csv文件: csv是逗号分隔符格式 一般我们用的execl生成的格式是xls和xlsx  直接重命名为csv的话会报错: Error: line contains NULL byte insun解决方案:出错原因是直接是把后缀为xls的execl文件重命名为csv的 正常的要是另存为csv文件 就不会报错了 譬

Python读取txt文件

Python读取txt文件,有两种方式: (1)逐行读取 1 data=open("data.txt") 2 line=data.readline() 3 while line: 4 print line 5 line=data.readline() (2)一次全部读入内存 1 data=open("data.txt") 2 for line in data.readlines(): 3 print line

python读取excel文件(xrld模块)

Python读取excel文件 一.python  xlrd模块 安装 mac 下安装python  xlrd模块 http://www.crifan.com/python_read_excel_xls_file_xlrd/comment-page-1/ python setup.py install 在mac 下出现的错误是 http://stackoverflow.com/questions/18199853/error-could-not-create-library-python-2-7

python读取EXCLE文件数据

python读取EXCEL,利用 Google 搜索 Python Excel,点击第一条结果http://www.python-excel.org/ ,能够跨平台处理 Excel. 按照文档一步步去做,要安装 三个包: xlrd(用于读Excel): xlwt(用于写Excel): xlutils(处理Excel的工具箱) 1 from xlrd import open_workbook 2 import re 3 4 #创建一个用于读取sheet的生成器,依次生成每行数据,row_count

dbf文件使用python读取程序

使用python读取dbf # -*- coding: utf-8 -*- import struct,csv,datetime class DBF_Operator(): @staticmethod def SHHQ_dbf_reader(f): numrec, lenheader = struct.unpack('<xxxxLH22x', f.read(32)) numfields = (lenheader - 33) // 32 fields = [] for fieldno in xra