-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
TST suppress convergence warnings in coordinate descent #20144
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3a18719
Fix coordinate descent convergence warnings (Fixes #4780)
norbusan 3b7a6fe
Fix long lines
norbusan 59cdd02
Better layout for long lines breaking
norbusan d9ca263
Factor out prediction computation, add comment about suppress()
norbusan 85eabb8
Merge remote-tracking branch 'upstream/main' into issue4780
norbusan c44a3c6
various updates concerning warnings
norbusan 46d50cd
Style fix and duplication fix
norbusan f9bc37d
Use gap <= tol for check
norbusan 8edf0ea
Merge remote-tracking branch 'upstream/main' into issue4780
adrinjalali 334138a
remove failing convergence warning test
adrinjalali 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 |
---|---|---|
|
@@ -94,13 +94,17 @@ def test_lasso_zero(): | |
# Check that the lasso can handle zero data without crashing | ||
X = [[0], [0], [0]] | ||
y = [0, 0, 0] | ||
clf = Lasso(alpha=0.1).fit(X, y) | ||
# _cd_fast.pyx tests for gap < tol, but here we get 0.0 < 0.0 | ||
# should probably be changed to gap <= tol ? | ||
adrinjalali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with ignore_warnings(category=ConvergenceWarning): | ||
adrinjalali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clf = Lasso(alpha=0.1).fit(X, y) | ||
pred = clf.predict([[1], [2], [3]]) | ||
assert_array_almost_equal(clf.coef_, [0]) | ||
assert_array_almost_equal(pred, [0, 0, 0]) | ||
assert_almost_equal(clf.dual_gap_, 0) | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore::sklearn.exceptions.ConvergenceWarning") | ||
def test_enet_nonfinite_params(): | ||
# Check ElasticNet throws ValueError when dealing with non-finite parameter | ||
# values | ||
|
@@ -358,6 +362,7 @@ def _scale_alpha_inplace(estimator, n_samples): | |
estimator.set_params(alpha=alpha) | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore::sklearn.exceptions.ConvergenceWarning") | ||
@pytest.mark.parametrize( | ||
"LinearModel, params", | ||
[ | ||
|
@@ -593,14 +598,16 @@ def test_uniform_targets(): | |
for model in models_single_task: | ||
for y_values in (0, 5): | ||
y1.fill(y_values) | ||
assert_array_equal(model.fit(X_train, y1).predict(X_test), y1) | ||
with ignore_warnings(category=ConvergenceWarning): | ||
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. since we ignore the warning all the time, we could decorate the test function as well. |
||
assert_array_equal(model.fit(X_train, y1).predict(X_test), y1) | ||
norbusan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_array_equal(model.alphas_, [np.finfo(float).resolution] * 3) | ||
|
||
for model in models_multi_task: | ||
for y_values in (0, 5): | ||
y2[:, 0].fill(y_values) | ||
y2[:, 1].fill(2 * y_values) | ||
assert_array_equal(model.fit(X_train, y2).predict(X_test), y2) | ||
with ignore_warnings(category=ConvergenceWarning): | ||
assert_array_equal(model.fit(X_train, y2).predict(X_test), y2) | ||
assert_array_equal(model.alphas_, [np.finfo(float).resolution] * 3) | ||
|
||
|
||
|
@@ -686,7 +693,7 @@ def test_multitask_enet_and_lasso_cv(): | |
|
||
X, y, _, _ = build_dataset(n_targets=3) | ||
clf = MultiTaskElasticNetCV( | ||
n_alphas=10, eps=1e-3, max_iter=100, l1_ratio=[0.3, 0.5], tol=1e-3, cv=3 | ||
n_alphas=10, eps=1e-3, max_iter=200, l1_ratio=[0.3, 0.5], tol=1e-3, cv=3 | ||
) | ||
clf.fit(X, y) | ||
assert 0.5 == clf.l1_ratio_ | ||
|
@@ -696,7 +703,7 @@ def test_multitask_enet_and_lasso_cv(): | |
assert (2, 10) == clf.alphas_.shape | ||
|
||
X, y, _, _ = build_dataset(n_targets=3) | ||
clf = MultiTaskLassoCV(n_alphas=10, eps=1e-3, max_iter=100, tol=1e-3, cv=3) | ||
clf = MultiTaskLassoCV(n_alphas=10, eps=1e-3, max_iter=500, tol=1e-3, cv=3) | ||
clf.fit(X, y) | ||
assert (3, X.shape[1]) == clf.coef_.shape | ||
assert (3,) == clf.intercept_.shape | ||
|
@@ -945,7 +952,8 @@ def test_check_input_false(): | |
# dtype is still cast in _preprocess_data to X's dtype. So the test should | ||
# pass anyway | ||
X = check_array(X, order="F", dtype="float32") | ||
clf.fit(X, y, check_input=False) | ||
with ignore_warnings(category=ConvergenceWarning): | ||
adrinjalali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clf.fit(X, y, check_input=False) | ||
# With no input checking, providing X in C order should result in false | ||
# computation | ||
X = check_array(X, order="C", dtype="float64") | ||
|
@@ -1061,6 +1069,7 @@ def test_enet_float_precision(): | |
) | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore::sklearn.exceptions.ConvergenceWarning") | ||
def test_enet_l1_ratio(): | ||
# Test that an error message is raised if an estimator that | ||
# uses _alpha_grid is called with l1_ratio=0 | ||
|
@@ -1129,8 +1138,6 @@ def test_warm_start_multitask_lasso(): | |
[ | ||
(Lasso, 1, dict(precompute=True)), | ||
(Lasso, 1, dict(precompute=False)), | ||
(MultiTaskLasso, 2, dict()), | ||
(MultiTaskLasso, 2, dict()), | ||
], | ||
) | ||
def test_enet_coordinate_descent(klass, n_classes, kwargs): | ||
|
@@ -1474,6 +1481,7 @@ def test_enet_sample_weight_does_not_overwrite_sample_weight(check_input): | |
assert_array_equal(sample_weight, sample_weight_1_25) | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore::sklearn.exceptions.ConvergenceWarning") | ||
@pytest.mark.parametrize("ridge_alpha", [1e-1, 1.0, 1e6]) | ||
def test_enet_ridge_consistency(ridge_alpha): | ||
# Check that ElasticNet(l1_ratio=0) converges to the same solution as Ridge | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I do not understand why it would be the case in this test. By default the tolerance is 1e-4:
This test does not override the default. The same is true for
MultiTaskLasso
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok I understand, this is because we rescale the tolerance by a data dependent quantity which is 0 in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is indeed sub-optimal -
y == 0
are valid options. What about the following change to keeptol
as is if it would become0
(similar code for all the different coordinate descent function instances):Then we could also return to the previous (
gap < tol
).