MINIST数据集读取

本文将直接以代码形式展示,句释已给出:

import os
import struct
import numpy as np
import matplotlib.pyplot as plt

data_path = r'D:\Data\minist/'

def Minst_Load(path, kind='train'):
    """Load MNIST data from `path`"""

    images_path = os.path.join(path, '{}-images.idx3-ubyte'.format(kind))
    labels_path = os.path.join(path, '{}-labels.idx1-ubyte'.format(kind))

    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
    # images: n x m, n is number of samples, m is 784 pixels
    # labels: n
features, labels = Minst_Load(data_path)  # features: (60000, 784), labels: (60000,)
# np.savetxt('train_img.csv', features, fmt='%i', delimiter=',')     convert numpy array to csv file
# features = np.genfromtxt('train_img.csv', dtype=int, delimiter=',')   read csv file into numpy array

# Show
x_train, y_train = [], labels
for i in range(len(features)):
    x_train.append(np.reshape(features[i], (28, 28)))
x_train = np.array(x_train)

plt.imshow(x_train[111], cmap='gray')  # Grayscale
plt.show()
print(y_train[111])

弄清格式后,代码可直接使用,不必弄清每一步。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值