MNIST 手写数据集介绍

1,139 阅读1分钟

1、数据集介绍

  • MNIST数据集是机器学习领域中非常经典的一个数据集,最简单的方法就是使用如下代码直接加载:
import tensorflow as tf
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)
  • 可以看出数据集由60000个训练样本和10000个测试样本组成
  • 每个样本都是一张28 * 28像素的灰度手写数字图片
  • 每个像素点是一个0-255的整数

2、打印第一个手写图片

import matplotlib.pyplot as plt

plt.figure()
plt.imshow(X_train[0])
plt.colorbar()
plt.grid(False)
plt.show()

ty8ZGV.png

3、打印前25张手写数字

# 把像素值缩放到 0-1
X_train = X_train / 255.0
X_test = X_test / 255.0
# 所有的分类标签
class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(X_train[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[y_train[i]])

ty8VP0.png