import numpy as np
import matplotlib.pyplot as pltdef read_mnist_images(filename):with open(filename, 'rb') as f:# 读取魔术数字、图像数量、行数、列数magic_number = int.from_bytes(f.read(4), 'big')number_of_images = int.from_bytes(f.read(4), 'big')rows = int.from_bytes(f.read(4), 'big')cols = int.from_bytes(f.read(4), 'big')# 读取图像数据images = np.frombuffer(f.read(), dtype=np.uint8)images = images.reshape((number_of_images, rows, cols))return images# 修改为您的路径
filename = './data/MNIST/raw/train-images-idx3-ubyte'images = read_mnist_images(filename)# 显示第一张图像
plt.imshow(images[0], cmap='gray')
plt.title("First Image in MNIST Dataset")
plt.show()
把最后第三行的0,修改成别的数字,就会显示某张数字。
运行之后,结果如下