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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <jeremiedbb>`.

: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`
.......................

Expand Down
36 changes: 29 additions & 7 deletions sklearn/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.")

Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion sklearn/tests/test_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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