python - 无法绘制 MNIST 数字

标签 python matplotlib computer-vision mnist

我正在尝试加载和可视化 MNIST 数字,但我得到的数字具有移位的像素

import matplotlib.pyplot as plt
import numpy as np

mnist_data  = open('data/mnist/train-images-idx3-ubyte', 'rb')

image_size = 28
num_images = 4

buf = mnist_data.read(num_images * image_size * image_size)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size)

_, axarr1 = plt.subplots(2,2)
axarr1[0, 0].imshow(data[0])
axarr1[0, 1].imshow(data[1])
axarr1[1, 0].imshow(data[2])
axarr1[1, 1].imshow(data[3])

MNIST

谁能告诉我为什么会这样,代码看起来没问题,谢谢

最佳答案

你没有说你从哪里获得 MNIST 数据,但是,if it is formatted like the original data set ,您似乎忘记在尝试访问数据之前提取 header :

image_size = 28
num_images = 4

mnist_data = open('train-images-idx3-ubyte', 'rb')

mnist_data.seek(16) # skip over the first 16 bytes that correspond to the header
buf = mnist_data.read(num_images * image_size * image_size)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size)

_, axarr1 = plt.subplots(2,2)
axarr1[0, 0].imshow(data[0])
axarr1[0, 1].imshow(data[1])
axarr1[1, 0].imshow(data[2])
axarr1[1, 1].imshow(data[3])

enter image description here

关于python - 无法绘制 MNIST 数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56004586/

相关文章:

python - 如何使用单词范围对象读取项目符号/编号列表?

python - 如何优化 pandas 中的数据帧迭代?

Python绘制实时数据

python - Matplotlib 箱线图 : what algorithm is used to calculate range and identify outliers?

opencv - 使图像的背景变白

python - 我什么时候应该在 python 中使用 uuid.uuid1() 和 uuid.uuid4()?

python - if 语句后的 "UnboundLocalError: local variable referenced before assignment"

python - 转换错误: Failed to convert value(s) to axis units

opencv - 将2个直方图与卡方进行比较

opencv - 如何在OpenCV中变换图像以匹配圆形模型