10000 TST use global_dtype in sklearn/cluster/tests/test_affinity_propagati… · scikit-learn/scikit-learn@f647868 · GitHub
[go: up one dir, main page]

Skip to content

Commit f647868

Browse files
jjerphanjeremiedbbglemaitre
authored
TST use global_dtype in sklearn/cluster/tests/test_affinity_propagation.py (#22667)
Co-authored-by: Jérémie du Boisberranger <jeremiedbb@users.noreply.github.com> Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
1 parent bb080aa commit f647868

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

sklearn/cluster/tests/test_affinity_propagation.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
random_state=0,
3030
)
3131

32+
# TODO: AffinityPropagation must preserve dtype for its fitted attributes
33+
# and test must be created accordingly to this new behavior.
34+
# For more details, see: https://github.com/scikit-learn/scikit-learn/issues/11000
3235

33-
def test_affinity_propagation(global_random_seed):
36+
37+
def test_affinity_propagation(global_random_seed, global_dtype):
3438
"""Test consistency of the affinity propagations."""
35-
S = -euclidean_distances(X, squared=True)
39+
S = -euclidean_distances(X.astype(global_dtype, copy=False), squared=True)
3640
preference = np.median(S) * 10
3741
cluster_centers_indices, labels = affinity_propagation(
3842
S, preference=preference, random_state=global_random_seed
@@ -108,11 +112,12 @@ def test_affinity_propagation_precomputed_with_sparse_input():
108112
AffinityPropagation(affinity="precomputed").fit(csr_matrix((3, 3)))
109113

110114

111-
def test_affinity_propagation_predict(global_random_seed):
115+
def test_affinity_propagation_predict(global_random_seed, global_dtype):
112116
# Test AffinityPropagation.predict
113117
af = AffinityPropagation(affinity="euclidean", random_state=global_random_seed)
114-
labels = af.fit_predict(X)
115-
labels2 = af.predict(X)
118+
X_ = X.astype(global_dtype, copy=False)
119+
labels = af.fit_predict(X_)
120+
labels2 = af.predict(X_)
116121
assert_array_equal(labels, labels2)
117122

118123

@@ -131,23 +136,23 @@ def test_affinity_propagation_predict_error():
131136
af.predict(X)
132137

133138

134-
def test_affinity_propagation_fit_non_convergence():
139+
def test_affinity_propagation_fit_non_convergence(global_dtype):
135140
# In case of non-convergence of affinity_propagation(), the cluster
136141
# centers should be an empty array and training samples should be labelled
137142
# as 10000 noise (-1)
138-
X = np.array([[0, 0], [1, 1], [-2, -2]])
143+
X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype)
139144

140145
# Force non-convergence by allowing only a single iteration
141146
af = AffinityPropagation(preference=-10, max_iter=1, random_state=82)
142147

143148
with pytest.warns(ConvergenceWarning):
144149
af.fit(X)
145-
assert_array_equal(np.empty((0, 2)), af.cluster_centers_)
150+
assert_allclose(np.empty((0, 2)), af.cluster_centers_)
146151
assert_array_equal(np.array([-1, -1, -1]), af.labels_)
147152

148153

149-
def test_affinity_propagation_equal_mutual_similarities():
150-
X = np.array([[-1, 1], [1, -1]])
154+
def test_affinity_propagation_equal_mutual_similarities(global_dtype):
155+
X = np.array([[-1, 1], [1, -1]], dtype=global_dtype)
151156
S = -euclidean_distances(X, squared=True)
152157

153158
# setting preference > similarity
@@ -178,10 +183,10 @@ def test_affinity_propagation_equal_mutual_similarities():
178183
assert_array_equal([0, 0], labels)
179184

180185

181-
def test_affinity_propagation_predict_non_convergence():
186+
def test_affinity_propagation_predict_non_convergence(global_dtype):
182187
# In case of non-convergence of affinity_propagation(), the cluster
183188
# centers should be an empty array
184-
X = np.array([[0, 0], [1, 1], [-2, -2]])
189+
X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype)
185190

186191
# Force non-convergence by allowing only a single iteration
187192
with pytest.warns(ConvergenceWarning):
@@ -195,8 +200,10 @@ def test_affinity_propagation_predict_non_convergence():
195200
assert_array_equal(np.array([-1, -1, -1]), y)
196201

197202

198-
def test_affinity_propagation_non_convergence_regressiontest():
199-
X = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]])
203+
def test_affinity_propagation_non_convergence_regressiontest(global_dtype):
204+
X = np.array(
205+
[[1, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 1]], dtype=global_dtype
206+
)
200207
af = AffinityPropagation(affinity="euclidean", max_iter=2, random_state=34)
201208
msg = (
202209
"Affinity propagation did not converge, this model may return degenerate"
@@ -208,17 +215,17 @@ def test_affinity_propagation_non_convergence_regressiontest():
208215
assert_array_equal(np.array([0, 0, 0]), af.labels_)
209216

210217

211-
def test_equal_similarities_and_preferences():
218+
def test_equal_similarities_and_preferences(global_dtype):
212219
# Unequal distances
213-
X = np.array([[0, 0], [1, 1], [-2, -2]])
220+
X = np.array([[0, 0], [1, 1], [-2, -2]], dtype=global_dtype)
214221
S = -euclidean_distances(X, squared=True)
215222

216223
assert not _equal_similarities_and_preferences(S, np.array(0))
217224
assert not _equal_similarities_and_preferences(S, np.array([0, 0]))
218225
assert not _equal_similarities_and_preferences(S, np.array([0, 1]))
219226

220227
# Equal distances
221-
X = np.array([[0, 0], [1, 1]])
228+
X = np.array([[0, 0], [1, 1]], dtype=global_dtype)
222229
S = -euclidean_distances(X, squared=True)
223230

224231
# Different preferences
@@ -251,10 +258,14 @@ def test_affinity_propagation_random_state():
251258

252259

253260
@pytest.mark.parametrize("centers", [csr_matrix(np.zeros((1, 10))), np.zeros((1, 10))])
254-
def test_affinity_propagation_convergence_warning_dense_sparse(centers):
255-
"""Non-regression, see #13334"""
261+
def test_affinity_propagation_convergence_warning_dense_sparse(centers, global_dtype):
262+
"""
263+
Check that having sparse or dense `centers` format should not
264+
influence the convergence.
265+
Non-regression test for gh-13334.
266+
"""
256267
rng = np.random.RandomState(42)
257-
X = rng.rand(40, 10)
268+
X = rng.rand(40, 10).astype(global_dtype, copy=False)
258269
y = (4 * rng.rand(40)).astype(int)
259270
ap = AffinityPropagation(random_state=46)
260271
ap.fit(X, y)
@@ -265,11 +276,11 @@ def test_affinity_propagation_convergence_warning_dense_sparse(centers):
265276

266277

267278
# FIXME; this test is broken with different random states, needs to be revisited
268-
def test_affinity_propagation_float32():
279+
def test_correct_clusters(global_dtype):
269280
# Test to fix incorrect clusters due to dtype change
270281
# (non-regression test for issue #10832)
271282
X = np.array(
272-
[[1, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1]], dtype="float32"
283+
[[1, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1]], dtype=global_dtype
273284
)
274285
afp = AffinityPropagation(preference=1, affinity="precomputed", random_state=0).fit(
275286
X

0 commit comments

Comments
 (0)
0