21
21
from sklearn .utils .validation import _check_psd_eigenvalues
22
22
23
23
24
- def test_kernel_pca ():
24
+ def test_kernel_pca (global_random_seed ):
25
25
"""Nominal test for all solvers and all known kernels + a custom one
26
26
27
27
It tests
28
28
- that fit_transform is equivalent to fit+transform
29
29
- that the shapes of transforms and inverse transforms are correct
30
30
"""
31
- rng = np .random .RandomState (0 )
31
+ rng = np .random .RandomState (global_random_seed )
32
32
X_fit = rng .random_sample ((5 , 4 ))
33
33
X_pred = rng .random_sample ((2 , 4 ))
34
34
@@ -81,15 +81,15 @@ def test_kernel_pca_invalid_parameters():
81
81
estimator .fit (np .random .randn (10 , 10 ))
82
82
83
83
84
- def test_kernel_pca_consistent_transform ():
84
+ def test_kernel_pca_consistent_transform (global_random_seed ):
85
85
"""Check robustness to mutations in the original training array
86
86
87
87
Test that after fitting a kPCA model, it stays independent of any
88
88
mutation of the values of the original data object by relying on an
89
89
internal copy.
90
90
"""
91
91
# 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 )
93
93
X = state .rand (10 , 10 )
94
94
kpca = KernelPCA (random_state = state ).fit (X )
95
95
transformed1 = kpca .transform (X )
@@ -100,12 +100,12 @@ def test_kernel_pca_consistent_transform():
100
100
assert_array_almost_equal (transformed1 , transformed2 )
101
101
102
102
103
- def test_kernel_pca_deterministic_output ():
103
+ def test_kernel_pca_deterministic_output (global_random_seed ):
104
104
"""Test that Kernel PCA produces deterministic output
105
105
106
106
Tests that the same inputs and random state produce the same output.
107
107
"""
108
- rng = np .random .RandomState (0 )
108
+ rng = np .random .RandomState (global_random_seed )
109
109
X = rng .rand (10 , 10 )
110
110
eigen_solver = ("arpack" , "dense" )
111
111
@@ -118,13 +118,13 @@ def test_kernel_pca_deterministic_output():
118
118
119
119
120
120
@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 ):
122
122
"""Test that kPCA works on a sparse data input.
123
123
124
124
Same test as ``test_kernel_pca except inverse_transform`` since it's not
125
125
implemented for sparse matrices.
126
126
"""
127
- rng = np .random .RandomState (0 )
127
+ rng = np .random .RandomState (global_random_seed )
128
128
X_fit = csr_container (rng .random_sample ((5 , 4 )))
129
129
X_pred = csr_container (rng .random_sample ((2 , 4 )))
130
130
@@ -157,12 +157,12 @@ def test_kernel_pca_sparse(csr_container):
157
157
158
158
@pytest .mark .parametrize ("solver" , ["auto" , "dense" , "arpack" , "randomized" ])
159
159
@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 ):
161
161
"""Test that kPCA with linear kernel is equivalent to PCA for all solvers.
162
162
163
163
KernelPCA with linear kernel should produce the same output as PCA.
164
164
"""
165
- rng = np .random .RandomState (0 )
165
+ rng = np .random .RandomState (global_random_seed )
166
166
X_fit = rng .random_sample ((5 , n_features ))
167
167
X_pred = rng .random_sample ((2 , n_features ))
168
168
@@ -246,9 +246,9 @@ def test_leave_zero_eig():
246
246
assert_array_almost_equal (np .abs (A ), np .abs (B ))
247
247
248
248
249
- def test_kernel_pca_precomputed ():
249
+ def test_kernel_pca_precomputed (global_random_seed ):
250
250
"""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 )
252
252
X_fit = rng .random_sample ((5 , 4 ))
253
253
X_pred = rng .random_sample ((2 , 4 ))
254
254
@@ -526,12 +526,12 @@ def test_kernel_pca_feature_names_out():
526
526
assert_array_equal ([f"kernelpca{ i } " for i in range (2 )], names )
527
527
528
528
529
- def test_kernel_pca_inverse_correct_gamma ():
529
+ def test_kernel_pca_inverse_correct_gamma (global_random_seed ):
530
530
"""Check that gamma is set correctly when not provided.
531
531
532
532
Non-regression test for #26280
533
533
"""
534
- rng = np .random .RandomState (0 )
534
+ rng = np .random .RandomState (global_random_seed )
535
535
X = rng .random_sample ((5 , 4 ))
536
536
537
537
kwargs = {
0 commit comments