MNIST数据集是一个手写识别数据集,机器学习基础的数据集,其原始数据集以字节形式存储,包含四个部分:
- 训练集images: train-images-idx3-ubyte.gz (包含60000个样本)
- 训练集labels: train-labels-idx1-ubyte.gz (包含60000个标签)
- 测试集images: t10k-images-idx3-ubyte.gz (包含10000个样本)
- 测试集labels: t10k-labels-idx1-ubyte.gz (包含10000个标签)
学习过程中,原始的数据格式不太习惯,遂根据相关资料,将其转化为csv格式,分别存储在mnist_train.csv,mnist_test.csv两个文件中,供学习使用。
MNIST数据集下载:
http://yann.lecun.com/exdb/mnist/ 或 https://download.csdn.net/download/albert201605/10340814
转化方法:下载原始数据后,置于工程目录下,解压,运行以下代码即可。
def convert(imgf, labelf, outf, n):
f = open(imgf, "rb")
o = open(outf, "w")
l = open(labelf, "rb")
f.read(16)
l.read(8)
images = []
for i in range(n):
image = [ord(l.read(1))]
for j in range(28*28):
image.append(ord(f.read(1)))
images.append(image)
for image in images:
o.write(",".join(str(pix) for pix in image)+"\n")
f.close()
o.close()
l.close()
convert("MNIST/train-images.idx3-ubyte", "MNIST/train-labels.idx1-ubyte",
"mnist_train.csv", 60000)
convert("MNIST/t10k-images.idx3-ubyte", "MNIST/t10k-labels.idx1-ubyte",
"mnist_test.csv", 10000)
print("Convert Finished!")
参考:
- https://blog.csdn.net/simple_the_best/article/details/75267863
- https://pjreddie.com/projects/mnist-in-csv/