手写数字集MNIST(1)下载

ML界的Hello World:手写数字集MNIST(1)下载

下载、显示MNIST数据集
import tensorflow as tf
import matplotlib.pyplot as plt

# 取别名
mnist = tf.keras.datasets.mnist
# 下载
(train_x, train_y), (test_x, test_y) = mnist.load_data()
print("Training set:", len(train_x))  # Training set: 60000
print("Testing set:", len(test_x))  # Testing set: 10000
print(train_x.shape, train_x.dtype)  # (60000, 28, 28) uint8
print(train_y.shape, train_y.dtype)  # (60000,) uint8

# plt.axis("off")
plt.imshow(train_x[0], cmap="gray")
plt.show()
随机显示4幅图片
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

# 取别名
mnist = tf.keras.datasets.mnist
# 下载
(train_x, train_y), (test_x, test_y) = mnist.load_data()

# 依靠matplotlib随机显示4幅图片
for i in range(4):
    num = np.random.randint(1, train_x.shape[0])

    plt.subplot(1, 4, i+1)
    plt.axis("off")
    plt.imshow(train_x[num], cmap="gray")
    plt.title(train_y[num])

plt.show()

你可能感兴趣的:(图像,tensorflow)