读取 mnist 数据集 --- python

手写数字识别数据集(MNIST)是机器学习中常用来作为训练测试数据的一个经典数据集。使用时需要根据数据格式,自己写读取函数.(官网似乎有基于matlab的读写函数),下面是分别基于python3  python2 的读取代码,供参考.

基于python3

import numpy as np
import struct
 
def loadImageSet(filename):
	print "load image set",filename
	binfile= open(filename, 'rb')
	buffers = binfile.read()
 
	head = struct.unpack_from('>IIII' , buffers ,0)
	print "head,",head
 
	offset = struct.calcsize('>IIII')
	imgNum = head[1]
	width = head[2]
	height = head[3]
	#[60000]*28*28
	bits = imgNum * width * height
	bitsString = '>' + str(bits) + 'B' #like '>47040000B'
 
	imgs = struct.unpack_from(bitsString,buffers,offset)
 
	binfile.close()
	imgs = np.reshape(imgs,[imgNum,1,width*height])
	print "load imgs finished"
	return imgs
 
def loadLabelSet(filename):
 
	print "load label set",filename
	binfile = open(filename, 'rb')
	buffers = binfile.read()
 
	head = struct.unpack_from('>II' , buffers ,0)
	print "head,",head
	imgNum=head[1]
 
	offset = struct.calcsize('>II')
	numString = '>'+str(imgNum)+"B"
	labels = struct.unpack_from(numString , buffers , offset)
	binfile.close()
	labels = np.reshape(labels,[imgNum,1])
 
	print 'load label finished'
	return labels
 
if __name__=="__main__":
 
	imgs = loadImageSet("train-images.idx3-ubyte")
	labels = loadLabelSet("train-labels.idx1-ubyte")
	
	#imgs = loadImageSet("t10k-images.idx3-ubyte")
	#labels = loadLabelSet("t10k-labels.idx1-ubyte")

基于python2

import numpy as np
import struct
 
def loadImageSet(filename):
	print "load image set",filename
	binfile= open(filename, 'rb')
	buffers = binfile.read()
 
	head = struct.unpack_from('>IIII' , buffers ,0)
	print "head,",head
 
	offset = struct.calcsize('>IIII')
	imgNum = head[1]
	width = head[2]
	height = head[3]
	#[60000]*28*28
	bits = imgNum * width * height
	bitsString = '>' + str(bits) + 'B' #like '>47040000B'
 
	imgs = struct.unpack_from(bitsString,buffers,offset)
 
	binfile.close()
	imgs = np.reshape(imgs,[imgNum,1,width*height])
	print "load imgs finished"
	return imgs
 
def loadLabelSet(filename):
 
	print "load label set",filename
	binfile = open(filename, 'rb')
	buffers = binfile.read()
 
	head = struct.unpack_from('>II' , buffers ,0)
	print "head,",head
	imgNum=head[1]
 
	offset = struct.calcsize('>II')
	numString = '>'+str(imgNum)+"B"
	labels = struct.unpack_from(numString , buffers , offset)
	binfile.close()
	labels = np.reshape(labels,[imgNum,1])
 
	print 'load label finished'
	return labels
 
if __name__=="__main__":
 
	imgs = loadImageSet("train-images.idx3-ubyte")
	labels = loadLabelSet("train-labels.idx1-ubyte")

为了测试是否读取成功只读取一张图片的python 代码

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()


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值