下载 地址:http://yann.lecun.com/exdb/mnist/ 解压 如图 上传 使用的是Jupyter Notebook,所以有这一步,其他编辑器自己考虑路径 代码
import os import struct import numpy as np def read_mnist(labels_path,images_path): with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II', lbpath.read(8)) labels = np.fromfile(lbpath, dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII', imgpath.read(16)) images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) return images, labels labels_path = 't10k-labels.idx1-ubyte' #标签文件路径 images_path = 't10k-images.idx3-ubyte' #图片文件路径 test_images, test_labels = read_mnist(labels_path,images_path) print(len(test_labels)) print(test_labels[0:15]) print(len(test_images)) print(len(test_images[0]))此处以文件较小的测试集为例,训练集同理。 由运行结果可知,测试集里有10000个样本。images储存样本的特征,特征向量784维。labels储存样本的分类类别,有[0-9]类。