-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
TST use global_random_seed in sklearn/linear_model/tests/test_base.py #23465
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
TST use global_random_seed in sklearn/linear_model/tests/test_base.py #23465
Conversation
…ts, which fails on a few seeds now
…fit_intercept=True for test to pass
@svenstehle Thanks for working on this. I think
|
Hi @lorentzenchr thanks for your reply and input. A valid idea to drop the check for
|
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.
LGTM. Thanks for fixing this.
Thanks @lorentzenchr . There are more tests to check in this file though, I will go through them over the weekend to see how many need changes/rework. Depending on that, we can discuss how to proceed. Update: at least one other test is impacted. |
As a conversation point, I added |
@svenstehle Please keep the instructions of #22827 in mind, e.g.
For instance, |
Good catch, thanks for raising this. Addressed |
…hts_greater_than_1d since we are only checking that no value errors are raised
…nse, test will be deprecated in v1.2
…base' of https://github.com/svenstehle/scikit-learn into tst-global_random_seed-sklearn-linear_model-tests-test_base
@@ -233,8 +235,9 @@ def test_linear_regression_sparse_equal_dense(normalize, fit_intercept): | |||
assert_allclose(clf_dense.coef_, clf_sparse.coef_) | |||
|
|||
|
|||
def test_linear_regression_multiple_outcome(random_state=0): | |||
def test_linear_regression_multiple_outcome(global_random_seed): |
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.
We can set a local random state here.
We only check that we fit idependently on different target and the randomness will not impact anything apart of creating the dataset.
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.
Good point, addressed.
# Test multiple-outcome linear regressions | ||
random_state = check_random_state(global_random_seed) | ||
X, y = make_regression(random_state=random_state) |
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.
you can pass directly random_state=0
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.
addressed
# Test multiple-outcome linear regressions with sparse data | ||
random_state = check_random_state(random_state) | ||
random_state = check_random_state(global_random_seed) | ||
X, y = make_sparse_uncorrelated(random_state=random_state) |
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.
I would say that this is the same case than above but for sparse matrix so real need to parametrize the random state here.
# Test multiple-outcome nonnegative linear regressions | ||
random_state = check_random_state(random_state) | ||
random_state = check_random_state(global_random_seed) | ||
X, y = make_sparse_uncorrelated(random_state=random_state) |
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.
same as above
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.
I think that there is a couple of places that we can avoid to parametrize. Otherwise LGTM
…execution order Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
…te' and replace all 'random_state' variables with 'rng'
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.
LGTM
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
Thanks @svenstehle LGTM. Merging |
…scikit-learn#23465) Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com> Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
Reference Issues/PRs
Partially addresses #22827
Task List
FIXME
s for sklearn version 1.2 in code (currently at 1.2.dev0) --> out of scope for this PRValueError
)test_base.py
What does this implement/fix? Explain your changes.
global_random_seed
to all seeded tests insklearn/linear_model/tests/test_base.py
test_linear_regression_sample_weights
assert reg.score(X, y) > 0.5
- scores can be below 0.5 for some cases offit_intercept=False
Long version:
Observations:
for specific seeds, i.e. seeds
(3, 15, 17, 20, 23, 25, 54, 64, 79, 91, 99)
, the testtest_linear_regression_sample_weights
fails iffit_intercept=False
. The test passes iffit_intercept=True
:In the failing cases with one of these seeds and
fit_intercept=False
,reg.score
is lower than 0.5. Settingfit_intercept=True
fixes this for all seeds.Any other combination than
n_samples, n_features = 6, 5
results in more failed tests. E.g. reducing ton_samples, n_features = 5, 4
does not help even withfit_intercept=True
n_samples
needs to remain at a low value, because it will be hard to fit a linear regression to more than a few randomly chosen samples/targets. E.g. every test fails withn_samples = 30
.Possible solutions
remove
assert reg.score(X, y) > 0.5
run only tests with working combinations of
global_random_seed
andfit_intercept
; e.g.mark bad combinations as
xfail
- we have to introduce a gate-keeper fixture for this:Either one does the job and introduces more seeds to the test. I prefer the latter option though. It tells a better story: it tests every combination and safeguards against silently passing combinations in the future.
Comments and opinions are very much appreciated! :)
Any other comments?
I created a task list to track the implementation progress of
global_random_seed
. Also, let's discuss if we tackle the two version 1.2 relatedFIXME
items in this or in another (linked?) PR (how is that kept track of normally?)