导入本地已下载的MNIST数据集

导入本地已下载的MNIST数据集,四个文件:
t10k-images-idx3-ubyte.gz
t10k-labels-idx1-ubyte.gz
train-images-idx3-ubyte.gz
train-labels-idx1-ubyte.gz

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

def load_data_gz(data_folder):
    files = ['train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz',
             't10k-images-idx3-ubyte.gz']

    paths = []
    for fname in files:
        paths.append(os.path.join(data_folder, fname))

    //# 读取每个文件夹的数据
    with gzip.open(paths[0], 'rb') as lbpath:
        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[1], 'rb') as imgpath:
        x_train = np.frombuffer(imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 784)

    with gzip.open(paths[2], 'rb') as lbpath:
        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[3], 'rb') as imgpath:
        x_test = np.frombuffer(imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 784)

    return x_train, y_train, x_test, y_test

//# 调用load_data_gz函数加载数据集
data_folder = '\\mnist_dataset'
x_train_gz, y_train_gz, x_test_gz, y_test_gz = load_data_gz(data_folder)

print('x_train_gz.shape:', x_train_gz.shape)
print('y_train_gz.shape', y_train_gz.shape)
print('x_test_gz.shape:', x_test_gz.shape)
print('y_test_gz.shape:', y_test_gz.shape)

输出结果为:

x_train_gz.shape: (60000, 784)
y_train_gz.shape (60000,)
x_test_gz.shape: (10000, 784)
y_test_gz.shape: (10000,)

将长度784的向量转换为28*28的图像

train_image = np.zeros([60000, 28, 28]).astype(np.float32)

for i in range(x_train_gz.shape[0]):
    re = x_train_gz[i, :].reshape(28, 28)
    train_image[i, :, :] = re
print('train_image.shape: ', train_image.shape)

输出结果为:

train_image.shape:  (60000, 28, 28)

选取前16张查看:


plt.figure()
for i in range(16):
    plt.subplot(4, 4, i+1)
    plt.imshow(train_image[i, :, :], 'gray')
    plt.axis('off')
plt.show()

显示结果:
在这里插入图片描述

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值