0

I'm working with the Mnist data set, in order to learn about Machine learning, and as for now I'm trying to display the first digit in the Mnist data set as an image, and I have encountered a problem.

I have a matrix with the dimensions 784x10000, where each column is a digit in the data set. I have created the matrix myself, because the Mnist data set came in the form of a text file, which in itself caused me quite a lot of problems, but that's a question for itself.

The MN_train matrix below, is my large 784x10000 matrix. So what I'm trying to do below, is to fill up a 28x28 matrix, in order to display my image.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

grey = np.zeros(shape=(28,28))
k = 0

for l in range(28):
    for p in range(28):
        grey[p,l]=MN_train[k,0]
        k = k + 1
print grey
plt.show(grey) 

But when I try to display the image, I get the following error:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Followed by a image plot that does not look like the number five, as I would expect.

Is there something I have overlooked, or does this tell me that my manipulation of the text file, in order to construct the MN_train matrix, has resulted in an error?

1 Answer 1

1

The error you get is because you supply the array to show. show accepts only a single boolean argument hold=True or False.
In order to create an image plot, you need to use imshow.

plt.imshow(grey)
plt.show()  # <- no argument here

Also note that the loop is rather inefficient. You may just reshape the input column array.

The complete code would then look like

import numpy as np
import matplotlib.pyplot as plt

MN_train = np.loadtxt( ... )

grey = MN_train[:,0].reshape((28,28))

plt.imshow(grey)
plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing that our @ImportanceOfBeingErnest! Now I can surely see that my manipulation of the text file has resulted in an error haha Back to work then.

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.