Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0f00fb7

Browse filesBrowse files
committed
Make positive tolerance for backwards direction an error
1 parent 2ef9915 commit 0f00fb7
Copy full SHA for 0f00fb7

File tree

Expand file treeCollapse file tree

2 files changed

+30
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-6
lines changed

‎sklearn/feature_selection/_sequential.py

Copy file name to clipboardExpand all lines: sklearn/feature_selection/_sequential.py
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ def fit(self, X, y=None):
257257
elif isinstance(self.n_features_to_select, Real):
258258
self.n_features_to_select_ = int(n_features * self.n_features_to_select)
259259

260-
if self.tol is not None and self.tol < 0 and self.direction == "forward":
261-
raise ValueError("tol must be positive when doing forward selection")
260+
if self.tol is not None:
261+
if self.tol < 0 and self.direction == "forward":
262+
raise ValueError("tol must be positive when doing forward selection")
263+
if self.tol > 0 and self.direction == "backward":
264+
raise ValueError("tol must be negative when doing backward selection")
262265

263266
cv = check_cv(self.cv, y, classifier=is_classifier(self.estimator))
264267

‎sklearn/feature_selection/tests/test_sequential.py

Copy file name to clipboardExpand all lines: sklearn/feature_selection/tests/test_sequential.py
+25-4Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def test_n_features_to_select_auto(direction, max_features_to_select):
5555
"""
5656
n_features = 10
5757
tol = 1e-3
58+
if direction == "backward":
59+
tol *= -1
60+
5861
X, y = make_regression(n_features=n_features, random_state=0)
5962
sfs = SequentialFeatureSelector(
6063
LinearRegression(),
@@ -96,6 +99,8 @@ def test_n_features_to_select_stopping_criterion(direction):
9699
X, y = make_regression(n_features=50, n_informative=10, random_state=0)
97100

98101
tol = 1e-3
102+
if direction == "backward":
103+
tol *= -1
99104

100105
sfs = SequentialFeatureSelector(
101106
LinearRegression(),
@@ -131,7 +136,15 @@ def test_n_features_to_select_stopping_criterion(direction):
131136
assert (sfs_cv_score - added_cv_score) <= tol
132137
assert (sfs_cv_score - removed_cv_score) >= tol
133138
else:
134-
assert (added_cv_score - sfs_cv_score) <= tol
139+
assert sfs_cv_score <= added_cv_score
140+
assert sfs_cv_score >= removed_cv_score
141+
# The "added" score should be equal or higher than the SFS score
142+
# so the difference between them should be >= tol, which is a
143+
# negative number.
144+
assert (sfs_cv_score - added_cv_score) >= tol
145+
# Because tol is negative the delta between scores should be
146+
# less than or equal to the tolerance, in absolute terms
147+
# the delta is bigger than the tolerance
135148
assert (removed_cv_score - sfs_cv_score) <= tol
136149

137150

@@ -282,19 +295,27 @@ def test_no_y_validation_model_fit(y):
282295
sfs.fit(X, y)
283296

284297

285-
def test_forward_neg_tol_error():
286-
"""Check that we raise an error when tol<0 and direction='forward'"""
298+
def test_tol_sign_depends_on_direction():
299+
"""Check that we raise an error when the sign of tol and the direction do not match"""
287300
X, y = make_regression(n_features=10, random_state=0)
288301
sfs = SequentialFeatureSelector(
289302
LinearRegression(),
290303
n_features_to_select="auto",
291304
direction="forward",
292305
tol=-1e-3,
293306
)
294-
295307
with pytest.raises(ValueError, match="tol must be positive"):
296308
sfs.fit(X, y)
297309

310+
sfs = SequentialFeatureSelector(
311+
LinearRegression(),
312+
n_features_to_select="auto",
313+
direction="backward",
314+
tol=+1e-3,
315+
)
316+
with pytest.raises(ValueError, match="tol must be negative"):
317+
sfs.fit(X, y)
318+
298319

299320
def test_backward_neg_tol():
300321
"""Check that SequentialFeatureSelector works negative tol

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.