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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37228371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:05:19  来源:igfitidea点击:

Visualize MNIST dataset using OpenCV or Matplotlib/Pyplot

pythonopencvmatplotlibmachine-learningmnist

提问by decipher

i have MNIST dataset and i am trying to visualise it using pyplot. The dataset is in cvsformat where each row is one image of 784 pixels. i want to visualise it in pyplotor opencvin the 28*28 image format. I am trying directly using :

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

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

but i its not working? any ideas on how should i approach this.

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

回答by bakkal

Assuming you have a CSV file with this format, which is a format the MNIST dataset is available in

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

label, pixel_1_1, pixel_1_2, ...

Here's how you can visulize it in Python with Matplotlib and then OpenCV

以下是如何使用 Matplotlib 和 OpenCV 在 Python 中对其进行可视化

Matplotlib / Pyplot

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

OpenCV

You can take the pixelsnumpy array from above which is of dtype='uint8'(unsigned 8-bits integer) and shape 28 x 28 , and plot with cv2.imshow()

您可以pixels从上面获取numpy 数组,它是dtype='uint8'(unsigned 8-bits integer) 和形状 28 x 28 ,并用绘图cv2.imshow()

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

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

回答by cpury

For all like me who want a quick and dirty solution, simply to get a rough idea what a given input is about, in-console and without fancy libraries:

对于像我这样想要快速而肮脏的解决方案的人来说,只需粗略地了解给定的输入是关于什么的,在控制台中并且没有花哨的库:

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))

(expects the input to be shaped like [784]and with float values from 0 to 1. If either is not the case, you can easily convert (e.g. pixels = pixels.reshape((784,))or pixels \= 255)

(期望输入的形状像[784]0 到 1 的浮点值。如果不是这种情况,您可以轻松转换(例如pixels = pixels.reshape((784,))pixels \= 255

Output

输出

The output is a bit distorted but you get the idea.

输出有点失真,但你明白了。

回答by Suraj Subramanian

Importing necessary packages

导入必要的包

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

Reading mnist train dataset ( which is csv formatted ) as a pandas dataframe

将 mnist 训练数据集(采用 csv 格式)作为 Pandas 数据框读取

s = pd.read_csv("mnist_train.csv")

Converting the pandas dataframe to a numpy matrix

将 Pandas 数据帧转换为 numpy 矩阵

data = np.matrix(s)

The first column contains the label, so store it in a separate array

第一列包含标签,因此将其存储在单独的数组中

output = data[:, 0]

And delete the first column from the data matrix

并从数据矩阵中删除第一列

data = np.delete(data, 0, 1)

The first row represents the first image, it is 28X28 image (stored as 784 pixels)

第一行代表第一张图片,是28X28的图片(存储为784像素)

img = data[0].reshape(28,28)

[And displaying the image][1]
plt.imshow(img, cmap="gray")

enter image description here

在此处输入图片说明