ageron / handson-ml2

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.
Apache License 2.0
27.9k stars 12.76k forks source link

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

Open selcukbeyhan opened 3 years ago

selcukbeyhan commented 3 years ago

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.

ageron commented 3 years ago

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!

wzqwtt commented 3 years ago

Thank you very much!