i have MNIST dataset and i am trying to visualise it using pyplot. The dataset is in cvs
format where each row is one image of 784 pixels. i want to visualise it in pyplot
or opencv
in the 28*28 image format. I am trying directly using :
plt.imshow(X[2:],cmap =plt.cm.gray_r, interpolation = "nearest")
but i its not working? any ideas on how should i approach this.
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:
(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,))
orpixels \= 255
)The output is a bit distorted but you get the idea.
Assuming you have a CSV file with this format, which is a format the MNIST dataset is available in
Here's how you can visulize it in Python with Matplotlib and then OpenCV
Matplotlib / Pyplot
OpenCV
You can take the
pixels
numpy array from above which is ofdtype='uint8'
(unsigned 8-bits integer) and shape 28 x 28 , and plot withcv2.imshow()
Importing necessary packages
Reading mnist train dataset ( which is csv formatted ) as a pandas dataframe
Converting the pandas dataframe to a numpy matrix
The first column contains the label, so store it in a separate array
And delete the first column from the data matrix
The first row represents the first image, it is 28X28 image (stored as 784 pixels)