Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5bf9491

Browse files
authored
API Deprecate n_features_in_ from Dummy* (#20960)
1 parent c0e5d1b commit 5bf9491

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

doc/whats_new/v1.0.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ Changelog
335335
in 1.2. Use the new parameters `alpha_W` and `alpha_H` instead. :pr:`20512` by
336336
:user:`Jérémie du Boisberranger <jeremiedbb>`.
337337

338+
:mod:`sklearn.dummy`
339+
....................
340+
341+
- |API| Attribute `n_features_in_` in :class:`dummy.DummyRegressor` and
342+
:class:`dummy.DummyRegressor` is deprecated and will be removed in 1.2.
343+
:pr:`20960` by `Thomas Fan`_.
344+
338345
:mod:`sklearn.ensemble`
339346
.......................
340347

sklearn/dummy.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .base import BaseEstimator, ClassifierMixin, RegressorMixin
1111
from .base import MultiOutputMixin
1212
from .utils import check_random_state
13+
from .utils import deprecated
1314
from .utils.validation import _num_samples
1415
from .utils.validation import check_array
1516
from .utils.validation import check_consistent_length
@@ -75,10 +76,12 @@ class DummyClassifier(MultiOutputMixin, ClassifierMixin, BaseEstimator):
7576
n_outputs_ : int
7677
Number of outputs.
7778
78-
n_features_in_ : int
79-
Number of features seen during :term:`fit`.
79+
n_features_in_ : `None`
80+
Always set to `None`.
8081
8182
.. versionadded:: 0.24
83+
.. deprecated:: 1.0
84+
Will be removed in 1.0
8285
8386
sparse_output_ : bool
8487
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):
164167

165168
self.n_outputs_ = y.shape[1]
166169

167-
self.n_features_in_ = None # No input validation is done for X
168-
169170
check_consistent_length(X, y)
170171

171172
if sample_weight is not None:
@@ -421,6 +422,16 @@ def score(self, X, y, sample_weight=None):
421422
X = np.zeros(shape=(len(y), 1))
422423
return super().score(X, y, sample_weight)
423424

425+
# TODO: Remove in 1.2
426+
# mypy error: Decorated property not supported
427+
@deprecated( # type: ignore
428+
"`n_features_in_` is deprecated in 1.0 and will be removed in 1.2."
429+
)
430+
@property
431+
def n_features_in_(self):
432+
check_is_fitted(self)
433+
return None
434+
424435

425436
class DummyRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
426437
"""Regressor that makes predictions using simple rules.
@@ -459,10 +470,12 @@ class DummyRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
459470
Mean or median or quantile of the training targets or constant value
460471
given by the user.
461472
462-
n_features_in_ : int
463-
Number of features seen during :term:`fit`.
473+
n_features_in_ : `None`
474+
Always set to `None`.
464475
465476
.. versionadded:: 0.24
477+
.. deprecated:: 1.0
478+
Will be removed in 1.0
466479
467480
n_outputs_ : int
468481
Number of outputs.
@@ -518,7 +531,6 @@ def fit(self, X, y, sample_weight=None):
518531
)
519532

520533
y = check_array(y, ensure_2d=False)
521-
self.n_features_in_ = None # No input validation is done for X
522534
if len(y) == 0:
523535
raise ValueError("y must not be empty.")
524536

@@ -656,3 +668,13 @@ def score(self, X, y, sample_weight=None):
656668
if X is None:
657669
X = np.zeros(shape=(len(y), 1))
658670
return super().score(X, y, sample_weight)
671+
672+
# TODO: Remove in 1.2
673+
# mypy error: Decorated property not supported
674+
@deprecated( # type: ignore
675+
"`n_features_in_` is deprecated in 1.0 and will be removed in 1.2."
676+
)
677+
@property
678+
def n_features_in_(self):
679+
check_is_fitted(self)
680+
return None

sklearn/tests/test_dummy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,4 +733,7 @@ def test_n_features_in_(Dummy):
733733
d = Dummy()
734734
assert not hasattr(d, "n_features_in_")
735735
d.fit(X, y)
736-
assert d.n_features_in_ is None
736+
737+
with pytest.warns(FutureWarning, match="`n_features_in_` is deprecated"):
738+
n_features_in = d.n_features_in_
739+
assert n_features_in is None

0 commit comments

Comments
 (0)