Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mhham
Copy link
Contributor

@mhham mhham commented Feb 10, 2021

This PR implements the feature specified in #18976 :

Add two keyword arguments to sklearn/inspection/_plot/partial_dependence.py:plot_partial_dependence : ice_lines_kw and pd_line_kw. They take priority over line_kw when not None and allow to format the ICE (individual) lines and the PD (average) line separately

Example:

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import fetch_california_housing
from sklearn.inspection import plot_partial_dependence

X, y = fetch_california_housing(return_X_y=True, as_frame=True)
features = ['MedInc', 'AveOccup', 'HouseAge', 'AveRooms', 'AveOccup']
est = RandomForestRegressor(n_estimators=10)
est.fit(X, y)
display = plot_partial_dependence(
       est, X, features, kind="both", subsample=100,
       n_jobs=3, grid_resolution=20, random_state=0,
       line_kw={"color": "g", "linewidth": 5},
       pd_line_kw={"color": "r"},
       ice_lines_kw={"linewidth": 0.5, "color":"b", "alpha": 0.3},
)

Which outputs:
image

@glemaitre glemaitre changed the title Add kwargs to format ICE and PD lines separately in partial dependence plots ENH Add kwargs to format ICE and PD lines separately in partial dependence plots Feb 10, 2021
@glemaitre
Copy link
Member

Please add an entry to the change log at doc/whats_new/v*.rst. Like the other entries there, please reference this pull request with :pr: and credit yourself (and other contributors if applicable) with :user:.

We would also need some test to check the behaviour. These tests are located in test_partial_dependence.py. We will have to check that setting the line will change the the plot. We can inspect the matplotlib axis for such checking.

What would be important is to check that if the new keywords are used, they takeover the previous available keyword.
We should also raise a warning if all keyword are set or maybe an error since it would not give a reasonable output.

Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also use the keyword in the documentation: https://scikit-learn.org/stable/auto_examples/inspection/plot_partial_dependence.html#sphx-glr-auto-examples-inspection-plot-partial-dependence-py to highlight the average line in a different color than blue.

@cmarmo
Copy link
Contributor

cmarmo commented Feb 24, 2021

Hi @mhham thanks for your work so far! Do you mind synchronizing with upstream? Thanks!

Copy link
Contributor

@cmarmo cmarmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mhham for your work and your patience!
Do you mind modifying the example to show the enhancement you implemented? This is quite an improvement in the visualization!
Also, in the meanwhile some conflicts arose...

@mhham
Copy link
Contributor Author

mhham commented Apr 7, 2021

Thanks @mhham for your work and your patience!
Do you mind modifying the example to show the enhancement you implemented? This is quite an improvement in the visualization!
Also, in the meanwhile some conflicts arose...

All done 👍

@glemaitre
Copy link
Member

I will review this PR shortly

@glemaitre glemaitre self-requested a review April 9, 2021 08:33
Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am finally back on the PDP feature. I merge the master changes into this PR. I made a couple of comments with a bit of cosmetic for the rendering but the core code looks fine.

Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jjerphan jjerphan self-requested a review July 13, 2021 14:07
@glemaitre glemaitre removed their assignment Jul 17, 2021
Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution @mhham!

I have a suggestion to prevent irrelevant warnings to be raised.

@glemaitre glemaitre self-assigned this Jul 30, 2021
@glemaitre glemaitre removed their assignment Jul 30, 2021
Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @mhham !

@glemaitre glemaitre self-assigned this Aug 2, 2021
@mhham
Copy link
Contributor Author

mhham commented Aug 2, 2021

Thank you all for the subsequent work 👍 !

Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Thanks @mhham; this helps better interpreting partial dependencies plots. 👍

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments on the docstrings, otherwise LGTM.

ax = Axes3D(fig)
surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1,
cmap=plt.cm.BuPu, edgecolor='k')
ax = Axes3D(fig, auto_add_to_figure=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AttributeError: Unknown property auto_add_to_figure

So it does not seem to be supported in all matplotlib version?

@glemaitre glemaitre merged commit 696ca3d into scikit-learn:main Aug 16, 2021
samronsin pushed a commit to samronsin/scikit-learn that referenced this pull request Nov 30, 2021
…dence plots (scikit-learn#19428)

Co-authored-by: Guillaume Lemaitre <[email protected]>
Co-authored-by: Chiara Marmo <[email protected]>
Co-authored-by: Julien Jerphanion <[email protected]>
Co-authored-by: Thomas J. Fan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants