Skip to content

Commit 44b1f4d

Browse files
authored
TST use global_random_seed in sklearn/decomposition/tests/test_incremental_pca.py (scikit-learn#31250)
1 parent 0c28c82 commit 44b1f4d

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

sklearn/decomposition/tests/test_incremental_pca.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def test_incremental_pca_sparse(sparse_container):
8787
ipca.partial_fit(X_sparse)
8888

8989

90-
def test_incremental_pca_check_projection():
90+
def test_incremental_pca_check_projection(global_random_seed):
9191
# Test that the projection of data is correct.
92-
rng = np.random.RandomState(1999)
92+
rng = np.random.RandomState(global_random_seed)
9393
n, p = 100, 3
9494
X = rng.randn(n, p) * 0.1
9595
X[:10] += np.array([3, 4, 5])
@@ -108,9 +108,9 @@ def test_incremental_pca_check_projection():
108108
assert_almost_equal(np.abs(Yt[0][0]), 1.0, 1)
109109

110110

111-
def test_incremental_pca_inverse():
111+
def test_incremental_pca_inverse(global_random_seed):
112112
# Test that the projection of data can be inverted.
113-
rng = np.random.RandomState(1999)
113+
rng = np.random.RandomState(global_random_seed)
114114
n, p = 50, 3
115115
X = rng.randn(n, p) # spherical data
116116
X[:, 1] *= 0.00001 # make middle component relatively small
@@ -217,9 +217,9 @@ def test_incremental_pca_num_features_change():
217217
ipca.partial_fit(X2)
218218

219219

220-
def test_incremental_pca_batch_signs():
220+
def test_incremental_pca_batch_signs(global_random_seed):
221221
# Test that components_ sign is stable over batch sizes.
222-
rng = np.random.RandomState(1999)
222+
rng = np.random.RandomState(global_random_seed)
223223
n_samples = 100
224224
n_features = 3
225225
X = rng.randn(n_samples, n_features)
@@ -254,9 +254,9 @@ def test_incremental_pca_partial_fit_small_batch():
254254
assert_allclose(pca.components_, pipca.components_, atol=1e-3)
255255

256256

257-
def test_incremental_pca_batch_values():
257+
def test_incremental_pca_batch_values(global_random_seed):
258258
# Test that components_ values are stable over batch sizes.
259-
rng = np.random.RandomState(1999)
259+
rng = np.random.RandomState(global_random_seed)
260260
n_samples = 100
261261
n_features = 3
262262
X = rng.randn(n_samples, n_features)
@@ -286,9 +286,9 @@ def test_incremental_pca_batch_rank():
286286
assert_allclose_dense_sparse(components_i, components_j)
287287

288288

289-
def test_incremental_pca_partial_fit():
289+
def test_incremental_pca_partial_fit(global_random_seed):
290290
# Test that fit and partial_fit get equivalent results.
291-
rng = np.random.RandomState(1999)
291+
rng = np.random.RandomState(global_random_seed)
292292
n, p = 50, 3
293293
X = rng.randn(n, p) # spherical data
294294
X[:, 1] *= 0.00001 # make middle component relatively small
@@ -316,9 +316,9 @@ def test_incremental_pca_against_pca_iris():
316316
assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1)
317317

318318

319-
def test_incremental_pca_against_pca_random_data():
319+
def test_incremental_pca_against_pca_random_data(global_random_seed):
320320
# Test that IncrementalPCA and PCA are approximate (to a sign flip).
321-
rng = np.random.RandomState(1999)
321+
rng = np.random.RandomState(global_random_seed)
322322
n_samples = 100
323323
n_features = 3
324324
X = rng.randn(n_samples, n_features) + 5 * rng.rand(1, n_features)
@@ -348,10 +348,10 @@ def test_explained_variances():
348348
assert_almost_equal(pca.noise_variance_, ipca.noise_variance_, decimal=prec)
349349

350350

351-
def test_singular_values():
351+
def test_singular_values(global_random_seed):
352352
# Check that the IncrementalPCA output has the correct singular values
353353

354-
rng = np.random.RandomState(0)
354+
rng = np.random.RandomState(global_random_seed)
355355
n_samples = 1000
356356
n_features = 100
357357

@@ -360,7 +360,7 @@ def test_singular_values():
360360
)
361361

362362
pca = PCA(n_components=10, svd_solver="full", random_state=rng).fit(X)
363-
ipca = IncrementalPCA(n_components=10, batch_size=100).fit(X)
363+
ipca = IncrementalPCA(n_components=10, batch_size=150).fit(X)
364364
assert_array_almost_equal(pca.singular_values_, ipca.singular_values_, 2)
365365

366366
# Compare to the Frobenius norm
@@ -382,7 +382,7 @@ def test_singular_values():
382382
)
383383

384384
# Set the singular values and see what we get back
385-
rng = np.random.RandomState(0)
385+
rng = np.random.RandomState(global_random_seed)
386386
n_samples = 100
387387
n_features = 110
388388

0 commit comments

Comments
 (0)