2
2
3
3
import numpy as np
4
4
import pytest
5
- from scipy .sparse import csr_matrix , issparse
5
+ from scipy .sparse import issparse
6
6
7
7
from sklearn .base import BaseEstimator , BiclusterMixin
8
8
from sklearn .cluster import SpectralBiclustering , SpectralCoclustering
19
19
assert_array_almost_equal ,
20
20
assert_array_equal ,
21
21
)
22
+ from sklearn .utils .fixes import CSR_CONTAINERS
22
23
23
24
24
25
class MockBiclustering (BiclusterMixin , BaseEstimator ):
@@ -34,11 +35,12 @@ def get_indices(self, i):
34
35
)
35
36
36
37
37
- def test_get_submatrix ():
38
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
39
+ def test_get_submatrix (csr_container ):
38
40
data = np .arange (20 ).reshape (5 , 4 )
39
41
model = MockBiclustering ()
40
42
41
- for X in (data , csr_matrix (data ), data .tolist ()):
43
+ for X in (data , csr_container (data ), data .tolist ()):
42
44
submatrix = model .get_submatrix (0 , X )
43
45
if issparse (submatrix ):
44
46
submatrix = submatrix .toarray ()
@@ -58,7 +60,8 @@ def _test_shape_indices(model):
58
60
assert len (j_ind ) == n
59
61
60
62
61
- def test_spectral_coclustering (global_random_seed ):
63
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
64
+ def test_spectral_coclustering (global_random_seed , csr_container ):
62
65
# Test Dhillon's Spectral CoClustering on a simple problem.
63
66
param_grid = {
64
67
"svd_method" : ["randomized" , "arpack" ],
@@ -72,7 +75,7 @@ def test_spectral_coclustering(global_random_seed):
72
75
)
73
76
S -= S .min () # needs to be nonnegative before making it sparse
74
77
S = np .where (S < 1 , 0 , S ) # threshold some values
75
- for mat in (S , csr_matrix (S )):
78
+ for mat in (S , csr_container (S )):
76
79
for kwargs in ParameterGrid (param_grid ):
77
80
model = SpectralCoclustering (
78
81
n_clusters = 3 , random_state = global_random_seed , ** kwargs
@@ -87,7 +90,8 @@ def test_spectral_coclustering(global_random_seed):
87
90
_test_shape_indices (model )
88
91
89
92
90
- def test_spectral_biclustering (global_random_seed ):
93
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
94
+ def test_spectral_biclustering (global_random_seed , csr_container ):
91
95
# Test Kluger methods on a checkerboard dataset.
92
96
S , rows , cols = make_checkerboard (
93
97
(30 , 30 ), 3 , noise = 0.5 , random_state = global_random_seed
@@ -100,7 +104,7 @@ def test_spectral_biclustering(global_random_seed):
100
104
"mini_batch" : [True ],
101
105
}
102
106
103
- for mat in (S , csr_matrix (S )):
107
+ for mat in (S , csr_container (S )):
104
108
for param_name , param_values in non_default_params .items ():
105
109
for param_value in param_values :
106
110
model = SpectralBiclustering (
@@ -145,20 +149,22 @@ def _do_bistochastic_test(scaled):
145
149
assert_almost_equal (scaled .sum (axis = 0 ).mean (), scaled .sum (axis = 1 ).mean (), decimal = 1 )
146
150
147
151
148
- def test_scale_normalize (global_random_seed ):
152
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
153
+ def test_scale_normalize (global_random_seed , csr_container ):
149
154
generator = np .random .RandomState (global_random_seed )
150
155
X = generator .rand (100 , 100 )
151
- for mat in (X , csr_matrix (X )):
156
+ for mat in (X , csr_container (X )):
152
157
scaled , _ , _ = _scale_normalize (mat )
153
158
_do_scale_test (scaled )
154
159
if issparse (mat ):
155
160
assert issparse (scaled )
156
161
157
162
158
- def test_bistochastic_normalize (global_random_seed ):
163
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
164
+ def test_bistochastic_normalize (global_random_seed , csr_container ):
159
165
generator = np .random .RandomState (global_random_seed )
160
166
X = generator .rand (100 , 100 )
161
- for mat in (X , csr_matrix (X )):
167
+ for mat in (X , csr_container (X )):
162
168
scaled = _bistochastic_normalize (mat )
163
169
_do_bistochastic_test (scaled )
164
170
if issparse (mat ):
@@ -181,11 +187,12 @@ def test_fit_best_piecewise(global_random_seed):
181
187
assert_array_equal (best , vectors [:2 ])
182
188
183
189
184
- def test_project_and_cluster (global_random_seed ):
190
+ @pytest .mark .parametrize ("csr_container" , CSR_CONTAINERS )
191
+ def test_project_and_cluster (global_random_seed , csr_container ):
185
192
model = SpectralBiclustering (random_state = global_random_seed )
186
193
data = np .array ([[1 , 1 , 1 ], [1 , 1 , 1 ], [3 , 6 , 3 ], [3 , 6 , 3 ]])
187
194
vectors = np .array ([[1 , 0 ], [0 , 1 ], [0 , 0 ]])
188
- for mat in (data , csr_matrix (data )):
195
+ for mat in (data , csr_container (data )):
189
196
labels = model ._project_and_cluster (mat , vectors , n_clusters = 2 )
190
197
assert_almost_equal (v_measure_score (labels , [0 , 0 , 1 , 1 ]), 1.0 )
191
198
0 commit comments