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
5 changes: 5 additions & 0 deletions doc/whats_new/v1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ Changelog
not check for nans during input validation.
:pr:`21807` by `Thomas Fan`_.

- |Enhancement| :class:`feature_selection.SelectKBest` and
:class:`feature_selection.GenericUnivariateSelect` with `mode='k_best'`
now shows a warning when `k` is greater than the number of features.
:pr:`27841` by `Thomas Fan`_.

:mod:`sklearn.inspection`
.........................

Expand Down
6 changes: 3 additions & 3 deletions sklearn/feature_selection/_univariate_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,9 @@ def __init__(self, score_func=f_classif, *, k=10):

def _check_params(self, X, y):
if not isinstance(self.k, str) and self.k > X.shape[1]:
raise ValueError(
f"k should be <= n_features = {X.shape[1]}; "
f"got {self.k}. Use k='all' to return all features."
warnings.warn(
f"k={self.k} is greater than n_features={X.shape[1]}. "
"All the features will be returned."
)

def _get_support_mask(self):
Expand Down
5 changes: 3 additions & 2 deletions sklearn/feature_selection/tests/test_feature_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,10 @@ def test_invalid_k():
X = [[0, 1, 0], [0, -1, -1], [0, 0.5, 0.5]]
y = [1, 0, 1]

with pytest.raises(ValueError):
msg = "k=4 is greater than n_features=3. All the features will be returned."
with pytest.warns(UserWarning, match=msg):
SelectKBest(k=4).fit(X, y)
with pytest.raises(ValueError):
with pytest.warns(UserWarning, match=msg):
GenericUnivariateSelect(mode="k_best", param=4).fit(X, y)


Expand Down