本文主要介绍对下载到本地的MNIST或FashionMNIST数据集如何加载解析的问题。下载到本地的数据集一般有两种格式:numpy的压缩格式.npz,以及gzip压缩格式.gz,下面我们分别介绍,在以下介绍中,均假设读者已经将数据集下载到本地
1. 加载MNIST(npz格式数据集加载)
# npz格式数据集的加载代码非常简单,直接用numpy的load函数即可
import numpy as np
# 假设数据保存在'./datasets/'文件夹下
try:
data = np.load('./datasets/mnist.npz')
x_train, y_train, x_test, y_test = data['x_train'],data['y_train'],data['x_test'],data['y_test']
# 可以将其中一条数据保存成txt文件,查看一下,会对这组数据有个直观的感受
# np.savetxt('test.txt',x_train[0],fmt='%3d',newline='\n\n')
# 将数据归一化
x_train, x_test = x_train/255.0, x_test/255.0
except Exception as e:
print('%s' %e)
2. 加载FashionMNIST(gz格式数据的加载)
# 查看数据集图片
(train_images, train_labels), (test_im