使用 OpenCV 或 Matplotlib/Pyplot 可视化 MNIST 数据集
Visualize MNIST dataset using OpenCV or Matplotlib/Pyplot
我有 MNIST 数据集,我正在尝试使用 pyplot 将其可视化。数据集采用 cvs
格式,其中每一行是一张 784 像素的图像。我想以 pyplot
或 opencv
的 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
OpenCV
你可以从上面获取 pixels
numpy 数组,它是 dtype='uint8'
(无符号 8 位整数)和形状 28 x 28 ,并用 cv2.imshow()
[=17 绘制=]
title = 'Label is {label}'.format(label=label)
cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()
对于所有像我一样想要一个快速而肮脏的解决方案的人来说,只是为了粗略地了解给定的输入是关于什么的,在控制台中并且没有花哨的库:
def print_greyscale(pixels, width=28, height=28):
def get_single_greyscale(pixel):
val = 232 + round(pixel * 23)
return '\x1b[48;5;{}m \x1b[0m'.format(int(val))
for l in range(height):
line_pixels = pixels[l * width:(l+1) * width]
print(''.join(get_single_greyscale(p) for p in line_pixels))
(期望输入的形状像 [784]
并且浮点值从 0 到 1。如果不是这种情况,您可以轻松转换(例如 pixels = pixels.reshape((784,))
或 pixels \= 255
)
输出有点失真,但你明白了。
正在导入必要的包
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
将 mnist 训练数据集(csv 格式)读取为 pandas 数据帧
s = pd.read_csv("mnist_train.csv")
将 pandas 数据帧转换为 numpy 矩阵
data = np.matrix(s)
第一列包含标签,因此将其存储在一个单独的数组中
output = data[:, 0]
并从数据矩阵中删除第一列
data = np.delete(data, 0, 1)
第一行代表第一张图片,是28X28的图片(存储为784像素)
img = data[0].reshape(28,28)
# And displaying the image
plt.imshow(img, cmap="gray")
我有 MNIST 数据集,我正在尝试使用 pyplot 将其可视化。数据集采用 cvs
格式,其中每一行是一张 784 像素的图像。我想以 pyplot
或 opencv
的 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
OpenCV
你可以从上面获取 pixels
numpy 数组,它是 dtype='uint8'
(无符号 8 位整数)和形状 28 x 28 ,并用 cv2.imshow()
[=17 绘制=]
title = 'Label is {label}'.format(label=label)
cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()
对于所有像我一样想要一个快速而肮脏的解决方案的人来说,只是为了粗略地了解给定的输入是关于什么的,在控制台中并且没有花哨的库:
def print_greyscale(pixels, width=28, height=28):
def get_single_greyscale(pixel):
val = 232 + round(pixel * 23)
return '\x1b[48;5;{}m \x1b[0m'.format(int(val))
for l in range(height):
line_pixels = pixels[l * width:(l+1) * width]
print(''.join(get_single_greyscale(p) for p in line_pixels))
(期望输入的形状像 [784]
并且浮点值从 0 到 1。如果不是这种情况,您可以轻松转换(例如 pixels = pixels.reshape((784,))
或 pixels \= 255
)
输出有点失真,但你明白了。
正在导入必要的包
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
将 mnist 训练数据集(csv 格式)读取为 pandas 数据帧
s = pd.read_csv("mnist_train.csv")
将 pandas 数据帧转换为 numpy 矩阵
data = np.matrix(s)
第一列包含标签,因此将其存储在一个单独的数组中
output = data[:, 0]
并从数据矩阵中删除第一列
data = np.delete(data, 0, 1)
第一行代表第一张图片,是28X28的图片(存储为784像素)
img = data[0].reshape(28,28)
# And displaying the image
plt.imshow(img, cmap="gray")