原文链接

使用方法

下载原始数据后,置于工程目录下,解压,运行以下代码即可
使用数据:其中训练集包含60,000个样本,测试集包含10,000个样本

  1. 训练集images: train-images-idx3-ubyte.gz
  2. 训练集labels: train-labels-idx1-ubyte.gz
  3. 测试集images: t10k-images-idx3-ubyte.gz
  4. 测试集labels: t10k-labels-idx1-ubyte.gz

转化后:mnist_train.csv,mnist_test.csv两个文件

  1. def convert(imgf, labelf, outf, n):
  2. f = open(imgf, 'rb')
  3. o = open(outf, 'w')
  4. l = open(labelf, 'rb')
  5. f.read(16)
  6. l.read(8)
  7. images = []
  8. for i in range(n):
  9. image = [ord(l.read(1))]
  10. for j in range(28 * 28):
  11. image.append(ord(f.read(1)))
  12. images.append(image)
  13. for image in images:
  14. o.write(','.join(str(pix) for pix in image) + '\n')
  15. f.close()
  16. o.close()
  17. l.close()
  18. convert('MNIST/train-images.idx3-ubyte', 'MNIST/train-labels.idx1-ubyte', 'mnist_train.csv', 60000)
  19. convert('MNIST/t10k-images.idx3-ubyte', 'MNIST/t10k-labels.idx1-ubyte', 'mnist_test.csv', 10000)
  20. print("Convert Finished")

matlab版本

https://download.csdn.net/download/u012183487/10164134