Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Deprecates plot_partial_dependence #20959

Merged
merged 11 commits into from Sep 9, 2021

Conversation

thomasjpfan
Copy link
Member

Deprecates plot_partial_dependence for PartialDependenceDisplay.from_estimator.

The testing in sklearn/inspection/_plot/tests/test_plot_partial_dependence.py switches to the from_estimator version and does not call plot_partial_dependence. I think this is okay given that plot_partial_dependence is a simple redirect to the class method.

CC @glemaitre

@thomasjpfan
Copy link
Member Author

This is the remaining plotting function that does not have a class method. Placing on 1.0

@thomasjpfan thomasjpfan added this to the 1.0 milestone Sep 6, 2021
@glemaitre
Copy link
Member

I wanted to have:

before to touch this. I was feeling that it would be much less of a hassle than solving the conflict :)
But if we are really in a rush, let's go this way.

@glemaitre
Copy link
Member

I am going to review it in the morning (CET time :))

@thomasjpfan
Copy link
Member Author

before to touch this. I was feeling that it would be much less of a hassle than solving the conflict :)
But if we are really in a rush, let's go this way.

Yea it would have been better to get some of those PRs in first. Things seem to be moving along very quickly.

I am going to review it in the morning (CET time :))

Thank youuu!

@glemaitre glemaitre self-requested a review September 7, 2021 07:41
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.

A quick first past by checking with a couple of occurrences forgotten elsewhere:

  • In the function partial_dependence, in the "See Also" section, we should refer to the .from_estimator method and remove the entry for the plot_partial_dependence function.

I will have a deeper look on the code itself now but since that all tests are passing, I think that there is no reason for it to be OK.

@@ -64,7 +64,7 @@ Plotting with Multiple Axes
---------------------------

