diff --git a/doc/whats_new/v1.2.rst b/doc/whats_new/v1.2.rst index 0e62b87a2ab48..9536c475bc608 100644 --- a/doc/whats_new/v1.2.rst +++ b/doc/whats_new/v1.2.rst @@ -27,6 +27,13 @@ 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.cluster` ...................... @@ -41,6 +48,15 @@ Changelog empty selection of columns when `set_output(transform="pandas")`. :pr:`25570` by `Thomas Fan`_. +: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/sklearn/base.py b/sklearn/base.py index 37c61e4cb34d6..5c7168adabc5e 100644 --- a/sklearn/base.py +++ b/sklearn/base.py @@ -227,6 +227,25 @@ 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", + # 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) return self diff --git a/sklearn/ensemble/tests/test_weight_boosting.py b/sklearn/ensemble/tests/test_weight_boosting.py index c46b140503fd8..eeaffab09a6c8 100755 --- a/sklearn/ensemble/tests/test_weight_boosting.py +++ b/sklearn/ensemble/tests/test_weight_boosting.py @@ -630,3 +630,21 @@ 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()) + + with pytest.warns(FutureWarning, match="Parameter 'base_estimator' of"): + clf.set_params(base_estimator__max_depth=2)