fastai / fastbook

The fastai book, published as Jupyter Notebooks
Other
21.99k stars 8.46k forks source link

Outdated sklearn.inspection.plot_partial_dependence #629

Open st0fm opened 11 months ago

st0fm commented 11 months ago

The function sklearn.inspection.plot_partial_dependence is not implemented in newer scikit-learn versions. For example, when trying to execute the partial dependence example from chapter 09_tabular.ipynb in "Model Interpretation -> Partial Dependence" I will get the following error:

from sklearn.inspection import plot_partial_dependence

fig,ax = plt.subplots(figsize=(12, 4))
plot_partial_dependence(m, valid_xs_final, ['YearMade','ProductSize'],
                        grid_resolution=20, ax=ax);

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[331], line 1
----> 1 from sklearn.inspection import plot_partial_dependence
      3 fig,ax = plt.subplots(figsize=(12, 4))
      4 plot_partial_dependence(m, valid_xs_final, ['YearMade','ProductSize'],
      5                         grid_resolution=20, ax=ax);

ImportError: cannot import name 'plot_partial_dependence' from 'sklearn.inspection'

Tested with scikit-learn version:

!pip freeze | grep scikit-learn
scikit-learn==1.3.1

In the documetation of scikit-learn I can't find sklearn.inspection.plot_partial_dependence after version 1.1.

In the newer scikit versions (1.2 or 1.3) we can use sklearn.inspection.PartialDependenceDisplay.from_estimator instead

For example:

from sklearn.inspection import PartialDependenceDisplay

fig,ax = plt.subplots(figsize=(12, 4))
PartialDependenceDisplay.from_estimator(m, valid_xs_final, ['YearMade', 'ProductSize'], 
                                        grid_resolution=20, ax=ax)

References:

mattsgarlata commented 9 months ago

Thanks for filing this! It was a big help for me.

If the maintainers will accept a PR to fix this in chapter 9, I'd be happy to supply it. I'll wait until this PR is merged first though: #634