gpt4 book ai didi

python - 使用 OpenCV 或 Matplotlib/Pyplot 可视化 MNIST 数据集

转载 作者:太空狗 更新时间:2023-10-29 19:31:27 27 4
gpt4 key购买 nike

我有 MNIST 数据集,我正在尝试使用 pyplot 将其可视化。数据集采用 cvs 格式,其中每一行是一张 784 像素的图像。我想在 pyplotopencv 中以 28*28 图像格式将其可视化。我正在尝试直接使用:

plt.imshow(X[2:],cmap =plt.cm.gray_r, interpolation = "nearest") 

但它不起作用?关于我应该如何处理这个问题的任何想法。

最佳答案

假设您有一个这种格式的 CSV 文件,这是 MNIST 数据集可用的格式

label, pixel_1_1, pixel_1_2, ...

以下是如何使用 Python 使用 Matplotlib 然后使用 OpenCV 将其可视化

Matplotlib/Pyplot

import numpy as np
import csv
import matplotlib.pyplot as plt

with open('mnist_test_10.csv', 'r') as csv_file:
for data in csv.reader(csv_file):
# The first column is the label
label = data[0]

# The rest of columns are pixels
pixels = data[1:]

# Make those columns into a array of 8-bits pixels
# This array will be of 1D with length 784
# The pixel intensity values are integers from 0 to 255
pixels = np.array(pixels, dtype='uint8')

# Reshape the array into 28 x 28 array (2-dimensional array)
pixels = pixels.reshape((28, 28))

# Plot
plt.title('Label is {label}'.format(label=label))
plt.imshow(pixels, cmap='gray')
plt.show()

break # This stops the loop, I just want to see one

enter image description here

OpenCV

您可以从上面获取 pixels numpy 数组,它是 dtype='uint8'(无符号 8 位整数)和形状 28 x 28 ,并绘制cv2.imshow()

    title = 'Label is {label}'.format(label=label)

cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 使用 OpenCV 或 Matplotlib/Pyplot 可视化 MNIST 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com