KeyError while fetching MNIST data

Viewed 656

I'm trying to play around MNSIT dataset and here is the error I got in the very beginning. Error while fetching MNIST image

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
x, y = mnist['data'], mnist['target']
digit = x[36001]
2 Answers

Expanding my comment, I think the MNIST dataset of openml was recently (?) switched to return a pandas DataFrame instead of a numpy array. Another possible fix is in the function call:

mnist = fetch_openml('mnist_784', as_frame=False)

x is 2-dimensional; try this:

digit = x[36001, :]

Since, you're facing an error, which must be due to x being not treated as a numpy array, try the following two methods:

digit = x.iloc[36001, :]

Or

digit = x.to_numpy()[36001,:]
Related