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

[BUG] 03_classification.ipynb - Assignment issue between DataFrame to Array

Open selcukbeyhan opened this issue 4 years ago • 2 comments

I am not a python expert but I realized there is probably a compatibility issue with following code:

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

some_digit = X[0]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap=mpl.cm.binary)
plt.axis("off")

save_fig("some_digit_plot")
plt.show()

The assignment some_digit = X[0] throws an error.

I managed to fix this like this:

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

some_digit = X[0:1]
some_digit_arr = np.asarray(some_digit)
some_digit_image = some_digit_arr.reshape(28, 28)

plt.imshow(some_digit_image, cmap="binary")
plt.axis("off")

save_fig("some_digit_plot")
plt.show()

Thank you.

selcukbeyhan avatar Jul 02 '21 15:07 selcukbeyhan

Hi @selcukbeyhan ,

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!

ageron avatar Jul 06 '21 02:07 ageron

Thank you very much!

wzqwtt avatar Oct 13 '21 08:10 wzqwtt