10000 Don't use global np.random.seed in tests (#13356) · ogrisel/scikit-learn@e43574e · GitHub
[go: up one dir, main page]

Skip to content

Commit e43574e

Browse files
praths007agramfort
authored andcommitted
Don't use global np.random.seed in tests (scikit-learn#13356)
* initial commit * used random class * fixed failing testcases, reverted __init__.py * fixed failing testcases #2 - passed rng as parameter to ParameterSampler class - changed seed from 0 to 42 (as original) * fixed failing testcases #2 - passed rng as parameter to SparseRandomProjection class * fixed failing testcases #4 - passed rng as parameter to GaussianRandomProjection class * fixed failing test case because of flake 8
1 parent 703991f commit e43574e

File tree

13 files changed

+43
-42
lines changed

13 files changed

+43
-42
lines changed

sklearn/covariance/empirical_covariance_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class EmpiricalCovariance(BaseEstimator):
120120
>>> from sklearn.datasets import make_gaussian_quantiles
121121
>>> real_cov = np.array([[.8, .3],
122122
... [.3, .4]])
123-
>>> np.random.seed(0)
124-
>>> X = np.random.multivariate_normal(mean=[0, 0],
123+
>>> rng = np.random.RandomState(0)
124+
>>> X = rng.multivariate_normal(mean=[0, 0],
125125
... cov=real_cov,
126126
... size=500)
127127
>>> cov = EmpiricalCovariance().fit(X)

sklearn/covariance/robust_covariance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ class MinCovDet(EmpiricalCovariance):
586586
>>> from sklearn.datasets import make_gaussian_quantiles
587587
>>> real_cov = np.array([[.8, .3],
588588
... [.3, .4]])
589-
>>> np.random.seed(0)
590-
>>> X = np.random.multivariate_normal(mean=[0, 0],
589+
>>> rng = np.random.RandomState(0)
590+
>>> X = rng.multivariate_normal(mean=[0, 0],
591591
... cov=real_cov,
592592
... size=500)
593593
>>> cov = MinCovDet(random_state=0).fit(X)

sklearn/covariance/shrunk_covariance_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ class ShrunkCovariance(EmpiricalCovariance):
103103
>>> from sklearn.datasets import make_gaussian_quantiles
104104
>>> real_cov = np.array([[.8, .3],
105105
... [.3, .4]])
106-
>>> np.random.seed(0)
107-
>>> X = np.random.multivariate_normal(mean=[0, 0],
106+
>>> rng = np.random.RandomState(0)
107+
>>> X = rng.multivariate_normal(mean=[0, 0],
108108
... cov=real_cov,
109109
... size=500)
110110
>>> cov = ShrunkCovariance().fit(X)

sklearn/decomposition/tests/test_fastica.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def test_fastica_simple(add_noise=False):
5454
# Test the FastICA algorithm on very simple data.
5555
rng = np.random.RandomState(0)
5656
# scipy.stats uses the global RNG:
57-
np.random.seed(0)
5857
n_samples = 1000
5958
# Generate two sources:
6059
s1 = (2 * np.sin(np.linspace(0, 100, n_samples)) > 0) - 1

sklearn/linear_model/huber.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ class HuberRegressor(LinearModel, RegressorMixin, BaseEstimator):
196196
>>> import numpy as np
197197
>>> from sklearn.linear_model import HuberRegressor, LinearRegression
198198
>>> from sklearn.datasets import make_regression
199-
>>> np.random.seed(0)
199+
>>> rng = np.random.RandomState(0)
200200
>>> X, y, coef = make_regression(
201201
... n_samples=200, n_features=2, noise=4.0, coef=True, random_state=0)
202-
>>> X[:4] = np.random.uniform(10, 20, (4, 2))
203-
>>> y[:4] = np.random.uniform(10, 20, 4)
202+
>>> X[:4] = rng.uniform(10, 20, (4, 2))
203+
>>> y[:4] = rng.uniform(10, 20, 4)
204204
>>> huber = HuberRegressor().fit(X, y)
205205
>>> huber.score(X, y) # doctest: +ELLIPSIS
206206
-7.284608623514573

sklearn/linear_model/ridge.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,9 @@ class Ridge(_BaseRidge, RegressorMixin):
697697
>>> from sklearn.linear_model import Ridge
698698
>>> import numpy as np
699699
>>> n_samples, n_features = 10, 5
700-
>>> np.random.seed(0)
701-
>>> y = np.random.randn(n_samples)
702-
>>> X = np.random.randn(n_samples, n_features)
700+
>>> rng = np.random.RandomState(0)
701+
>>> y = rng.randn(n_samples)
702+
>>> X = rng.randn(n_samples, n_features)
703703
>>> clf = Ridge(alpha=1.0)
704704
>>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
705705
Ridge(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=None,

sklearn/linear_model/sag.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ def sag_solver(X, y, sample_weight=None, loss='log', alpha=1., beta=0.,
201201
>>> import numpy as np
202202
>>> from sklearn import linear_model
203203
>>> n_samples, n_features = 10, 5
204-
>>> np.random.seed(0)
205-
>>> X = np.random.randn(n_samples, n_features)
206-
>>> y = np.random.randn(n_samples)
204+
>>> rng = np.random.RandomState(0)
205+
>>> X = rng.randn(n_samples, n_features)
206+
>>> y = rng.randn(n_samples)
207207
>>> clf = linear_model.Ridge(solver='sag')
208208
>>> clf.fit(X, y)
209209
... #doctest: +NORMALIZE_WHITESPACE

sklearn/linear_model/stochastic_gradient.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,9 +1559,9 @@ class SGDRegressor(BaseSGDRegressor):
15591559
>>> import numpy as np
15601560
>>> from sklearn import linear_model
15611561
>>> n_samples, n_features = 10, 5
1562-
>>> np.random.seed(0)
1563-
>>> y = np.random.randn(n_samples)
1564-
>>> X = np.random.randn(n_samples, n_features)
1562+
>>> rng = np.random.RandomState(0)
1563+
>>> y = rng.randn(n_samples)
1564+
>>> X = rng.randn(n_samples, n_features)
15651565
>>> clf = linear_model.SGDRegressor(max_iter=1000, tol=1e-3)
15661566
>>> clf.fit(X, y)
15671567
... #doctest: +NORMALIZE_WHITESPACE

sklearn/model_selection/_search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ class ParameterSampler:
228228
>>> from sklearn.model_selection import ParameterSampler
229229
>>> from scipy.stats.distributions import expon
230230
>>> import numpy as np
231-
>>> np.random.seed(0)
231+
>>> rng = np.random.RandomState(0)
232232
>>> param_grid = {'a':[1, 2], 'b': expon()}
233-
>>> param_list = list(ParameterSampler(param_grid, n_iter=4))
233+
>>> param_list = list(ParameterSampler(param_grid, n_iter=4,
234+
... random_state=rng))
234235
>>> rounded_list = [dict((k, round(v, 6)) for (k, v) in d.items())
235236
... for d in param_list]
236237
>>> rounded_list == [{'b': 0.89856, 'a': 1},

sklearn/neighbors/binary_tree.pxi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ Examples
302302
Query for k-nearest neighbors
303303
304304
>>> import numpy as np
305-
>>> np.random.seed(0)
306-
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
305+
>>> rng = np.random.RandomState(0)
306+
>>> X = rng.random_sample((10, 3)) # 10 points in 3 dimensions
307307
>>> tree = {BinaryTree}(X, leaf_size=2) # doctest: +SKIP
308308
>>> dist, ind = tree.query(X[:1], k=3) # doctest: +SKIP
309309
>>> print(ind) # indices of 3 closest neighbors
@@ -316,8 +316,8 @@ pickle operation: the tree needs not be rebuilt upon unpickling.
316316
317317
>>> import numpy as np
318318
>>> import pickle
319-
>>> np.random.seed(0)
320-
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
319+
>>> rng = np.random.RandomState(0)
320+
>>> X = rng.random_sample((10, 3)) # 10 points in 3 dimensions
321321
>>> tree = {BinaryTree}(X, leaf_size=2) # doctest: +SKIP
322322
>>> s = pickle.dumps(tree) # doctest: +SKIP
323323
>>> tree_copy = pickle.loads(s) # doctest: +SKIP
@@ -330,8 +330,8 @@ pickle operation: the tree needs not be rebuilt upon unpickling.
330330
Query for neighbors within a given radius
331331
332332
>>> import numpy as np
333-
>>> np.random.seed(0)
334-
>>> X = np.random.random((10, 3)) # 10 points in 3 dimensions
333+
>>> rng = np.random.RandomState(0)
334+
>>> X = rng.random_sample((10, 3)) # 10 points in 3 dimensions
335335
>>> tree = {BinaryTree}(X, leaf_size=2) # doctest: +SKIP
336336
>>> print(tree.query_radius(X[:1], r=0.3, count_only=True))
337337
3
@@ -343,17 +343,17 @@ Query for neighbors within a given radius
343343
Compute a gaussian kernel density estimate:
344344
345345
>>> import numpy as np
346-
>>> np.random.seed(1)
347-
>>> X = np.random.random((100, 3))
346+
>>> rng = np.random.RandomState(42)
347+
>>> X = rng.random_sample((100, 3))
348348
>>> tree = {BinaryTree}(X) # doctest: +SKIP
349349
>>> tree.kernel_density(X[:3], h=0.1, kernel='gaussian')
350350
array([ 6.94114649, 7.83281226, 7.2071716 ])
351351
352352
Compute a two-point auto-correlation function
353353
354354
>>> import numpy as np
355-
>>> np.random.seed(0)
356-
>>> X = np.random.random((30, 3))
355+
>>> rng = np.random.RandomState(0)
356+
>>> X = rng.random_sample((30, 3))
357357
>>> r = np.linspace(0, 1, 5)
358358
>>> tree = {BinaryTree}(X) # doctest: +SKIP
359359
>>> tree.two_point_correlation(X, r)

0 commit comments

Comments
 (0)
0