Skip to content

Commit 057b82e

Browse files
authored
API Deprecates plot_partial_dependence (scikit-learn#20959)
* 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
1 parent 82f0ab9 commit 057b82e

File tree

10 files changed

+433
-57
lines changed

10 files changed

+433
-57
lines changed

doc/developers/plotting.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ Plotting with Multiple Axes
6464
---------------------------
6565

6666
Some of the plotting tools like
67-
:func:`~sklearn.inspection.plot_partial_dependence` and
68-
:class:`~sklearn.inspection.PartialDependenceDisplay` support plottong on
67+
:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` and
68+
:class:`~sklearn.inspection.PartialDependenceDisplay` support plotting on
6969
multiple axes. Two different scenarios are supported:
7070

7171
1. If a list of axes is passed in, `plot` will check if the number of axes is
@@ -87,8 +87,8 @@ be placed. In this case, we suggest using matplotlib's
8787
By default, the `ax` keyword in `plot` is `None`. In this case, the single
8888
axes is created and the gridspec api is used to create the regions to plot in.
8989

90-
See for example, :func:`~sklearn.inspection.plot_partial_dependence` which
91-
plots multiple lines and contours using this API. The axes defining the
90+
See for example, :func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator
91+
which plots multiple lines and contours using this API. The axes defining the
9292
bounding box is saved in a `bounding_ax_` attribute. The individual axes
9393
created are stored in an `axes_` ndarray, corresponding to the axes position on
9494
the grid. Positions that are not used are set to `None`. Furthermore, the

doc/modules/partial_dependence.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ independent of the house age, whereas for values less than 2 there is a strong
5555
dependence on age.
5656

5757
The :mod:`sklearn.inspection` module provides a convenience function
58-
:func:`plot_partial_dependence` to create one-way and two-way partial
58+
:func:`~PartialDependenceDisplay.from_estimator` to create one-way and two-way partial
5959
dependence plots. In the below example we show how to create a grid of
6060
partial dependence plots: two one-way PDPs for the features ``0`` and ``1``
6161
and a two-way PDP between the two features::
6262

6363
>>> from sklearn.datasets import make_hastie_10_2
6464
>>> from sklearn.ensemble import GradientBoostingClassifier
65-
>>> from sklearn.inspection import plot_partial_dependence
65+
>>> from sklearn.inspection import PartialDependenceDisplay
6666

6767
>>> X, y = make_hastie_10_2(random_state=0)
6868
>>> clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
6969
... max_depth=1, random_state=0).fit(X, y)
7070
>>> features = [0, 1, (0, 1)]
71-
>>> plot_partial_dependence(clf, X, features)
71+
>>> PartialDependenceDisplay.from_estimator(clf, X, features)
7272
<...>
7373

7474
You can access the newly created figure and Axes objects using ``plt.gcf()``
@@ -82,7 +82,7 @@ the PDPs should be created via the ``target`` argument::
8282
>>> mc_clf = GradientBoostingClassifier(n_estimators=10,
8383
... max_depth=1).fit(iris.data, iris.target)
8484
>>> features = [3, 2, (3, 2)]
85-
>>> plot_partial_dependence(mc_clf, X, features, target=0)
85+
>>> PartialDependenceDisplay.from_estimator(mc_clf, X, features, target=0)
8686
<...>
8787

8888
The same parameter ``target`` is used to specify the target in multi-output
@@ -138,20 +138,20 @@ and the house price in the PD line. However, the ICE lines show that there
138138
are some exceptions, where the house price remains constant in some ranges of
139139
the median income.
140140

141-
The :mod:`sklearn.inspection` module's :func:`plot_partial_dependence`
141+
The :mod:`sklearn.inspection` module's :meth:`PartialDependenceDisplay.from_estimator`
142142
convenience function can be used to create ICE plots by setting
143143
``kind='individual'``. In the example below, we show how to create a grid of
144144
ICE plots:
145145

146146
>>> from sklearn.datasets import make_hastie_10_2
147147
>>> from sklearn.ensemble import GradientBoostingClassifier
148-
>>> from sklearn.inspection import plot_partial_dependence
148+
>>> from sklearn.inspection import PartialDependenceDisplay
149149

150150
>>> X, y = make_hastie_10_2(random_state=0)
151151
>>> clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
152152
... max_depth=1, random_state=0).fit(X, y)
153153
>>> features = [0, 1]
154-
>>> plot_partial_dependence(clf, X, features,
154+
>>> PartialDependenceDisplay.from_estimator(clf, X, features,
155155
... kind='individual')
156156
<...>
157157

@@ -160,7 +160,7 @@ feature of interest. Hence, it is recommended to use ICE plots alongside
160160
PDPs. They can be plotted together with
161161
``kind='both'``.
162162

163-
>>> plot_partial_dependence(clf, X, features,
163+
>>> PartialDependenceDisplay.from_estimator(clf, X, features,
164164
... kind='both')
165165
<...>
166166

doc/whats_new/v1.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ Changelog
471471
:func:`~sklearn.inspection.permutation_importance`.
472472
:pr:`19411` by :user:`Simona Maggio <simonamaggio>`.
473473

474+
- |API| :class:`inspection.PartialDependenceDisplay` exposes a class method:
475+
:func:`~inspection.PartialDependenceDisplay.from_estimator`.
476+
:func:`inspection.plot_partial_dependence` is deprecated in favor of the
477+
class method and will be removed in 1.2. :pr:`20959` by `Thomas Fan`_.
478+
474479
:mod:`sklearn.kernel_approximation`
475480
...................................
476481

examples/ensemble/plot_monotonic_constraints.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<https://xgboost.readthedocs.io/en/latest/tutorials/monotonic.html>`_.
2020
"""
2121
from sklearn.ensemble import HistGradientBoostingRegressor
22-
from sklearn.inspection import plot_partial_dependence
22+
from sklearn.inspection import PartialDependenceDisplay
2323
import numpy as np
2424
import matplotlib.pyplot as plt
2525

@@ -43,7 +43,7 @@
4343
# Without any constraint
4444
gbdt = HistGradientBoostingRegressor()
4545
gbdt.fit(X, y)
46-
disp = plot_partial_dependence(
46+
disp = PartialDependenceDisplay.from_estimator(
4747
gbdt,
4848
X,
4949
features=[0, 1],
@@ -55,7 +55,7 @@
5555
gbdt = HistGradientBoostingRegressor(monotonic_cst=[1, -1])
5656
gbdt.fit(X, y)
5757

58-
plot_partial_dependence(
58+
PartialDependenceDisplay.from_estimator(
5959
gbdt,
6060
X,
6161
features=[0, 1],

examples/inspection/plot_partial_dependence.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@
111111

112112
import matplotlib.pyplot as plt
113113
from sklearn.inspection import partial_dependence
114-
from sklearn.inspection import plot_partial_dependence
114+
from sklearn.inspection import PartialDependenceDisplay
115115

116116
print("Computing partial dependence plots...")
117117
tic = time()
118118
features = ["MedInc", "AveOccup", "HouseAge", "AveRooms"]
119-
display = plot_partial_dependence(
119+
display = PartialDependenceDisplay.from_estimator(
120120
est,
121121
X_train,
122122
features,
@@ -166,7 +166,7 @@
166166

167167
print("Computing partial dependence plots...")
168168
tic = time()
169-
display = plot_partial_dependence(
169+
display = PartialDependenceDisplay.from_estimator(
170170
est,
171171
X_train,
172172
features,
@@ -229,7 +229,7 @@
229229
print("Computing partial dependence plots...")
230230
tic = time()
231231
_, ax = plt.subplots(ncols=3, figsize=(9, 4))
232-
display = plot_partial_dependence(
232+
display = PartialDependenceDisplay.from_estimator(
233233
est,
234234
X_train,
235235
features,

examples/miscellaneous/plot_partial_dependence_visualization_api.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sklearn.preprocessing import StandardScaler
2323
from sklearn.pipeline import make_pipeline
2424
from sklearn.tree import DecisionTreeRegressor
25-
from sklearn.inspection import plot_partial_dependence
25+
from sklearn.inspection import PartialDependenceDisplay
2626

2727

2828
# %%
@@ -49,22 +49,22 @@
4949
#
5050
# We plot partial dependence curves for features "age" and "bmi" (body mass
5151
# index) for the decision tree. With two features,
52-
# :func:`~sklearn.inspection.plot_partial_dependence` expects to plot two
53-
# curves. Here the plot function place a grid of two plots using the space
52+
# :func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` expects to plot
53+
# two curves. Here the plot function place a grid of two plots using the space
5454
# defined by `ax` .
5555
fig, ax = plt.subplots(figsize=(12, 6))
5656
ax.set_title("Decision Tree")
57-
tree_disp = plot_partial_dependence(tree, X, ["age", "bmi"], ax=ax)
57+
tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age", "bmi"], ax=ax)
5858

5959
# %%
6060
# The partial depdendence curves can be plotted for the multi-layer perceptron.
6161
# In this case, `line_kw` is passed to
62-
# :func:`~sklearn.inspection.plot_partial_dependence` to change the color of
63-
# the curve.
62+
# :func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` to change the
63+
# color of the curve.
6464
fig, ax = plt.subplots(figsize=(12, 6))
6565
ax.set_title("Multi-layer Perceptron")
66-
mlp_disp = plot_partial_dependence(mlp, X, ["age", "bmi"], ax=ax,
67-
line_kw={"color": "red"})
66+
mlp_disp = PartialDependenceDisplay.from_estimator(mlp, X, ["age", "bmi"], ax=ax,
67+
line_kw={"color": "red"})
6868

6969
# %%
7070
# Plotting partial dependence of the two models together
@@ -129,7 +129,6 @@
129129
# Here, we plot the partial dependence curves for a single feature, "age", on
130130
# the same axes. In this case, `tree_disp.axes_` is passed into the second
131131
# plot function.
132-
tree_disp = plot_partial_dependence(tree, X, ["age"])
133-
mlp_disp = plot_partial_dependence(mlp, X, ["age"],
134-
ax=tree_disp.axes_,
135-
line_kw={"color": "red"})
132+
tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age"])
133+
mlp_disp = PartialDependenceDisplay.from_estimator(
134+
mlp, X, ["age"], ax=tree_disp.axes_, line_kw={"color": "red"})

sklearn/inspection/_partial_dependence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def partial_dependence(
370370
371371
See Also
372372
--------
373-
plot_partial_dependence : Plot Partial Dependence.
373+
PartialDependenceDisplay.from_estimator : Plot Partial Dependence.
374374
PartialDependenceDisplay : Partial Dependence visualization.
375375
376376
Examples

0 commit comments

Comments
 (0)