读取mnist数据集显示图片信息

MNIST数据集下载地址https://download.csdn.net/download/weixin_33595571/10826617
QQ群:476842922(欢迎加群讨论学习)
import numpy as np
import struct
import matplotlib.pyplot as plt
 
filename = 'train-images.idx3-ubyte'
binfile = open(filename , 'rb')
buf = binfile.read()
 
index = 1
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()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_33595571/article/details/84785640