29
29
random_state = 0 ,
30
30
)
31
31
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
32
35
33
- def test_affinity_propagation (global_random_seed ):
36
+
37
+ def test_affinity_propagation (global_random_seed , global_dtype ):
34
38
"""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 )
36
40
preference = np .median (S ) * 10
37
41
cluster_centers_indices , labels = affinity_propagation (
38
42
S , preference = preference , random_state = global_random_seed
@@ -108,11 +112,12 @@ def test_affinity_propagation_precomputed_with_sparse_input():
108
112
AffinityPropagation (affinity = "precomputed" ).fit (csr_matrix ((3 , 3 )))
109
113
110
114
111
- def test_affinity_propagation_predict (global_random_seed ):
115
+ def test_affinity_propagation_predict (global_random_seed , global_dtype ):
112
116
# Test AffinityPropagation.predict
113
117
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_ )
116
121
assert_array_equal (labels , labels2 )
117
122
118
123
@@ -131,23 +136,23 @@ def test_affinity_propagation_predict_error():
131
136
af .predict (X )
132
137
133
138
134
- def test_affinity_propagation_fit_non_convergence ():
139
+ def test_affinity_propagation_fit_non_convergence (global_dtype ):
135
140
# In case of non-convergence of affinity_propagation(), the cluster
136
141
# centers should be an empty array and training samples should be labelled
137
142
# 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 )
139
144
140
145
# Force non-convergence by allowing only a single iteration
141
146
af = AffinityPropagation (preference = - 10 , max_iter = 1 , random_state = 82 )
142
147
143
148
with pytest .warns (ConvergenceWarning ):
144
149
af .fit (X )
145
- assert_array_equal (np .empty ((0 , 2 )), af .cluster_centers_ )
150
+ assert_allclose (np .empty ((0 , 2 )), af .cluster_centers_ )
146
151
assert_array_equal (np .array ([- 1 , - 1 , - 1 ]), af .labels_ )
147
152
148
153
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 )
151
156
S = - euclidean_distances (X , squared = True )
152
157
153
158
# setting preference > similarity
@@ -178,10 +183,10 @@ def test_affinity_propagation_equal_mutual_similarities():
178
183
assert_array_equal ([0 , 0 ], labels )
179
184
180
185
181
- def test_affinity_propagation_predict_non_convergence ():
186
+ def test_affinity_propagation_predict_non_convergence (global_dtype ):
182
187
# In case of non-convergence of affinity_propagation(), the cluster
183
188
# 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 )
185
190
186
191
# Force non-convergence by allowing only a single iteration
187
192
with pytest .warns (ConvergenceWarning ):
@@ -195,8 +200,10 @@ def test_affinity_propagation_predict_non_convergence():
195
200
assert_array_equal (np .array ([- 1 , - 1 , - 1 ]), y )
196
201
197
202
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
+ )
200
207
af = AffinityPropagation (affinity = "euclidean" , max_iter = 2 , random_state = 34 )
201
208
msg = (
202
209
"Affinity propagation did not converge, this model may return degenerate"
@@ -208,17 +215,17 @@ def test_affinity_propagation_non_convergence_regressiontest():
208
215
assert_array_equal (np .array ([0 , 0 , 0 ]), af .labels_ )
209
216
210
217
211
- def test_equal_similarities_and_preferences ():
218
+ def test_equal_similarities_and_preferences (global_dtype ):
212
219
# 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 )
214
221
S = - euclidean_distances (X , squared = True )
215
222
216
223
assert not _equal_similarities_and_preferences (S , np .array (0 ))
217
224
assert not _equal_similarities_and_preferences (S , np .array ([0 , 0 ]))
218
225
assert not _equal_similarities_and_preferences (S , np .array ([0 , 1 ]))
219
226
220
227
# Equal distances
221
- X = np .array ([[0 , 0 ], [1 , 1 ]])
228
+ X = np .array ([[0 , 0 ], [1 , 1 ]], dtype = global_dtype )
222
229
S = - euclidean_distances (X , squared = True )
223
230
224
231
# Different preferences
@@ -251,10 +258,14 @@ def test_affinity_propagation_random_state():
251
258
252
259
253
260
@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
+ """
256
267
rng = np .random .RandomState (42 )
257
- X = rng .rand (40 , 10 )
268
+ X = rng .rand (40 , 10 ). astype ( global_dtype , copy = False )
258
269
y = (4 * rng .rand (40 )).astype (int )
259
270
ap = AffinityPropagation (random_state = 46 )
260
271
ap .fit (X , y )
@@ -265,11 +276,11 @@ def test_affinity_propagation_convergence_warning_dense_sparse(centers):
265
276
266
277
267
278
# 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 ):
269
280
# Test to fix incorrect clusters due to dtype change
270
281
# (non-regression test for issue #10832)
271
282
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
273
284
)
274
285
afp = AffinityPropagation (preference = 1 , affinity = "precomputed" , random_state = 0 ).fit (
275
286
X
0 commit comments