8000 [MRG+1] explicit exception message for strict selectors by ogrisel · Pull Request #4206 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG+1] explicit exception message for strict selectors #4206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2015
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
6 changes: 6 additions & 0 deletions sklearn/feature_selection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# License: BSD 3 clause

from abc import ABCMeta, abstractmethod
from warnings import warn

import numpy as np
from scipy.sparse import issparse, csc_matrix
Expand Down Expand Up @@ -73,6 +74,11 @@ def transform(self, X):
"""
X = check_array(X, accept_sparse='csr')
mask = self.get_support()
if not mask.any():
warn("No features were selected: either the data is"
" too noisy or the selection test too strict.",
UserWarning)
return np.empty(0).reshape((X.shape[0], 0))
if len(mask) != X.shape[1]:
raise ValueError("X has a different shape than during fitting.")
return check_array(X, accept_sparse='csr')[:, safe_mask(X, mask)]
Expand Down
27 changes: 26 additions & 1 deletion sklearn/feature_selection/tests/test_feature_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sklearn.utils.testing import assert_less
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import ignore_warnings
from sklearn.utils.testing import assert_warns_message
from sklearn.utils import safe_mask

from sklearn.datasets.samples_generator import (make_classification,
Expand Down Expand Up @@ -251,10 +252,13 @@ def test_select_kbest_zero():
shuffle=False, random_state=0)

univariate_filter = SelectKBest(f_classif, k=0)
univariate_filter.fit(X, y).transform(X)
univariate_filter.fit(X, y)
support = univariate_filter.get_support()
gtruth = np.zeros(10, dtype=bool)
assert_array_equal(support, gtruth)
X_selected = assert_warns_message(UserWarning, 'No features were selected',
univariate_filter.transform, X)
assert_equal(X_selected.shape, (20, 0))


def test_select_fpr_classif():
Expand Down Expand Up @@ -585,3 +589,24 @@ def test_f_classif_constant_feature():
X, y = make_classification(n_samples=10, n_features=5)
X[:, 0] = 2.0
assert_warns(UserWarning, f_classif, X, y)


def test_no_feature_selected():
rng = np.random.RandomState(0)

# Generate random uncorrelated data: a strict univariate test should
# rejects all the features
X = rng.rand(40, 10)
y = rng.randint(0, 4, size=40)
strict_selectors = [
SelectFwe(alpha=0.01).fit(X, y),
SelectFdr(alpha=0.01).fit(X, y),
SelectFpr(alpha=0.01).fit(X, y),
SelectPercentile(percentile=0).fit(X, y),
SelectKBest(k=0).fit(X, y),
]
for selector in strict_selectors:
assert_array_equal(selector.get_support(), np.zeros(10))
X_selected = assert_warns_message(
UserWarning, 'No features were selected', selector.transform, X)
assert_equal(X_selected.shape, (40, 0))
6 changes: 4 additions & 2 deletions sklearn/feature_selection/univariate_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,10 @@ def _get_support_mask(self):

alpha = self.alpha
sv = np.sort(self.pvalues_)
threshold = sv[sv < alpha * np.arange(len(self.pvalues_))].max()
return self.pvalues_ <= threshold
selected = sv[sv < alpha * np.arange(len(self.pvalues_))]
if selected.size == 0:
return np.zeros_like(self.pvalues_, dtype=bool)
return self.pvalues_ <= selected.max()


class SelectFwe(_BaseFilter):
Expand Down
0