Some of the plotting tools like
:func:`~sklearn.inspection.plot_partial_dependence` and
:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` and
:class:`~sklearn.inspection.PartialDependenceDisplay` support plottong on
Copy link
Member

Choose a reason for hiding this comment

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

plottong -> plotting

Copy link
Member

Choose a reason for hiding this comment

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

There is another occurence of plot_partial_dependence in l.90

@@ -34,13 +35,19 @@ def clf_diabetes(diabetes):
return clf


def test_plot_partial_dependence_deprecation(pyplot, clf_diabetes, diabetes):
Copy link
Member

Choose a reason for hiding this comment

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

Could you isolate all the test of the plot_partial_dependence in a py file. Following what I did for the other, the file would be name test_partial_dependence_display.py

Then you can let the test_plot_partial_dependence.py untouched, only adding a filter for the warnings on the top of the file:

pytestmark = pytest.mark.filterwarnings(
    # TODO: Remove in 1.2 (as well as all the tests below)
    "ignore:Function plot_partial_dependence is deprecated",
)

@@ -145,13 +145,13 @@ ICE plots:

Copy link
Member

Choose a reason for hiding this comment

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

l.141, there is an occurence for the function plot_partial_dependence. We should instead refer to the class method .from_estimator

with pytest.warns(FutureWarning):
plot_partial_dependence(clf_diabetes, diabetes.data, [0])


@pytest.mark.filterwarnings("ignore:A Bunch will be returned")
@pytest.mark.parametrize("grid_resolution", [10, 20])
def test_plot_partial_dependence(grid_resolution, pyplot, clf_diabetes, diabetes):
Copy link
Member

Choose a reason for hiding this comment

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

if possible, I would consider to rename the name of the test functions where plot_partial_dependence occur and replace by partial_dependence_display

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.

There is a couple of issues in the documentation. I also commented regarding the different pattern used compared to the other plot_* functions deprecation.

with multiple calls. To plot the the partial dependence for multiple
estimators, please pass the axes created by the first call to the
second call::
:func:`PartialDependenceDisplay.from_estimator` does not support using
Copy link
Member

Choose a reason for hiding this comment

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

I think that we want to revert this part. We still want the documentation to be unchanged.

Copy link
Member

Choose a reason for hiding this comment

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

We only need a .. deprecated:: 1.0 above the note mentioning to use the .from_estimator instead.

@@ -267,181 +271,34 @@ def plot_partial_dependence(
>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from sklearn.inspection import plot_partial_dependence
>>> from sklearn.inspection import PartialDependenceDisplay
Copy link
Member

Choose a reason for hiding this comment

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

Same here. We want to revert. However, could you add .from_estimator in the "See Also" above.

display = PartialDependenceDisplay(
pd_results=pd_results,
features=features,
return PartialDependenceDisplay.from_estimator(
Copy link
Member

Choose a reason for hiding this comment

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

For the other classes, @NicolasHug was in favor to keep the function untouched and instead to have a bit of duplicated code during the deprecation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, updated PR with the function body and the tests duplicated.

@@ -576,7 +433,6 @@ class PartialDependenceDisplay:
See Also
--------
partial_dependence : Compute Partial Dependence values.
Copy link
Member

Choose a reason for hiding this comment

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

I would add as well the .from_estimator method at this level.


Parameters
----------
estimator : BaseEstimator A fitted estimator object implementing
Copy link
Member

Choose a reason for hiding this comment

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

missing a breaking line after BaseEstimator

not (0 <= target_idx < len(estimator.classes_))
or estimator.classes_[target_idx] != target
):
raise ValueError("target not in est.classes_, got {}".format(target))
Copy link
Member

Choose a reason for hiding this comment

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

let use an f-string

try:
fx = feature_names.index(fx)
except ValueError as e:
raise ValueError("Feature %s not in feature_names" % fx) from e
Copy link
Member

Choose a reason for hiding this comment

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

an f-string as well

axes = np.asarray(ax, dtype=object)
if axes.size != len(features):
raise ValueError(
"Expected ax to have {} axes, got {}".format(
Copy link
Member

Choose a reason for hiding this comment

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

f-string

if i >= len(feature_names):
raise ValueError(
"All entries of features must be less than "
"len(feature_names) = {0}, got {1}.".format(len(feature_names), i)
Copy link
Member

Choose a reason for hiding this comment

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

f-string

raise ValueError("target must be specified for multi-output regressors")
if not 0 <= target <= n_tasks:
raise ValueError(
"target must be in [0, n_tasks], got {}.".format(target)
Copy link
Member

Choose a reason for hiding this comment

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

f-string

Comment on lines +76 to +78
... [1, 2]) # doctest: +SKIP
>>> disp2 = plot_partial_dependence(est2, X, [1, 2],
... ax=disp1.axes_)
... ax=disp1.axes_) # doctest: +SKIP
Copy link
Member Author

Choose a reason for hiding this comment

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

Added skips here to avoid the future warning.

Comment on lines +284 to +286
>>> plot_partial_dependence(clf, X, [0, (0, 1)]) # doctest: +SKIP
<...>
>>> plt.show()
>>> plt.show() # doctest: +SKIP
Copy link
Member Author

Choose a reason for hiding this comment

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

Added skips here to avoid the future warning.

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.

Only nitpicks. LGTM

Comment on lines 641 to 642
"""Partial dependence (PD) and individual conditional expectation (ICE)
plots.
Copy link
Member

Choose a reason for hiding this comment

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

Can we succeed to make it on a single line (for future numpydoc validation) ;)

:term:`predict_proba`, or :term:`decision_function`.
Multioutput-multiclass classifiers are not supported.

X : {array-like or dataframe} of shape (n_samples, n_features)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
X : {array-like or dataframe} of shape (n_samples, n_features)
X : {array-like, dataframe} of shape (n_samples, n_features)

Comment on lines 798 to 799

.. versionadded:: 1.0
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 1.0

Comment on lines 806 to 807

.. versionadded:: 1.0
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 1.0

Comment on lines 822 to 823

.. versionadded:: 0.22
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 0.22

Comment on lines 835 to 836

.. versionadded:: 0.24
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 0.24

Comment on lines 846 to 847

.. versionadded:: 0.24
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 0.24

Comment on lines 853 to 854

.. versionadded:: 0.24
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.. versionadded:: 0.24

@glemaitre
Copy link
Member

pinging @adrinjalali and tagging #20965

This one would be great to be included. All the current displays would have the same API and the deprecation will be announced. However, we need a second review :)

@thomasjpfan
Copy link
Member Author

For reviewers, this PR looks huge, but it's mostly a copy and paste of the tests and the implementation.

@adrinjalali
Copy link
Member

This is over 1.2k lines of code/change. I'm not sure if it's feasible to have it for the release. I'd like to treat this as a non-blocker, but happy to include it if it's merged by the time of the release.

@thomasjpfan
Copy link
Member Author

@glemaitre In trying to make this PR smaller, I defined a private _plot_partial_dependence and have plot_partial_dependence and from_estimator call into it. The test now use parameterize fixtures to test both forms.

Now most of the diff is the docstring of from_estimator in sklearn/inspection/_plot/partial_dependence.py.

In the future, we can do another refactor to make this nicer.

@glemaitre
Copy link
Member

glemaitre commented Sep 8, 2021 via email

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Thanks LGTM. It's 433 LoC now. So should we merge it with the changelog in 1.0?

@adrinjalali
Copy link
Member

Merging this, and will include in the release.

@adrinjalali adrinjalali merged commit 057b82e into scikit-learn:main Sep 9, 2021
@glemaitre
Copy link
Member

Thanks @adrinjalali

adrinjalali pushed a commit to adrinjalali/scikit-learn that referenced this pull request Sep 10, 2021
* API Deprecates plot_partial_dependence

* DOC Adds whats new

* DOC Fixes doc build errors

* CLN Removes feature warning

* CLN Address comments

* DOC Fix docstrings

* TST Skip future warning tests

* DOC Adjust docstrings

* REV Reduce diff

* DOC Adds docstring
adrinjalali pushed a commit that referenced this pull request Sep 14, 2021
* API Deprecates plot_partial_dependence

* DOC Adds whats new

* DOC Fixes doc build errors

* CLN Removes feature warning

* CLN Address comments

* DOC Fix docstrings

* TST Skip future warning tests

* DOC Adjust docstrings

* REV Reduce diff

* DOC Adds docstring
samronsin pushed a commit to samronsin/scikit-learn that referenced this pull request Nov 30, 2021
* API Deprecates plot_partial_dependence

* DOC Adds whats new

* DOC Fixes doc build errors

* CLN Removes feature warning

* CLN Address comments

* DOC Fix docstrings

* TST Skip future warning tests

* DOC Adjust docstrings

* REV Reduce diff

* DOC Adds docstring
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.

None yet

4 participants