8000 TST Extend tests for `scipy.sparse.*array` in `sklearn/cluster/tests/… · akaashp2000/scikit-learn@f33d6a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f33d6a9

Browse files
Kislovskiyjjerphan
authored andcommitted
TST Extend tests for scipy.sparse.*array in sklearn/cluster/tests/test_bicluster.py (scikit-learn#27093)
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz> Co-authored-by: Julien Jerphanion <git@jjerphan.xyz>
1 parent 83f5fa5 commit f33d6a9

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

sklearn/cluster/tests/test_bicluster.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import numpy as np
44
import pytest
5-
from scipy.sparse import csr_matrix, issparse
5+
from scipy.sparse import issparse
66

77
from sklearn.base import BaseEstimator, BiclusterMixin
88
from sklearn.cluster import SpectralBiclustering, SpectralCoclustering
@@ -19,6 +19,7 @@
1919
assert_array_almost_equal,
2020
assert_array_equal,
2121
)
22+
from sklearn.utils.fixes import CSR_CONTAINERS
2223

2324

2425
class MockBiclustering(BiclusterMixin, BaseEstimator):
@@ -34,11 +35,12 @@ def get_indices(self, i):
3435
)
3536

3637

37-
def test_get_submatrix():
38+
@pytest.mark.parametrize("csr_container", CSR_CONTAINERS)
39+
def test_get_submatrix(csr_container):
3840
data = np.arange(20).reshape(5, 4)
3941
model = MockBiclustering()
4042

41-
for X in (data, csr_matrix(data), data.tolist()):
43+
for X in (data, csr_container(data), data.tolist()):
4244
submatrix = model.get_submatrix(0, X)
4345
if issparse(submatrix):
4446
submatrix = submatrix.toarray()
@@ -58,7 +60,8 @@ def _test_shape_indices(model):
5860
assert len(j_ind) == n
5961

6062

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):
6265
# Test Dhillon's Spectral CoClustering on a simple problem.
6366
param_grid = {
6467
"svd_method": ["randomized", "arpack"],
@@ -72,7 +75,7 @@ def test_spectral_coclustering(global_random_seed):
7275
)
7376
S -= S.min() # needs to be nonnegative before making it sparse
7477
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)):
7679
for kwargs in ParameterGrid(param_grid):
7780
model = SpectralCoclustering(
7881
n_clusters=3, random_state=global_random_seed, **kwargs
@@ -87,7 +90,8 @@ def test_spectral_coclustering(global_random_seed):
8790
_test_shape_indices(model)
8891

8992

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):
9195
# Test Kluger methods on a checkerboard dataset.
9296
S, rows, cols = make_checkerboard(
9397
(30, 30), 3, noise=0.5, random_state=global_random_seed
@@ -100,7 +104,7 @@ def test_spectral_biclustering(global_random_seed):
100104
"mini_batch": [True],
101105
}
102106

103-
for mat in (S, csr_matrix(S)):
107+
for mat in (S, csr_container(S)):
104108
for param_name, param_values in non_default_params.items():
105109
for param_value in param_values:
106110
model = SpectralBiclustering(
@@ -145,20 +149,22 @@ def _do_bistochastic_test(scaled):
145149
assert_almost_equal(scaled.sum(axis=0).mean(), scaled.sum(axis=1).mean(), decimal=1)
146150

147151

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):
149154
generator = np.random.RandomState(global_random_seed)
150155
X = generator.rand(100, 100)
151-
for mat in (X, csr_matrix(X)):
156+
for mat in (X, csr_container(X)):
152157
scaled, _, _ = _scale_normalize(mat)
153158
_do_scale_test(scaled)
154159
if issparse(mat):
155160
assert issparse(scaled)
156161

157162

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):
159165
generator = np.random.RandomState(global_random_seed)
160166
X = generator.rand(100, 100)
161-
for mat in (X, csr_matrix(X)):
167+
for mat in (X, csr_container(X)):
162168
scaled = _bistochastic_normalize(mat)
163169
_do_bistochastic_test(scaled)
164170
if issparse(mat):
@@ -181,11 +187,12 @@ def test_fit_best_piecewise(global_random_seed):
181187
assert_array_equal(best, vectors[:2])
182188

183189

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):
185192
model = SpectralBiclustering(random_state=global_random_seed)
186193
data = np.array([[1, 1, 1], [1, 1, 1], [3, 6, 3], [3, 6, 3]])
187194
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)):
189196
labels = model._project_and_cluster(mat, vectors, n_clusters=2)
190197
assert_almost_equal(v_measure_score(labels, [0, 0, 1, 1]), 1.0)
191198

0 commit comments

Comments
 (0)
0