19

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.

2
  • Maybe youd include some more details, showing how you include your data and what exactly is "Not working", this may help you get quicker answers. Commented May 14, 2016 at 16:57
  • @bakkal ...thanks dude it works perfectly fine Commented May 17, 2016 at 18:36

3 Answers 3

46

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

label, pixel_1_1, pixel_1_2, ...

Here's how you can visulize it in Python with Matplotlib and then 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

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

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

    cv2.imshow(title, pixels)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

Comments

9

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

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

Converting the pandas dataframe to a numpy matrix

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)

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

# And displaying the image
plt.imshow(img, cmap="gray")

output image

Comments

6

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)

Output

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.