handson-ml2 icon indicating copy to clipboard operation
handson-ml2 copied to clipboard

[QUESTION]-Chap3 Classification

Open BHariKrishnaReddy opened this issue 3 years ago • 2 comments

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 2022-02-03 at 10 56 22 AM

Screenshot Screenshot 2022-02-03 at 10 54 52 AM

BHariKrishnaReddy avatar Feb 03 '22 05:02 BHariKrishnaReddy

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!

ageron avatar Feb 03 '22 19:02 ageron

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=True)

Hope this helps!

it should be

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

hh

239what475 avatar Apr 02 '22 06:04 239what475