From f8ca2fd22eadf610f087d4440c405f8d4656dcde Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Wed, 10 Feb 2021 14:36:59 +0100 Subject: [PATCH 01/20] Add parameters to format ICE and PD lines separately in partial dependence plot --- .../inspection/_plot/partial_dependence.py | 78 +++++++++++++++---- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index d6604d7ae675f..aa69ed018e771 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -33,6 +33,8 @@ def plot_partial_dependence( n_jobs=None, verbose=0, line_kw=None, + ice_lines_kw=None, + pd_line_kw=None, contour_kw=None, ax=None, kind="average", @@ -185,6 +187,16 @@ def plot_partial_dependence( Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. For one-way partial dependence plots. + ice_lines_kw : dict, default=None + Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + For ICE lines in the one-way partial dependence plots. + Takes priority over ```line_kw`` when not ``None``. + + pd_line_kw : dict, default=None + Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + For partial dependence in one-way partial dependence plots. + Takes priority over ```line_kw`` when not ``None``. + contour_kw : dict, default=None Dict with keywords passed to the ``matplotlib.pyplot.contourf`` call. For two-way partial dependence plots. @@ -399,7 +411,12 @@ def convert_feature(fx): random_state=random_state, ) return display.plot( - ax=ax, n_cols=n_cols, line_kw=line_kw, contour_kw=contour_kw + ax=ax, + n_cols=n_cols, + line_kw=line_kw, + ice_lines_kw=ice_lines_kw, + pd_line_kw=pd_line_kw, + contour_kw=contour_kw, ) @@ -655,8 +672,8 @@ def _plot_one_way_partial_dependence( n_cols, pd_plot_idx, n_lines, - individual_line_kw, - line_kw, + ice_lines_kw, + pd_line_kw, ): """Plot 1-way partial dependence: ICE and PDP. @@ -684,9 +701,9 @@ def _plot_one_way_partial_dependence( matching 2D position in the grid layout. n_lines : int The total number of lines expected to be plot on the axis. - individual_line_kw : dict + ice_lines_kw : dict Dict with keywords passed when plotting the ICE lines. - line_kw : dict + pd_line_kw : dict Dict with keywords passed when plotting the PD plot. """ from matplotlib import transforms # noqa @@ -699,7 +716,7 @@ def _plot_one_way_partial_dependence( ax, pd_plot_idx, n_lines, - individual_line_kw, + ice_lines_kw, ) if self.kind in ("average", "both"): @@ -713,7 +730,7 @@ def _plot_one_way_partial_dependence( feature_values, ax, pd_line_idx, - line_kw, + pd_line_kw, ) trans = transforms.blended_transform_factory( @@ -741,7 +758,7 @@ def _plot_one_way_partial_dependence( else: ax.set_yticklabels([]) - if line_kw.get("label", None) and self.kind != 'individual': + if pd_line_kw.get("label", None) and self.kind != 'individual': ax.legend() def _plot_two_way_partial_dependence( @@ -818,7 +835,16 @@ def _plot_two_way_partial_dependence( ax.set_ylabel(self.feature_names[feature_idx[1]]) @_deprecate_positional_args(version="1.1") - def plot(self, *, ax=None, n_cols=3, line_kw=None, contour_kw=None): + def plot( + self, + *, + ax=None, + n_cols=3, + line_kw=None, + ice_lines_kw=None, + pd_line_kw=None, + contour_kw=None, + ): """Plot partial dependence plots. Parameters @@ -841,6 +867,16 @@ def plot(self, *, ax=None, n_cols=3, line_kw=None, contour_kw=None): Dict with keywords passed to the `matplotlib.pyplot.plot` call. For one-way partial dependence plots. + ice_lines_kw : dict, default=None + Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + For ICE lines in the one-way partial dependence plots. + Takes priority over ```line_kw`` when not ``None``. + + pd_line_kw : dict, default=None + Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + For partial dependence in one-way partial dependence plots. + Takes priority over ```line_kw`` when not ``None``. + contour_kw : dict, default=None Dict with keywords passed to the `matplotlib.pyplot.contourf` call for two-way partial dependence plots. @@ -869,14 +905,22 @@ def plot(self, *, ax=None, n_cols=3, line_kw=None, contour_kw=None): "color": "C0", "label": "average" if self.kind == "both" else None, } - line_kw = {**default_line_kws, **line_kw} - individual_line_kw = line_kw.copy() - del individual_line_kw["label"] + if ice_lines_kw is None: + ice_lines_kw = {**default_line_kws, **line_kw} - if self.kind == 'individual' or self.kind == 'both': - individual_line_kw['alpha'] = 0.3 - individual_line_kw['linewidth'] = 0.5 + if self.kind == 'individual' or self.kind == 'both': + ice_lines_kw['alpha'] = 0.3 + ice_lines_kw['linewidth'] = 0.5 + else: + ice_lines_kw = {**default_line_kws, **ice_lines_kw} + + del ice_lines_kw["label"] + + if pd_line_kw is None: + pd_line_kw = {**default_line_kws, **line_kw} + else: + pd_line_kw = {**default_line_kws, **pd_line_kw} n_features = len(self.features) if self.kind in ("individual", "both"): @@ -972,8 +1016,8 @@ def plot(self, *, ax=None, n_cols=3, line_kw=None, contour_kw=None): n_cols, pd_plot_idx, n_lines, - individual_line_kw, - line_kw, + ice_lines_kw, + pd_line_kw, ) else: self._plot_two_way_partial_dependence( From 3b71e0d5234e0d29aeb6a4ade6c9c6c2a5a567f1 Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Wed, 10 Feb 2021 18:53:08 +0100 Subject: [PATCH 02/20] Add warning when line_kw is used in conjunction with pd_line_kw or ice_lines_kw --- sklearn/inspection/_plot/partial_dependence.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index aa69ed018e771..0fcbe8f284d49 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -1,6 +1,7 @@ import numbers from itertools import chain from math import ceil +import warnings import numpy as np from scipy import sparse @@ -895,6 +896,17 @@ def plot( if contour_kw is None: contour_kw = {} + if line_kw is not None and ice_lines_kw is not None: + warnings.warn( + "Both line_kw and ice_lines_kw are specified. ice_lines_kw " + "will take priority." + ) + if line_kw is not None and pd_line_kw is not None: + warnings.warn( + "Both line_kw and pd_line_kw are specified. pd_line_kw will " + "take priority." + ) + if ax is None: _, ax = plt.subplots() From e35187dc92fdb18b51da561530f332a908f93f4d Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Wed, 10 Feb 2021 18:53:48 +0100 Subject: [PATCH 03/20] Add test to check line formatting behavior in partial dependence plot --- .../tests/test_plot_partial_dependence.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index 6ec0fde9775af..ef939eaeb901a 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -588,3 +588,44 @@ def test_partial_dependence_overwrite_labels( legend_text = ax.get_legend().get_texts() assert len(legend_text) == 1 assert legend_text[0].get_text() == label + + +@pytest.mark.filterwarnings("ignore:A Bunch will be returned") +@pytest.mark.parametrize( + "line_kw, pd_line_kw, ice_lines_kw, expected_colors", + [ + ({"color": "r"}, {"color": "g"}, {"color": "b"}, ("g", "b")), + (None, {"color": "g"}, {"color": "b"}, ("g", "b")), + ({"color": "r"}, None, {"color": "b"}, ("r", "b")), + ({"color": "r"}, {"color": "g"}, None, ("g", "r")), + ({"color": "r"}, None, None, ("r", "r")), + ] +) +def test_plot_partial_dependence_lines_kw( + pyplot, + clf_diabetes, + diabetes, + line_kw, + pd_line_kw, + ice_lines_kw, + expected_colors, +): + + disp = plot_partial_dependence( + clf_diabetes, + diabetes.data, + [0, 2], + grid_resolution=20, + feature_names=diabetes.feature_names, + n_cols=2, + kind="both", + line_kw=line_kw, + pd_line_kw=pd_line_kw, + ice_lines_kw=ice_lines_kw, + ) + + line = disp.lines_[0, 0, -1] + assert line.get_color() == expected_colors[0] + + line = disp.lines_[0, 0, 0] + assert line.get_color() == expected_colors[1] From 69c33febc1f9e8408de57877cc4e5638d01d673b Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Wed, 10 Feb 2021 18:54:10 +0100 Subject: [PATCH 04/20] Add PR info to whats_new --- doc/whats_new/v1.0.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index d073ffd80bdf7..108969941960e 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -180,6 +180,14 @@ Changelog :class:`~sklearn.semi_supervised.LabelPropagation`. :pr:`19271` by :user:`Zhaowei Wang `. +:mod:`sklearn.inspection` +......................... + +- |Enhancement| Add kwargs to format ICE and PD lines separately in partial + dependence plots :func:`~sklearn.inspection.plot_partial_dependence` and + :meth:`~sklearn.inspection.PartialDependenceDisplay.plot`. + :pr:`19428` by :user:`Mehdi Hamoumi ` + Code and Documentation Contributors ----------------------------------- From 6a21afae1060e1bb12d8e3d8dd2b53a741def0fb Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Thu, 11 Feb 2021 11:01:46 +0100 Subject: [PATCH 05/20] Add call to action to silence line_kw warning Co-authored-by: Guillaume Lemaitre --- sklearn/inspection/_plot/partial_dependence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index 0fcbe8f284d49..f57377bf6e1b1 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -899,12 +899,12 @@ def plot( if line_kw is not None and ice_lines_kw is not None: warnings.warn( "Both line_kw and ice_lines_kw are specified. ice_lines_kw " - "will take priority." + "will take priority. Do not pass line_kw to silence this warning." ) if line_kw is not None and pd_line_kw is not None: warnings.warn( "Both line_kw and pd_line_kw are specified. pd_line_kw will " - "take priority." + "take priority. Do not pass line_kw to silence this warning." ) if ax is None: From b2b5d742de8ae88c5605006390d15e16ee25f141 Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Thu, 11 Feb 2021 11:02:40 +0100 Subject: [PATCH 06/20] Add comment to explain what test does Co-authored-by: Guillaume Lemaitre --- sklearn/inspection/_plot/tests/test_plot_partial_dependence.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index ef939eaeb901a..a422a69970dde 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -610,6 +610,8 @@ def test_plot_partial_dependence_lines_kw( ice_lines_kw, expected_colors, ): + # check that passing pd_line_kw and ice_lines_kw will act on the + # specific lines in the plot disp = plot_partial_dependence( clf_diabetes, From 41ede4df412892737d6b5b7d7ddf95f46d6cec11 Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Thu, 11 Feb 2021 11:56:22 +0100 Subject: [PATCH 07/20] Add partial dependence line kwargs to documentation --- examples/inspection/plot_partial_dependence.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/inspection/plot_partial_dependence.py b/examples/inspection/plot_partial_dependence.py index 927857d845f9e..1ee3859acac91 100644 --- a/examples/inspection/plot_partial_dependence.py +++ b/examples/inspection/plot_partial_dependence.py @@ -117,8 +117,16 @@ tic = time() features = ['MedInc', 'AveOccup', 'HouseAge', 'AveRooms'] display = plot_partial_dependence( - est, X_train, features, kind="both", subsample=50, - n_jobs=3, grid_resolution=20, random_state=0 + est, + X_train, + features, + kind="both", + subsample=50, + n_jobs=3, + grid_resolution=20, + random_state=0, + ice_lines_kw={"color": "b", "alpha": 0.3, "linewidth": 0.5}, + pd_line_kw={"color": "r"}, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( From 281ad2424bee2eff35f92efeb2a3157820a1ebc1 Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Thu, 11 Feb 2021 12:31:24 +0100 Subject: [PATCH 08/20] Add line_kw ice_lines_kw pd_line_kw warnings test --- .../tests/test_plot_partial_dependence.py | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index a422a69970dde..650e0fbab65b1 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -610,8 +610,10 @@ def test_plot_partial_dependence_lines_kw( ice_lines_kw, expected_colors, ): - # check that passing pd_line_kw and ice_lines_kw will act on the - # specific lines in the plot + """ + check that passing pd_line_kw and ice_lines_kw will act on the + specific lines in the plot + """ disp = plot_partial_dependence( clf_diabetes, @@ -631,3 +633,40 @@ def test_plot_partial_dependence_lines_kw( line = disp.lines_[0, 0, 0] assert line.get_color() == expected_colors[1] + + +def test_plot_partial_dependence_lines_kw_warnings( + pyplot, + clf_diabetes, + diabetes, +): + """ + check that passing line_kw along with pd_line_kw and ice_lines_kw + raises warnings + """ + with pytest.warns(Warning, match=r"^Both line_kw and") as record: + plot_partial_dependence( + clf_diabetes, + diabetes.data, + [0, 2], + grid_resolution=20, + feature_names=diabetes.feature_names, + n_cols=2, + kind="both", + line_kw={"color": "r"}, + pd_line_kw={"color": "g"}, + ice_lines_kw={"color": "b"}, + ) + + warnings_messages = {rcd.message.args[0] for rcd in record} + ice_lines_kw_msg = ( + "Both line_kw and ice_lines_kw are specified. ice_lines_kw " + "will take priority. Do not pass line_kw to silence this warning." + ) + pd_line_kw_msg = ( + "Both line_kw and pd_line_kw are specified. pd_line_kw " + "will take priority. Do not pass line_kw to silence this warning." + ) + + assert ice_lines_kw_msg in warnings_messages + assert pd_line_kw_msg in warnings_messages From 8ff41834e34b6b0ebd3e42b331618e45e2685fea Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Thu, 11 Feb 2021 12:34:26 +0100 Subject: [PATCH 09/20] Formatting + pytest check UserWarning instead of Warning --- sklearn/inspection/_plot/partial_dependence.py | 3 ++- sklearn/inspection/_plot/tests/test_plot_partial_dependence.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index f57377bf6e1b1..be8e9a169dafd 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -899,7 +899,8 @@ def plot( if line_kw is not None and ice_lines_kw is not None: warnings.warn( "Both line_kw and ice_lines_kw are specified. ice_lines_kw " - "will take priority. Do not pass line_kw to silence this warning." + "will take priority. Do not pass line_kw to silence this " + "warning." ) if line_kw is not None and pd_line_kw is not None: warnings.warn( diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index 650e0fbab65b1..ac685d9cf119f 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -644,7 +644,7 @@ def test_plot_partial_dependence_lines_kw_warnings( check that passing line_kw along with pd_line_kw and ice_lines_kw raises warnings """ - with pytest.warns(Warning, match=r"^Both line_kw and") as record: + with pytest.warns(UserWarning, match=r"^Both line_kw and") as record: plot_partial_dependence( clf_diabetes, diabetes.data, From c01307de27f211f84bf48aa1f4d4b992208e26aa Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Wed, 7 Apr 2021 10:52:46 +0200 Subject: [PATCH 10/20] Apply suggestions from code review: versionadded Co-authored-by: Chiara Marmo --- sklearn/inspection/_plot/partial_dependence.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index be8e9a169dafd..5ab26d5fe88e1 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -193,11 +193,13 @@ def plot_partial_dependence( For ICE lines in the one-way partial dependence plots. Takes priority over ```line_kw`` when not ``None``. + .. versionadded:: 1.0 pd_line_kw : dict, default=None Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. For partial dependence in one-way partial dependence plots. Takes priority over ```line_kw`` when not ``None``. + .. versionadded:: 1.0 contour_kw : dict, default=None Dict with keywords passed to the ``matplotlib.pyplot.contourf`` call. For two-way partial dependence plots. @@ -873,11 +875,13 @@ def plot( For ICE lines in the one-way partial dependence plots. Takes priority over ```line_kw`` when not ``None``. + .. versionadded:: 1.0 pd_line_kw : dict, default=None Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. For partial dependence in one-way partial dependence plots. Takes priority over ```line_kw`` when not ``None``. + .. versionadded:: 1.0 contour_kw : dict, default=None Dict with keywords passed to the `matplotlib.pyplot.contourf` call for two-way partial dependence plots. From 6c2d85f4573d5cd7da9a4324c0342da57017c093 Mon Sep 17 00:00:00 2001 From: Mehdi Hamoumi Date: Wed, 7 Apr 2021 11:00:05 +0200 Subject: [PATCH 11/20] Add ICE and PDP line formatting to example --- examples/inspection/plot_partial_dependence.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/inspection/plot_partial_dependence.py b/examples/inspection/plot_partial_dependence.py index 1ee3859acac91..73c39c360c0dc 100644 --- a/examples/inspection/plot_partial_dependence.py +++ b/examples/inspection/plot_partial_dependence.py @@ -168,8 +168,16 @@ print('Computing partial dependence plots...') tic = time() display = plot_partial_dependence( - est, X_train, features, kind="both", subsample=50, - n_jobs=3, grid_resolution=20, random_state=0 + est, + X_train, + features, + kind="both", + subsample=50, + n_jobs=3, + grid_resolution=20, + random_state=0, + ice_lines_kw={"color": "b", "alpha": 0.3, "linewidth": 0.5}, + pd_line_kw={"color": "r"}, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( From 438b68519e00caa55629f07ff519039da1bb86dd Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Tue, 29 Jun 2021 18:15:47 +0200 Subject: [PATCH 12/20] fix whats new --- doc/whats_new/v1.0.rst | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index c477848f73a35..a4811c8f04e71 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -314,6 +314,11 @@ Changelog :func:`~sklearn.inspection.permutation_importance`. :pr:`19411` by :user:`Simona Maggio `. +- |Enhancement| Add kwargs to format ICE and PD lines separately in partial + dependence plots :func:`~sklearn.inspection.plot_partial_dependence` and + :meth:`~sklearn.inspection.PartialDependenceDisplay.plot`. + :pr:`19428` by :user:`Mehdi Hamoumi `. + :mod:`sklearn.linear_model` ........................... @@ -456,7 +461,7 @@ Changelog :user:`Alonso Silva Allende `. :mod:`sklearn.mixture` -.............................. +...................... - |Fix| Ensure that the best parameters are set appropriately in the case of divergency for :class:`mixture.GaussianMixture` and @@ -492,7 +497,7 @@ Changelog :mod:`sklearn.neighbors` -.......................... +........................ - |Enhancement| The creation of :class:`neighbors.KDTree` and :class:`neighbors.BallTree` has been improved for their worst-cases time @@ -571,9 +576,6 @@ Changelog for non-English characters. :pr:`18959` by :user:`Zero ` and :user:`wstates `. -:mod:`sklearn.inspection` -......................... - - |Fix| Fixed a bug in :class:`tree.DecisionTreeClassifier`, :class:`tree.DecisionTreeRegressor` where a node could be split whereas it should not have been due to incorrect handling of rounding errors. @@ -585,11 +587,6 @@ Changelog and will be removed in 1.2. :pr:`20272` by :user:`Jérémie du Boisberranger `. -- |Enhancement| Add kwargs to format ICE and PD lines separately in partial - dependence plots :func:`~sklearn.inspection.plot_partial_dependence` and - :meth:`~sklearn.inspection.PartialDependenceDisplay.plot`. - :pr:`19428` by :user:`Mehdi Hamoumi `. - :mod:`sklearn.utils` .................... From 91f7acf036d7e01916bcf63f8d28d62ab16a17cc Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 9 Jul 2021 18:23:24 +0200 Subject: [PATCH 13/20] Apply suggestions from code review --- .../inspection/plot_partial_dependence.py | 8 +++--- .../inspection/_plot/partial_dependence.py | 28 +++++++++++-------- .../tests/test_plot_partial_dependence.py | 16 +++++------ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/examples/inspection/plot_partial_dependence.py b/examples/inspection/plot_partial_dependence.py index 4b0cb854f1a32..430a3ad34ff2e 100644 --- a/examples/inspection/plot_partial_dependence.py +++ b/examples/inspection/plot_partial_dependence.py @@ -125,8 +125,8 @@ n_jobs=3, grid_resolution=20, random_state=0, - ice_lines_kw={"color": "b", "alpha": 0.3, "linewidth": 0.5}, - pd_line_kw={"color": "r"}, + ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5}, + pd_line_kw={"color": "tab:orange", "linestyle": "--"}, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( @@ -175,8 +175,8 @@ n_jobs=3, grid_resolution=20, random_state=0, - ice_lines_kw={"color": "b", "alpha": 0.3, "linewidth": 0.5}, - pd_line_kw={"color": "r"}, + ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5}, + pd_line_kw={"color": "tab:orange", "linestyle": "--"}, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index ad247c7cd9d6c..007b63d747adc 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -191,17 +191,19 @@ def plot_partial_dependence( For one-way partial dependence plots. ice_lines_kw : dict, default=None - Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For ICE lines in the one-way partial dependence plots. - Takes priority over ```line_kw`` when not ``None``. + Takes priority over `line_kw` when not `None`. .. versionadded:: 1.0 + pd_line_kw : dict, default=None - Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For partial dependence in one-way partial dependence plots. - Takes priority over ```line_kw`` when not ``None``. + Takes priority over `line_kw` when not `None`. .. versionadded:: 1.0 + contour_kw : dict, default=None Dict with keywords passed to the ``matplotlib.pyplot.contourf`` call. For two-way partial dependence plots. @@ -897,17 +899,19 @@ def plot( For one-way partial dependence plots. ice_lines_kw : dict, default=None - Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For ICE lines in the one-way partial dependence plots. - Takes priority over ```line_kw`` when not ``None``. + Takes priority over `line_kw` when not `None`. .. versionadded:: 1.0 + pd_line_kw : dict, default=None - Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. + Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For partial dependence in one-way partial dependence plots. - Takes priority over ```line_kw`` when not ``None``. + Takes priority over `line_kw` when not `None`. .. versionadded:: 1.0 + contour_kw : dict, default=None Dict with keywords passed to the `matplotlib.pyplot.contourf` call for two-way partial dependence plots. @@ -928,14 +932,14 @@ def plot( if line_kw is not None and ice_lines_kw is not None: warnings.warn( - "Both line_kw and ice_lines_kw are specified. ice_lines_kw " - "will take priority. Do not pass line_kw to silence this " + "Both `line_kw` and `ice_lines_kw` are specified. `ice_lines_kw` " + "will take priority. Do not pass `line_kw` to silence this " "warning." ) if line_kw is not None and pd_line_kw is not None: warnings.warn( - "Both line_kw and pd_line_kw are specified. pd_line_kw will " - "take priority. Do not pass line_kw to silence this warning." + "Both `line_kw` and `pd_line_kw` are specified. `pd_line_kw` will " + "take priority. Do not pass `line_kw` to silence this warning." ) if ax is None: diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index 277e37d050f88..7ee35d9b9d83c 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -710,8 +710,8 @@ def test_plot_partial_dependence_lines_kw( expected_colors, ): """ - check that passing pd_line_kw and ice_lines_kw will act on the - specific lines in the plot + Check that passing `pd_line_kw` and `ice_lines_kw` will act on the + specific lines in the plot. """ disp = plot_partial_dependence( @@ -740,8 +740,8 @@ def test_plot_partial_dependence_lines_kw_warnings( diabetes, ): """ - check that passing line_kw along with pd_line_kw and ice_lines_kw - raises warnings + Check that passing `line_kw` along with `pd_line_kw` and `ice_lines_kw` + raises warnings. """ with pytest.warns(UserWarning, match=r"^Both line_kw and") as record: plot_partial_dependence( @@ -759,12 +759,12 @@ def test_plot_partial_dependence_lines_kw_warnings( warnings_messages = {rcd.message.args[0] for rcd in record} ice_lines_kw_msg = ( - "Both line_kw and ice_lines_kw are specified. ice_lines_kw " - "will take priority. Do not pass line_kw to silence this warning." + "Both `line_kw` and `ice_lines_kw` are specified. `ice_lines_kw` " + "will take priority. Do not pass `line_kw` to silence this warning." ) pd_line_kw_msg = ( - "Both line_kw and pd_line_kw are specified. pd_line_kw " - "will take priority. Do not pass line_kw to silence this warning." + "Both `line_kw` and `pd_line_kw` are specified. `pd_line_kw` " + "will take priority. Do not pass `line_kw` to silence this warning." ) assert ice_lines_kw_msg in warnings_messages From bca471934026664c26810283eaeed39289e69c2e Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 9 Jul 2021 18:24:49 +0200 Subject: [PATCH 14/20] black --- .../inspection/plot_partial_dependence.py | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/examples/inspection/plot_partial_dependence.py b/examples/inspection/plot_partial_dependence.py index 430a3ad34ff2e..53b6074e578a6 100644 --- a/examples/inspection/plot_partial_dependence.py +++ b/examples/inspection/plot_partial_dependence.py @@ -53,9 +53,7 @@ y -= y.mean() -X_train, X_test, y_train, y_test = train_test_split( - X, y, test_size=0.1, random_state=0 -) +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0) # %% # 1-way partial dependence with different models @@ -80,10 +78,12 @@ print("Training MLPRegressor...") tic = time() -est = make_pipeline(QuantileTransformer(), - MLPRegressor(hidden_layer_sizes=(50, 50), - learning_rate_init=0.01, - early_stopping=True)) +est = make_pipeline( + QuantileTransformer(), + MLPRegressor( + hidden_layer_sizes=(50, 50), learning_rate_init=0.01, early_stopping=True + ), +) est.fit(X_train, y_train) print(f"done in {time() - tic:.3f}s") print(f"Test R2 score: {est.score(X_test, y_test):.2f}") @@ -113,25 +113,25 @@ from sklearn.inspection import partial_dependence from sklearn.inspection import plot_partial_dependence -print('Computing partial dependence plots...') +print("Computing partial dependence plots...") tic = time() -features = ['MedInc', 'AveOccup', 'HouseAge', 'AveRooms'] +features = ["MedInc", "AveOccup", "HouseAge", "AveRooms"] display = plot_partial_dependence( - est, - X_train, - features, - kind="both", - subsample=50, - n_jobs=3, - grid_resolution=20, - random_state=0, - ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5}, - pd_line_kw={"color": "tab:orange", "linestyle": "--"}, + est, + X_train, + features, + kind="both", + subsample=50, + n_jobs=3, + grid_resolution=20, + random_state=0, + ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5}, + pd_line_kw={"color": "tab:orange", "linestyle": "--"}, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( - 'Partial dependence of house value on non-location features\n' - 'for the California housing dataset, with MLPRegressor' + "Partial dependence of house value on non-location features\n" + "for the California housing dataset, with MLPRegressor" ) display.figure_.subplots_adjust(hspace=0.3) @@ -164,7 +164,7 @@ # We will plot the partial dependence, both individual (ICE) and averaged one # (PDP). We limit to only 50 ICE curves to not overcrowd the plot. -print('Computing partial dependence plots...') +print("Computing partial dependence plots...") tic = time() display = plot_partial_dependence( est, @@ -175,13 +175,13 @@ n_jobs=3, grid_resolution=20, random_state=0, - ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5}, - pd_line_kw={"color": "tab:orange", "linestyle": "--"}, + ice_lines_kw={"color": "tab:blue", "alpha": 0.2, "linewidth": 0.5}, + pd_line_kw={"color": "tab:orange", "linestyle": "--"}, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( - 'Partial dependence of house value on non-location features\n' - 'for the California housing dataset, with Gradient Boosting' + "Partial dependence of house value on non-location features\n" + "for the California housing dataset, with Gradient Boosting" ) display.figure_.subplots_adjust(wspace=0.4, hspace=0.3) @@ -225,18 +225,23 @@ # the tree-based algorithm, when only PDPs are requested, they can be computed # on an efficient way using the `'recursion'` method. -features = ['AveOccup', 'HouseAge', ('AveOccup', 'HouseAge')] -print('Computing partial dependence plots...') +features = ["AveOccup", "HouseAge", ("AveOccup", "HouseAge")] +print("Computing partial dependence plots...") tic = time() _, ax = plt.subplots(ncols=3, figsize=(9, 4)) display = plot_partial_dependence( - est, X_train, features, kind='average', n_jobs=3, grid_resolution=20, + est, + X_train, + features, + kind="average", + n_jobs=3, + grid_resolution=20, ax=ax, ) print(f"done in {time() - tic:.3f}s") display.figure_.suptitle( - 'Partial dependence of house value on non-location features\n' - 'for the California housing dataset, with Gradient Boosting' + "Partial dependence of house value on non-location features\n" + "for the California housing dataset, with Gradient Boosting" ) display.figure_.subplots_adjust(wspace=0.4, hspace=0.3) @@ -256,24 +261,26 @@ import numpy as np from mpl_toolkits.mplot3d import Axes3D + fig = plt.figure() -features = ('AveOccup', 'HouseAge') +features = ("AveOccup", "HouseAge") pdp = partial_dependence( - est, X_train, features=features, kind='average', grid_resolution=20 + est, X_train, features=features, kind="average", grid_resolution=20 ) XX, YY = np.meshgrid(pdp["values"][0], pdp["values"][1]) Z = pdp.average[0].T ax = Axes3D(fig) -surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, - cmap=plt.cm.BuPu, edgecolor='k') +surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor="k") ax.set_xlabel(features[0]) ax.set_ylabel(features[1]) -ax.set_zlabel('Partial dependence') +ax.set_zlabel("Partial dependence") # pretty init view ax.view_init(elev=22, azim=122) plt.colorbar(surf) -plt.suptitle('Partial dependence of house value on median\n' - 'age and average occupancy, with Gradient Boosting') +plt.suptitle( + "Partial dependence of house value on median\n" + "age and average occupancy, with Gradient Boosting" +) plt.subplots_adjust(top=0.9) plt.show() From 2db0eb12b8f2cd0e122636d8e6758354c3dfe130 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 9 Jul 2021 18:32:08 +0200 Subject: [PATCH 15/20] solve issue warning --- sklearn/inspection/_plot/tests/test_plot_partial_dependence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index 7ee35d9b9d83c..5748efb01f8a5 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -743,7 +743,7 @@ def test_plot_partial_dependence_lines_kw_warnings( Check that passing `line_kw` along with `pd_line_kw` and `ice_lines_kw` raises warnings. """ - with pytest.warns(UserWarning, match=r"^Both line_kw and") as record: + with pytest.warns(UserWarning, match=r"^Both `line_kw` and") as record: plot_partial_dependence( clf_diabetes, diabetes.data, From 17cf12309979434faeb019c59936e62afcecc120 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 30 Jul 2021 17:38:19 +0200 Subject: [PATCH 16/20] TST check that we don't raise spurious warnings --- sklearn/inspection/_plot/partial_dependence.py | 10 +++++----- .../tests/test_plot_partial_dependence.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index 007b63d747adc..d3ec381ac1ba8 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -925,11 +925,6 @@ def plot( import matplotlib.pyplot as plt # noqa from matplotlib.gridspec import GridSpecFromSubplotSpec # noqa - if line_kw is None: - line_kw = {} - if contour_kw is None: - contour_kw = {} - if line_kw is not None and ice_lines_kw is not None: warnings.warn( "Both `line_kw` and `ice_lines_kw` are specified. `ice_lines_kw` " @@ -942,6 +937,11 @@ def plot( "take priority. Do not pass `line_kw` to silence this warning." ) + if line_kw is None: + line_kw = {} + if contour_kw is None: + contour_kw = {} + if ax is None: _, ax = plt.subplots() diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index 5748efb01f8a5..f62f05f00a0cc 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -769,3 +769,21 @@ def test_plot_partial_dependence_lines_kw_warnings( assert ice_lines_kw_msg in warnings_messages assert pd_line_kw_msg in warnings_messages + + # Make sure that we don't raise any warnings otherwise + for kwargs in [ + {"ice_lines_kw": {"color": "red"}}, + {"pd_line_kw": {"color": "red"}}, + ]: + with pytest.warns(None) as records: + plot_partial_dependence( + clf_diabetes, + diabetes.data, + [0, 2], + grid_resolution=20, + feature_names=diabetes.feature_names, + n_cols=2, + kind="both", + **kwargs, + ) + assert len(records) == 0 From 739f3f98371e8c551a2ec0cc6c6cbc0c4b9e453d Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Mon, 2 Aug 2021 11:45:44 +0200 Subject: [PATCH 17/20] avoid warning --- .../inspection/_plot/partial_dependence.py | 39 ++++------- .../tests/test_plot_partial_dependence.py | 67 +++---------------- 2 files changed, 24 insertions(+), 82 deletions(-) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index d3ec381ac1ba8..b66f0088498b8 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -1,7 +1,6 @@ import numbers from itertools import chain from math import ceil -import warnings import numpy as np from scipy import sparse @@ -925,20 +924,12 @@ def plot( import matplotlib.pyplot as plt # noqa from matplotlib.gridspec import GridSpecFromSubplotSpec # noqa - if line_kw is not None and ice_lines_kw is not None: - warnings.warn( - "Both `line_kw` and `ice_lines_kw` are specified. `ice_lines_kw` " - "will take priority. Do not pass `line_kw` to silence this " - "warning." - ) - if line_kw is not None and pd_line_kw is not None: - warnings.warn( - "Both `line_kw` and `pd_line_kw` are specified. `pd_line_kw` will " - "take priority. Do not pass `line_kw` to silence this warning." - ) - if line_kw is None: line_kw = {} + if ice_lines_kw is None: + ice_lines_kw = {} + if pd_line_kw is None: + pd_line_kw = {} if contour_kw is None: contour_kw = {} @@ -952,22 +943,20 @@ def plot( "color": "C0", "label": "average" if self.kind == "both" else None, } - - if ice_lines_kw is None: - ice_lines_kw = {**default_line_kws, **line_kw} - - if self.kind == "individual" or self.kind == "both": - ice_lines_kw["alpha"] = 0.3 - ice_lines_kw["linewidth"] = 0.5 + if self.kind in ("individual", "both"): + default_ice_lines_kws = {"alpha": 0.3, "linewidth": 0.5} else: - ice_lines_kw = {**default_line_kws, **ice_lines_kw} + default_ice_lines_kws = {} + ice_lines_kw = { + **default_line_kws, + **line_kw, + **default_ice_lines_kws, + **ice_lines_kw, + } del ice_lines_kw["label"] - if pd_line_kw is None: - pd_line_kw = {**default_line_kws, **line_kw} - else: - pd_line_kw = {**default_line_kws, **pd_line_kw} + pd_line_kw = {**default_line_kws, **line_kw, **pd_line_kw} n_features = len(self.features) if self.kind in ("individual", "both"): diff --git a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py index f62f05f00a0cc..4d33313c8c884 100644 --- a/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py +++ b/sklearn/inspection/_plot/tests/test_plot_partial_dependence.py @@ -698,6 +698,7 @@ def test_partial_dependence_overwrite_labels( ({"color": "r"}, None, {"color": "b"}, ("r", "b")), ({"color": "r"}, {"color": "g"}, None, ("g", "r")), ({"color": "r"}, None, None, ("r", "r")), + ({"color": "r"}, {"linestyle": "--"}, {"linestyle": "-."}, ("r", "r")), ], ) def test_plot_partial_dependence_lines_kw( @@ -709,8 +710,7 @@ def test_plot_partial_dependence_lines_kw( ice_lines_kw, expected_colors, ): - """ - Check that passing `pd_line_kw` and `ice_lines_kw` will act on the + """Check that passing `pd_line_kw` and `ice_lines_kw` will act on the specific lines in the plot. """ @@ -729,61 +729,14 @@ def test_plot_partial_dependence_lines_kw( line = disp.lines_[0, 0, -1] assert line.get_color() == expected_colors[0] + if pd_line_kw is not None and "linestyle" in pd_line_kw: + assert line.get_linestyle() == pd_line_kw["linestyle"] + else: + assert line.get_linestyle() == "-" line = disp.lines_[0, 0, 0] assert line.get_color() == expected_colors[1] - - -def test_plot_partial_dependence_lines_kw_warnings( - pyplot, - clf_diabetes, - diabetes, -): - """ - Check that passing `line_kw` along with `pd_line_kw` and `ice_lines_kw` - raises warnings. - """ - with pytest.warns(UserWarning, match=r"^Both `line_kw` and") as record: - plot_partial_dependence( - clf_diabetes, - diabetes.data, - [0, 2], - grid_resolution=20, - feature_names=diabetes.feature_names, - n_cols=2, - kind="both", - line_kw={"color": "r"}, - pd_line_kw={"color": "g"}, - ice_lines_kw={"color": "b"}, - ) - - warnings_messages = {rcd.message.args[0] for rcd in record} - ice_lines_kw_msg = ( - "Both `line_kw` and `ice_lines_kw` are specified. `ice_lines_kw` " - "will take priority. Do not pass `line_kw` to silence this warning." - ) - pd_line_kw_msg = ( - "Both `line_kw` and `pd_line_kw` are specified. `pd_line_kw` " - "will take priority. Do not pass `line_kw` to silence this warning." - ) - - assert ice_lines_kw_msg in warnings_messages - assert pd_line_kw_msg in warnings_messages - - # Make sure that we don't raise any warnings otherwise - for kwargs in [ - {"ice_lines_kw": {"color": "red"}}, - {"pd_line_kw": {"color": "red"}}, - ]: - with pytest.warns(None) as records: - plot_partial_dependence( - clf_diabetes, - diabetes.data, - [0, 2], - grid_resolution=20, - feature_names=diabetes.feature_names, - n_cols=2, - kind="both", - **kwargs, - ) - assert len(records) == 0 + if ice_lines_kw is not None and "linestyle" in ice_lines_kw: + assert line.get_linestyle() == ice_lines_kw["linestyle"] + else: + assert line.get_linestyle() == "-" From 4a012c5b6908b23ec6b7177cb6ce5f4a33f29502 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Mon, 2 Aug 2021 11:51:03 +0200 Subject: [PATCH 18/20] improve doc --- sklearn/inspection/_plot/partial_dependence.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index b66f0088498b8..1036e9996e254 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -187,19 +187,22 @@ def plot_partial_dependence( line_kw : dict, default=None Dict with keywords passed to the ``matplotlib.pyplot.plot`` call. - For one-way partial dependence plots. + For one-way partial dependence plots. It can be used to define common + properties for both `ice_lines_kw` and `pdp_line_kw`. ice_lines_kw : dict, default=None Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For ICE lines in the one-way partial dependence plots. - Takes priority over `line_kw` when not `None`. + The couple keyword/value defined in `ice_lines_kw` takes priority over + `line_kw` the one defined in `line_kw`. .. versionadded:: 1.0 pd_line_kw : dict, default=None Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For partial dependence in one-way partial dependence plots. - Takes priority over `line_kw` when not `None`. + The couple keyword/value defined in `pd_line_kw` takes priority over + `line_kw` the one defined in `line_kw`. .. versionadded:: 1.0 From c45c98d411864ac459890bd407e7460f4ba218e9 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Mon, 16 Aug 2021 11:29:53 +0200 Subject: [PATCH 19/20] Apply suggestions from code review Co-authored-by: Julien Jerphanion Co-authored-by: Thomas J. Fan --- examples/inspection/plot_partial_dependence.py | 3 ++- sklearn/inspection/_plot/partial_dependence.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/inspection/plot_partial_dependence.py b/examples/inspection/plot_partial_dependence.py index 53b6074e578a6..42ab6e1a5110f 100644 --- a/examples/inspection/plot_partial_dependence.py +++ b/examples/inspection/plot_partial_dependence.py @@ -270,7 +270,8 @@ ) XX, YY = np.meshgrid(pdp["values"][0], pdp["values"][1]) Z = pdp.average[0].T -ax = Axes3D(fig) +ax = Axes3D(fig, auto_add_to_figure=False) +fig.add_axes(ax) surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor="k") ax.set_xlabel(features[0]) ax.set_ylabel(features[1]) diff --git a/sklearn/inspection/_plot/partial_dependence.py b/sklearn/inspection/_plot/partial_dependence.py index 1036e9996e254..f03669e7a4207 100644 --- a/sklearn/inspection/_plot/partial_dependence.py +++ b/sklearn/inspection/_plot/partial_dependence.py @@ -193,16 +193,16 @@ def plot_partial_dependence( ice_lines_kw : dict, default=None Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For ICE lines in the one-way partial dependence plots. - The couple keyword/value defined in `ice_lines_kw` takes priority over - `line_kw` the one defined in `line_kw`. + The key value pairs defined in `ice_lines_kw` takes priority over + `line_kw`. .. versionadded:: 1.0 pd_line_kw : dict, default=None Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For partial dependence in one-way partial dependence plots. - The couple keyword/value defined in `pd_line_kw` takes priority over - `line_kw` the one defined in `line_kw`. + The key value pairs defined in `pd_line_kw` takes priority over + `line_kw`. .. versionadded:: 1.0 @@ -903,14 +903,16 @@ def plot( ice_lines_kw : dict, default=None Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For ICE lines in the one-way partial dependence plots. - Takes priority over `line_kw` when not `None`. + The key value pairs defined in `ice_lines_kw` takes priority over + `line_kw`. .. versionadded:: 1.0 pd_line_kw : dict, default=None Dictionary with keywords passed to the `matplotlib.pyplot.plot` call. For partial dependence in one-way partial dependence plots. - Takes priority over `line_kw` when not `None`. + The key value pairs defined in `pd_line_kw` takes priority over + `line_kw`. .. versionadded:: 1.0 From 15a42889e1070cf0e75430cf77bcffa89b4acfe1 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Mon, 16 Aug 2021 14:39:20 +0200 Subject: [PATCH 20/20] Update plot_partial_dependence.py --- examples/inspection/plot_partial_dependence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/inspection/plot_partial_dependence.py b/examples/inspection/plot_partial_dependence.py index 42ab6e1a5110f..ceccd8c3001c1 100644 --- a/examples/inspection/plot_partial_dependence.py +++ b/examples/inspection/plot_partial_dependence.py @@ -270,7 +270,7 @@ ) XX, YY = np.meshgrid(pdp["values"][0], pdp["values"][1]) Z = pdp.average[0].T -ax = Axes3D(fig, auto_add_to_figure=False) +ax = Axes3D(fig) fig.add_axes(ax) surf = ax.plot_surface(XX, YY, Z, rstride=1, cstride=1, cmap=plt.cm.BuPu, edgecolor="k") ax.set_xlabel(features[0])