Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Don't use global np.random.seed in tests #13345

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

Closed
rth opened this issue Mar 1, 2019 · 1 comment
Closed

Don't use global np.random.seed in tests #13345

rth opened this issue Mar 1, 2019 · 1 comment
Labels
Easy Well-defined and straightforward way to resolve good first issue Easy with clear instructions to resolve help wanted

Comments

@rth
Copy link
Member

rth commented Mar 1, 2019

A few tests and doctests are currently setting np.random.seed globally. Unless I'm missing something, we should avoid that as it has the potential to both

  • affect other tests (if those don't set the random seed explicitly
  • hurt reproducibility: e.g. tests that don't set a random_state may behave deterministically because of this when run as part of the full test suite (given that test order is deterministic now) but yield another result when run separately.
sklearn/random_projection.py
591:    >>> np.random.seed(42)

sklearn/covariance/empirical_covariance_.py
123:    >>> np.random.seed(0)

sklearn/covariance/shrunk_covariance_.py
106:    >>> np.random.seed(0)
374:    >>> np.random.seed(0)

sklearn/covariance/robust_covariance.py
589:    >>> np.random.seed(0)

sklearn/linear_model/huber.py
199:    >>> np.random.seed(0)

sklearn/linear_model/sag.py
204:    >>> np.random.seed(0)

sklearn/linear_model/ridge.py
643:    >>> np.random.seed(0)

sklearn/linear_model/stochastic_gradient.py
1562:    >>> np.random.seed(0)

sklearn/model_selection/_search.py
231:    >>> np.random.seed(0)

sklearn/__init__.py
95:    np.random.seed(_random_seed)

sklearn/svm/classes.py
877:    >>> np.random.seed(0)
1004:    >>> np.random.seed(0)

sklearn/neighbors/binary_tree.pxi
303:    >>> np.random.seed(0)
317:    >>> np.random.seed(0)
331:    >>> np.random.seed(0)
344:    >>> np.random.seed(1)
353:    >>> np.random.seed(0)

sklearn/decomposition/tests/test_fastica.py
57:    np.random.seed(0)

sklearn/neighbors/tests/test_ball_tree.py
155:    np.random.seed(0)
@jnothman
Copy link
Member

jnothman commented Mar 1, 2019

So the right idiom is often to write rng = np.RandomState(42) then use that whenever randomness is used

@rth rth added Easy Well-defined and straightforward way to resolve good first issue Easy with clear instructions to resolve help wanted labels Mar 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy Well-defined and straightforward way to resolve good first issue Easy with clear instructions to resolve help wanted
Projects
None yet
Development

No branches or pull requests

3 participants