From 698ddac6f5f445bf5def785b26ac5f5ba7ca9a55 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Wed, 25 Jan 2023 14:16:46 +0100 Subject: [PATCH 1/6] Enable setting of sub-parameters for base_estimator param The base_estimator param was renamed to estimator which lead to users not being able to set parameters for base_estimator__* properties. --- sklearn/base.py | 6 ++++++ sklearn/ensemble/tests/test_weight_boosting.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/sklearn/base.py b/sklearn/base.py index 37c61e4cb34d6..c69ad5cc27531 100644 --- a/sklearn/base.py +++ b/sklearn/base.py @@ -227,6 +227,12 @@ def set_params(self, **params): valid_params[key] = value for key, sub_params in nested_params.items(): + # The "base_estimator" key is special. It was deprecated and + # renamed to "estimator". This means we need to translate it + # here and set sub-parameters on "estimator", but only if the + # user did not explicitly set a value for "base_estimator". + if key == "base_estimator" and valid_params[key] == "deprecated": + key = "estimator" valid_params[key].set_params(**sub_params) return self diff --git a/sklearn/ensemble/tests/test_weight_boosting.py b/sklearn/ensemble/tests/test_weight_boosting.py index c46b140503fd8..aca28e5dbf6ee 100755 --- a/sklearn/ensemble/tests/test_weight_boosting.py +++ b/sklearn/ensemble/tests/test_weight_boosting.py @@ -630,3 +630,20 @@ def test_base_estimator_property_deprecated(AdaBoost): ) with pytest.warns(FutureWarning, match=warn_msg): model.base_estimator_ + + +# TODO(1.4): remove in 1.4 +def test_deprecated_base_estimator_parameters_can_be_set(): + """Check that setting base_estimator parameters works. + + During the deprecation cycle setting "base_estimator__*" params should + work. + + Non-regression test for https://github.com/scikit-learn/scikit-learn/issues/25470 + """ + # This implicitly sets "estimator", it is how old code (pre v1.2) would + # have instantiated AdaBoostClassifier and back then it would set + # "base_estimator". + clf = AdaBoostClassifier(DecisionTreeClassifier()) + + clf.set_params(base_estimator__max_depth=2) From 3d6d13c20ecba8c103252280a2c5008eac2b57a3 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Thu, 26 Jan 2023 09:04:41 +0100 Subject: [PATCH 2/6] Raise a warning when using old parameter names --- sklearn/base.py | 20 +++++++++++++++---- .../ensemble/tests/test_weight_boosting.py | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/sklearn/base.py b/sklearn/base.py index c69ad5cc27531..72d3f88cd6a24 100644 --- a/sklearn/base.py +++ b/sklearn/base.py @@ -228,10 +228,22 @@ def set_params(self, **params): for key, sub_params in nested_params.items(): # The "base_estimator" key is special. It was deprecated and - # renamed to "estimator". This means we need to translate it - # here and set sub-parameters on "estimator", but only if the - # user did not explicitly set a value for "base_estimator". - if key == "base_estimator" and valid_params[key] == "deprecated": + # renamed to "estimator" for several estimators. This means we + # need to translate it here and set sub-parameters on "estimator", + # but only if the user did not explicitly set a value for + # "base_estimator". + if ( + key == "base_estimator" + and valid_params[key] == "deprecated" + and self.__module__.startswith("sklearn.") + ): + warnings.warn( + f"Parameter 'base_estimator' of {self.__class__.__name__} is" + " deprecated in favor of 'estimator'. See" + f" {self.__class__.__name__}'s docstring for more details.", + FutureWarning, + stacklevel=2, + ) key = "estimator" valid_params[key].set_params(**sub_params) diff --git a/sklearn/ensemble/tests/test_weight_boosting.py b/sklearn/ensemble/tests/test_weight_boosting.py index aca28e5dbf6ee..eeaffab09a6c8 100755 --- a/sklearn/ensemble/tests/test_weight_boosting.py +++ b/sklearn/ensemble/tests/test_weight_boosting.py @@ -646,4 +646,5 @@ def test_deprecated_base_estimator_parameters_can_be_set(): # "base_estimator". clf = AdaBoostClassifier(DecisionTreeClassifier()) - clf.set_params(base_estimator__max_depth=2) + with pytest.warns(FutureWarning, match="Parameter 'base_estimator' of"): + clf.set_params(base_estimator__max_depth=2) From 2b13d8bfbfd5a49fabafd4a4455668d49977e5a2 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Thu, 26 Jan 2023 09:13:02 +0100 Subject: [PATCH 3/6] Add what's new entries The list of effected estimators comes from looking at the v1.2 what's new to find where the renaming took place. --- doc/whats_new/v1.3.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/whats_new/v1.3.rst b/doc/whats_new/v1.3.rst index 514a012e0ffaf..cd7d2519590b6 100644 --- a/doc/whats_new/v1.3.rst +++ b/doc/whats_new/v1.3.rst @@ -96,6 +96,13 @@ Changelog - |Feature| A `__sklearn_clone__` protocol is now available to override the default behavior of :func:`base.clone`. :pr:`24568` by `Thomas Fan`_. +:mod:`sklearn.calibration` +.......................... + +- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix + to set parameters of the estimator used in :class:`calibration.CalibratedClassifierCV`. + :pr:`25477` by :user:`Tim Head `. + :mod:`sklearn.cluster` ...................... @@ -130,6 +137,12 @@ Changelog scikit-learn 1.3: retraining with scikit-learn 1.3 is required. :pr:`25186` by :user:`Felipe Breve Siola `. +- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix + to set parameters of the estimator used in :class:`ensemble.AdaBoostClassifier`, + :class:`ensemble.AdaBoostRegressor`, :class:`ensemble.BaggingClassifier`, + and :class:`ensemble.BaggingRegressor`. + :pr:`25477` by :user:`Tim Head `. + :mod:`sklearn.exception` ........................ - |Feature| Added :class:`exception.InconsistentVersionWarning` which is raised From a331227f63b7ee6b8d956634daa26f63b5b540e7 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Wed, 8 Feb 2023 10:27:39 +0100 Subject: [PATCH 4/6] Move what's new entries, add TODO marker --- doc/whats_new/v1.2.rst | 16 ++++++++++++++++ doc/whats_new/v1.3.rst | 13 ------------- sklearn/base.py | 1 + 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index c572acf49370c..54200faf1a1bc 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -27,6 +27,22 @@ Changes impacting all modules Changelog --------- +:mod:`sklearn.calibration` +.......................... + +- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix + to set parameters of the estimator used in :class:`calibration.CalibratedClassifierCV`. + :pr:`25477` by :user:`Tim Head `. + +:mod:`sklearn.ensemble` +....................... + +- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix + to set parameters of the estimator used in :class:`ensemble.AdaBoostClassifier`, + :class:`ensemble.AdaBoostRegressor`, :class:`ensemble.BaggingClassifier`, + and :class:`ensemble.BaggingRegressor`. + :pr:`25477` by :user:`Tim Head `. + :mod:`sklearn.isotonic` ....................... diff --git a/doc/whats_new/v1.3.rst b/doc/whats_new/v1.3.rst index c689e585a7f8e..31ebe9e2d21d6 100644 --- a/doc/whats_new/v1.3.rst +++ b/doc/whats_new/v1.3.rst @@ -96,13 +96,6 @@ Changelog - |Feature| A `__sklearn_clone__` protocol is now available to override the default behavior of :func:`base.clone`. :pr:`24568` by `Thomas Fan`_. -:mod:`sklearn.calibration` -.......................... - -- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix - to set parameters of the estimator used in :class:`calibration.CalibratedClassifierCV`. - :pr:`25477` by :user:`Tim Head `. - :mod:`sklearn.cluster` ...................... @@ -143,12 +136,6 @@ Changelog scikit-learn 1.3: retraining with scikit-learn 1.3 is required. :pr:`25186` by :user:`Felipe Breve Siola `. -- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix - to set parameters of the estimator used in :class:`ensemble.AdaBoostClassifier`, - :class:`ensemble.AdaBoostRegressor`, :class:`ensemble.BaggingClassifier`, - and :class:`ensemble.BaggingRegressor`. - :pr:`25477` by :user:`Tim Head `. - - |Enhancement| :class:`ensemble.BaggingClassifier` and :class:`ensemble.BaggingRegressor` expose the `allow_nan` tag from the underlying estimator. :pr:`25506` by `Thomas Fan`_. diff --git a/sklearn/base.py b/sklearn/base.py index 72d3f88cd6a24..5c7168adabc5e 100644 --- a/sklearn/base.py +++ b/sklearn/base.py @@ -227,6 +227,7 @@ def set_params(self, **params): valid_params[key] = value for key, sub_params in nested_params.items(): + # TODO(1.4): remove specific handling of "base_estimator". # The "base_estimator" key is special. It was deprecated and # renamed to "estimator" for several estimators. This means we # need to translate it here and set sub-parameters on "estimator", From 8be004ef88b0f136a2ef915a00a2b7b722c1324d Mon Sep 17 00:00:00 2001 From: Tim Head Date: Wed, 8 Feb 2023 11:13:21 +0100 Subject: [PATCH 5/6] Fix missing newline --- doc/whats_new/v1.2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index a3a1d51f6febc..38776f6e05c38 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -40,6 +40,7 @@ Changelog - |Fix| Fixed a bug in :class:`cluster.BisectingKMeans`, preventing `fit` to randomly fail due to a permutation of the labels when running multiple inits. :pr:`25563` by :user:`Jérémie du Boisberranger `. + :mod:`sklearn.ensemble` ....................... From 5c4b441ce7eb1c7c7decfe55c715db8ce72c4d97 Mon Sep 17 00:00:00 2001 From: Tim Head Date: Wed, 8 Feb 2023 12:33:40 +0100 Subject: [PATCH 6/6] Fix what's new syntax --- doc/whats_new/v1.2.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index 38776f6e05c38..ee327f8807a25 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -30,7 +30,7 @@ Changelog :mod:`sklearn.calibration` .......................... -- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix +- |Fix| A deprecation warning is raised when using the `base_estimator__` prefix to set parameters of the estimator used in :class:`calibration.CalibratedClassifierCV`. :pr:`25477` by :user:`Tim Head `. @@ -44,7 +44,7 @@ Changelog :mod:`sklearn.ensemble` ....................... -- |Fix| A deprecation warning is raised when using the "base_estimator__" prefix +- |Fix| A deprecation warning is raised when using the `base_estimator__` prefix to set parameters of the estimator used in :class:`ensemble.AdaBoostClassifier`, :class:`ensemble.AdaBoostRegressor`, :class:`ensemble.BaggingClassifier`, and :class:`ensemble.BaggingRegressor`.