8000 TST use global_random_seed in sklearn/decomposition/tests/test_kernel… · yuvipanda/scikit-learn@f15ddc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f15ddc0

Browse files
TST use global_random_seed in sklearn/decomposition/tests/test_kernel_pca.py (scikit-learn#30518)
1 parent e520b8b commit f15ddc0

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

sklearn/decomposition/tests/test_kernel_pca.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
from sklearn.utils.validation import _check_psd_eigenvalues
2222

2323

24-
def test_kernel_pca():
24+
def test_kernel_pca(global_random_seed):
2525
"""Nominal test for all solvers and all known kernels + a custom one
2626
2727
It tests
2828
- that fit_transform is equivalent to fit+transform
2929
- that the shapes of transforms and inverse transforms are correct
3030
"""
31-
rng = np.random.RandomState(0)
31+
rng = np.random.RandomState(global_random_seed)
3232
X_fit = rng.random_sample((5, 4))
3333
X_pred = rng.random_sample((2, 4))
3434

@@ -81,15 +81,15 @@ def test_kernel_pca_invalid_parameters():
8181
estimator.fit(np.random.randn(10, 10))
8282

8383

84-
def test_kernel_pca_consistent_transform():
84+
def test_kernel_pca_consistent_transform(global_random_seed):
8585
"""Check robustness to mutations in the original training array
8686
8787
Test that after fitting a kPCA model, it stays independent of any
8888
mutation of the values of the original data object by relying on an
8989
internal copy.
9090
"""
9191
# X_fit_ needs to retain the old, unmodified copy of X
92-
state = np.random.RandomState(0)
92+
state = np.random.RandomState(global_random_seed)
9393
X = state.rand(10, 10)
9494
kpca = KernelPCA(random_state=state).fit(X)
9595
transformed1 = kpca.transform(X)
@@ -100,12 +100,12 @@ def test_kernel_pca_consistent_transform():
100100
assert_array_almost_equal(transformed1, transformed2)
101101

102102

103-
def test_kernel_pca_deterministic_output():
103+
def test_kernel_pca_deterministic_output(global_random_seed):
104104
"""Test that Kernel PCA produces deterministic output
105105
106106
Tests that the same inputs and random state produce the same output.
107107
"""
108-
rng = np.random.RandomState(0)
108+
rng = np.random.RandomState(global_random_seed)
109109
X = rng.rand(10, 10)
110110
eigen_solver = ("arpack", "dense")
111111

@@ -118,13 +118,13 @@ def test_kernel_pca_deterministic_output():
118118

119119

120120
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
121-
def test_kernel_pca_sparse(csr_container):
121+
def test_kernel_pca_sparse(csr_container, global_random_seed):
122122
"""Test that kPCA works on a sparse data input.
123123
124124
Same test as ``test_kernel_pca except inverse_transform`` since it's not
125125
implemented for sparse matrices.
126126
"""
127-
rng = np.random.RandomState(0)
127+
rng = np.random.RandomState(global_random_seed)
128128
X_fit = csr_container(rng.random_sample((5, 4)))
129129
X_pred = csr_container(rng.random_sample((2, 4)))
130130

@@ -157,12 +157,12 @@ def test_kernel_pca_sparse(csr_container):
157157

158158
@pytest.mark.parametrize("solver", ["auto", "dense", "arpack", "randomized"])
159159
@pytest.mark.parametrize("n_features", [4, 10])
160-
def test_kernel_pca_linear_kernel(solver, n_features):
160+
def test_kernel_pca_linear_kernel(solver, n_features, global_random_seed):
161161
"""Test that kPCA with linear kernel is equivalent to PCA for all solvers.
162162
163163
KernelPCA with linear kernel should produce the same output as PCA.
164164
"""
165-
rng = np.random.RandomState(0)
165+
rng = np.random.RandomState(global_random_seed)
166166
X_fit = rng.random_sample((5, n_features))
167167
X_pred = rng.random_sample((2, n_features))
168168

@@ -246,9 +246,9 @@ def test_leave_zero_eig():
246246
assert_array_almost_equal(np.abs(A), np.abs(B))
247247

248248

249-
def test_kernel_pca_precomputed():
249+
def test_kernel_pca_precomputed(global_random_seed):
250250
"""Test that kPCA works with a precomputed kernel, for all solvers"""
251-
rng = np.random.RandomState(0)
251+
rng = np.random.RandomState(global_random_seed)
252252
X_fit = rng.random_sample((5, 4))
253253
X_pred = rng.random_sample((2, 4))
254254

@@ -526,12 +526,12 @@ def test_kernel_pca_feature_names_out():
526526
assert_array_equal([f"kernelpca{i}" for i in range(2)], names)
527527

528528

529-
def test_kernel_pca_inverse_correct_gamma():
529+
def test_kernel_pca_inverse_correct_gamma(global_random_seed):
530530
"""Check that gamma is set correctly when not provided.
531531
532532
Non-regression test for #26280
533533
"""
534-
rng = np.random.RandomState(0)
534+
rng = np.random.RandomState(global_random_seed)
535535
X = rng.random_sample((5, 4))
536536

537537
kwargs = {

0 commit comments

Comments
 (0)
0