pythonscikit-learnmnist

KeyError while fetching MNIST data


I'm trying to play around MNIST dataset:

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

Here is the error I got at the very beginning:

Error screenshot to text:

KeyError                                  Traceback (most recent call last) 
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key) 
   2898             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas. libs.hashtable. PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable. PyObjectHashTable.get_item()

KeyError: 36000

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-8-8b8bb0b2ff6d> in <module>
----> 1 some_digit = X[36000] # Selecting the 36,000th image.

Solution

  • 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,:]