8000 FIX explicit exception message for strict selectors · scikit-learn/scikit-learn@99f0044 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99f0044

Browse files
committed
FIX explicit exception message for strict selectors
1 parent 1d08f08 commit 99f0044

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

sklearn/feature_selection/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def transform(self, X):
7373
"""
7474
X = check_array(X, accept_sparse='csr')
7575
mask = self.get_support()
76+
if not mask.any():
77+
raise ValueError("No features were selected: either the data is"
78+
" too noisy or the selection test too strict.")
7679
if len(mask) != X.shape[1]:
7780
raise ValueError("X has a different shape than during fitting.")
7881
return check_array(X, accept_sparse='csr')[:, safe_mask(X, mask)]

sklearn/feature_selection/tests/test_feature_select.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sklearn.utils.testing import assert_equal
1010
from sklearn.utils.testing import assert_almost_equal
1111
from sklearn.utils.testing import assert_raises
12+
from sklearn.utils.testing import assert_raise_message
1213
from sklearn.utils.testing import assert_true
1314
from sklearn.utils.testing import assert_array_equal
1415
from sklearn.utils.testing import assert_array_almost_equal
@@ -251,10 +252,12 @@ def test_select_kbest_zero():
251252
shuffle=False, random_state=0)
252253

253254
univariate_filter = SelectKBest(f_classif, k=0)
254-
univariate_filter.fit(X, y).transform(X)
255+
univariate_filter.fit(X, y)
255256
support = univariate_filter.get_support()
256257
gtruth = np.zeros(10, dtype=bool)
257258
assert_array_equal(support, gtruth)
259+
assert_raise_message(ValueError, 'No features were selected',
260+
univariate_filter.transform, X)
258261

259262

260263
def test_select_fpr_classif():
@@ -585,3 +588,17 @@ def test_f_classif_constant_feature():
585588
X, y = make_classification(n_samples=10, n_features=5)
586589
X[:, 0] = 2.0
587590
assert_warns(UserWarning, f_classif, X, y)
591+
592+
593+
def test_no_feature_selected():
594+
rng = np.random.RandomState(0)
595+
596+
# Generate random uncorrelated data: a strict univariate test should
597+
# rejects all the features
598+
X = rng.rand(40, 10)
599+
y = rng.randint(0, 4, size=40)
600+
fdr = SelectFdr(alpha=0.00001).fit(X, y)
601+
assert_array_equal(fdr.get_support(), np.zeros(10))
602+
603+
assert_raise_message(ValueError, 'No features were selected',
604+
fdr.transform, X)

sklearn/feature_selection/univariate_selection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,10 @@ def _get_support_mask(self):
496496

497497
alpha = self.alpha
498498
sv = np.sort(self.pvalues_)
499-
threshold = sv[sv < alpha * np.arange(len(self.pvalues_))].max()
500-
return self.pvalues_ <= threshold
499+
selected = sv[sv < alpha * np.arange(len(self.pvalues_))]
500+
if selected.size == 0:
501+
return np.zeros_like(self.pvalues_, dtype=bool)
502+
return self.pvalues_ <= selected.max()
501503

502504

503505
class SelectFwe(_BaseFilter):

0 commit comments

Comments
 (0)
0