Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue reshaping an array into (28, 28) for an MNIST image

When running this code, I want to show the image in matplotlib but am I getting an error in some_digit_image = some_digit.reshape(28, 28) where I get the ValueError: cannot reshape array of size 1 into shape (28, 28). Is there any way to fix this issue and get the image in the matplotlib. Thanks for helping me solve the issue.

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')

X, y = mnist["data"], mnist["target"]


%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

some_digit = X.loc[[36000]]
print(some_digit)

some_digit_image = some_digit.reshape(28, 28)

plt.imshow(some_digit_image, cmap=matplotlib.cm.binary, interpolation='nearest')
plt.axis('off')
plt.show()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are trying to perform reshape on dataframe object. Convert the data frame object to numpy array (some_digit = X.loc[[36000]].to_numpy()). It would fix the issue.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading