[QUESTION]-Chap3 Classification
KeyError: 0 is thrown as an error while the below set of code is executed.
import matplotlib as mpl
import matplotlib.pyplot as plt
digit = x[0]
digit = digit.reshape(28, 28)
plt.imshow(digit, cmap="binary")
plt.axis("off")
plt.show()
Full stacktrace here:
KeyError Traceback (most recent call last)
[/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py](https://localhost:8080/#) in get_loc(self, key, method, tolerance)
3360 try:
-> 3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
4 frames
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: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
[/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py](https://localhost:8080/#) in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 0
Expected behavior
Should print a image as below

Screenshot

Hi @BHariKrishnaReddy ,
Thanks for your feedback. The problem comes from the fact that fetch_openml() started returning Pandas DataFrames instead of NumPy arrays since Scikit-Learn 0.24. This messes up a lot of code examples. Luckily, there's a trivial fix: just set as_frame=False when calling fetch_openml(), and everything will run smoothly:
mnist = fetch_openml('mnist_784', version=1, as_frame=False)
Hope this helps!
Hi @BHariKrishnaReddy , Thanks for your feedback. The problem comes from the fact that
fetch_openml()started returning Pandas DataFrames instead of NumPy arrays since Scikit-Learn 0.24. This messes up a lot of code examples. Luckily, there's a trivial fix: just setas_frame=Falsewhen callingfetch_openml(), and everything will run smoothly:mnist = fetch_openml('mnist_784', version=1, as_frame=True)Hope this helps!
it should be
mnist = fetch_openml('mnist_784', version=1, as_frame=False)
hh