From a3f7d14912269f35e96153f7ef54fe73af45dd74 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Mon, 6 Sep 2021 21:14:50 -0400 Subject: [PATCH 1/2] ENH Adds feature_names_in_ for estimators that do not validate --- sklearn/dummy.py | 36 +++++++++++++++++++++++++++++------- sklearn/tests/test_dummy.py | 5 ++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/sklearn/dummy.py b/sklearn/dummy.py index 4fb08c09ffc36..6b2133defce6f 100644 --- a/sklearn/dummy.py +++ b/sklearn/dummy.py @@ -10,6 +10,7 @@ from .base import BaseEstimator, ClassifierMixin, RegressorMixin from .base import MultiOutputMixin from .utils import check_random_state +from .utils import deprecated from .utils.validation import _num_samples from .utils.validation import check_array from .utils.validation import check_consistent_length @@ -75,10 +76,12 @@ class DummyClassifier(MultiOutputMixin, ClassifierMixin, BaseEstimator): n_outputs_ : int Number of outputs. - n_features_in_ : int - Number of features seen during :term:`fit`. + n_features_in_ : `None` + Always set to `None`. .. versionadded:: 0.24 + .. deprecated:: 1.0 + Will be removed in 1.0 sparse_output_ : bool True if the array returned from predict is to be in sparse CSC format. @@ -164,8 +167,6 @@ def fit(self, X, y, sample_weight=None): self.n_outputs_ = y.shape[1] - self.n_features_in_ = None # No input validation is done for X - check_consistent_length(X, y) if sample_weight is not None: @@ -421,6 +422,16 @@ def score(self, X, y, sample_weight=None): X = np.zeros(shape=(len(y), 1)) return super().score(X, y, sample_weight) + # TODO: Remove in 1.2 + # mypy error: Decorated property not supported + @deprecated( # type: ignore + "`n_features_in_` is deprecated in 1.0 and will be removed in 1.2." + ) + @property + def n_features_in_(self): + check_is_fitted(self) + return None + class DummyRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator): """Regressor that makes predictions using simple rules. @@ -459,10 +470,12 @@ class DummyRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator): Mean or median or quantile of the training targets or constant value given by the user. - n_features_in_ : int - Number of features seen during :term:`fit`. + n_features_in_ : `None` + Always set to `None`. .. versionadded:: 0.24 + .. deprecated:: 1.0 + Will be removed in 1.0 n_outputs_ : int Number of outputs. @@ -518,7 +531,6 @@ def fit(self, X, y, sample_weight=None): ) y = check_array(y, ensure_2d=False) - self.n_features_in_ = None # No input validation is done for X if len(y) == 0: raise ValueError("y must not be empty.") @@ -656,3 +668,13 @@ def score(self, X, y, sample_weight=None): if X is None: X = np.zeros(shape=(len(y), 1)) return super().score(X, y, sample_weight) + + # TODO: Remove in 1.2 + # mypy error: Decorated property not supported + @deprecated( # type: ignore + "`n_features_in_` is deprecated in 1.0 and will be removed in 1.2." + ) + @property + def n_features_in_(self): + check_is_fitted(self) + return None diff --git a/sklearn/tests/test_dummy.py b/sklearn/tests/test_dummy.py index 82ff9189e87d1..984b51c25cc94 100644 --- a/sklearn/tests/test_dummy.py +++ b/sklearn/tests/test_dummy.py @@ -733,4 +733,7 @@ def test_n_features_in_(Dummy): d = Dummy() assert not hasattr(d, "n_features_in_") d.fit(X, y) - assert d.n_features_in_ is None + + with pytest.warns(FutureWarning, match="`n_features_in_` is deprecated"): + n_features_in = d.n_features_in_ + assert n_features_in is None From ffc50182f58ed86ed9fa26d0998dd7cb2b339d63 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Mon, 6 Sep 2021 21:39:10 -0400 Subject: [PATCH 2/2] DOC Adds whats new --- doc/whats_new/v1.0.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index d9dcf8757bc68..27e6c666be4f6 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -335,6 +335,13 @@ Changelog in 1.2. Use the new parameters `alpha_W` and `alpha_H` instead. :pr:`20512` by :user:`Jérémie du Boisberranger `. +:mod:`sklearn.dummy` +.................... + +- |API| Attribute `n_features_in_` in :class:`dummy.DummyRegressor` and + :class:`dummy.DummyRegressor` is deprecated and will be removed in 1.2. + :pr:`20960` by `Thomas Fan`_. + :mod:`sklearn.ensemble` .......................