-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
FIX Backwards SequentialFeatureSelector
always drops one feature
#26480
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
Draft
betatim
wants to merge
4
commits into
scikit-learn:main
Choose a base branch
from
betatim:feature-selection-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,14 +46,18 @@ def test_n_features_to_select(direction, n_features_to_select): | |
assert sfs.transform(X).shape[1] == n_features_to_select | ||
|
||
|
||
@pytest.mark.parametrize("direction", ("forward", "backward")) | ||
def test_n_features_to_select_auto(direction): | ||
"""Check the behaviour of `n_features_to_select="auto"` with different | ||
values for the parameter `tol`. | ||
@pytest.mark.parametrize( | ||
"direction,max_features_to_select", (("forward", 9), ("backward", 10)) | ||
) | ||
def test_n_features_to_select_auto(direction, max_features_to_select): | ||
"""Check the behaviour of `n_features_to_select="auto"` when selecting | ||
features in a forward and backward direction. | ||
""" | ||
|
||
n_features = 10 | ||
tol = 1e-3 | ||
if direction == "backward": | ||
tol *= -1 | ||
|
||
X, y = make_regression(n_features=n_features, random_state=0) | ||
sfs = SequentialFeatureSelector( | ||
LinearRegression(), | ||
|
@@ -64,7 +68,7 @@ def test_n_features_to_select_auto(direction): | |
) | ||
sfs.fit(X, y) | ||
|
||
max_features_to_select = n_features - 1 | ||
# max_features_to_select = n_features - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why this was here in the first place :-/ |
||
|
||
assert sfs.get_support(indices=True).shape[0] <= max_features_to_select | ||
assert sfs.n_features_to_select_ <= max_features_to_select | ||
|
@@ -95,6 +99,8 @@ def test_n_features_to_select_stopping_criterion(direction): | |
X, y = make_regression(n_features=50, n_informative=10, random_state=0) | ||
|
||
tol = 1e-3 | ||
if direction == "backward": | ||
tol *= -1 | ||
|
||
sfs = SequentialFeatureSelector( | ||
LinearRegression(), | ||
|
@@ -130,7 +136,15 @@ def test_n_features_to_select_stopping_criterion(direction): | |
assert (sfs_cv_score - added_cv_score) <= tol | ||
assert (sfs_cv_score - removed_cv_score) >= tol | ||
else: | ||
assert (added_cv_score - sfs_cv_score) <= tol | ||
assert sfs_cv_score <= added_cv_score | ||
assert sfs_cv_score >= removed_cv_score | ||
# The "added" score should be equal or higher than the SFS score | ||
# so the difference between them should be >= tol, which is a | ||
# negative number. | ||
assert (sfs_cv_score - added_cv_score) >= tol | ||
# Because tol is negative the delta between scores should be | ||
# less than or equal to the tolerance, in absolute terms | ||
# the delta is bigger than the tolerance | ||
assert (removed_cv_score - sfs_cv_score) <= tol | ||
|
||
|
||
|
@@ -281,19 +295,27 @@ def test_no_y_validation_model_fit(y): | |
sfs.fit(X, y) | ||
|
||
|
||
def test_forward_neg_tol_error(): | ||
"""Check that we raise an error when tol<0 and direction='forward'""" | ||
def test_tol_sign_depends_on_direction(): | ||
"""Check that we raise an error if the sign of tol and direction do not match""" | ||
X, y = make_regression(n_features=10, random_state=0) | ||
sfs = SequentialFeatureSelector( | ||
LinearRegression(), | ||
n_features_to_select="auto", | ||
direction="forward", | ||
tol=-1e-3, | ||
) | ||
|
||
with pytest.raises(ValueError, match="tol must be positive"): | ||
sfs.fit(X, y) | ||
|
||
sfs = SequentialFeatureSelector( | ||
LinearRegression(), | ||
n_features_to_select="auto", | ||
direction="backward", | ||
tol=+1e-3, | ||
) | ||
with pytest.raises(ValueError, match="tol must be negative"): | ||
sfs.fit(X, y) | ||
|
||
|
||
def test_backward_neg_tol(): | ||
"""Check that SequentialFeatureSelector works negative tol | ||
|
@@ -334,3 +356,28 @@ def test_cv_generator_support(): | |
|
||
sfs = SequentialFeatureSelector(knc, n_features_to_select=5, cv=splits) | ||
sfs.fit(X, y) | ||
|
||
|
||
def test_backwards_doesnt_remove_feature(): | ||
"""All features should be kept. | ||
|
||
Non regression test for #26369 | ||
""" | ||
expected_selected_features = [ | ||
0, | ||
1, | ||
] | ||
rng = np.random.RandomState(0) | ||
n_samples = 500 | ||
X = rng.randn(n_samples, 2) | ||
y = 3 * X[:, 0] - 10 * X[:, 1] | ||
|
||
sfs = SequentialFeatureSelector( | ||
LinearRegression(), | ||
direction="backward", | ||
cv=2, | ||
n_features_to_select="auto", | ||
tol=-0.01, | ||
) | ||
sfs.fit(X, y) | ||
assert_array_equal(sfs.get_support(indices=True), expected_selected_features) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.