8000 FIX AffinityPropagation fix results with float32 input (#17995) · scikit-learn/scikit-learn@3cb3d41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cb3d41

Browse files
authored
FIX AffinityPropagation fix results with float32 input (#17995)
1 parent 6ddaaed commit 3cb3d41

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/whats_new/v0.24.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Changelog
7070
`init_size_`, are deprecated and will be removed in 0.26. :pr:`17864` by
7171
:user:`Jérémie du Boisberranger <jeremiedbb>`.
7272

73+
- |Fix| Fixed a bug in :class:`cluster.AffinityPropagation`, that
74+
gives incorrect clusters when the array dtype is float32.
75+
:pr:`17995` by :user:`Thomaz Santana <Wikilicious>` and :user:`Amanda Dsouza <amy12xx>`.
76+
7377
:mod:`sklearn.covariance`
7478
.........................
7579

sklearn/cluster/_affinity_propagation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def affinity_propagation(S, *, preference=None, convergence_iter=15,
162162
tmp = np.zeros((n_samples, n_samples))
163163

164164
# Remove degeneracies
165-
S += ((np.finfo(np.double).eps * S + np.finfo(np.double).tiny * 100) *
165+
S += ((np.finfo(S.dtype).eps * S + np.finfo(S.dtype).tiny * 100) *
166166
random_state.randn(n_samples, n_samples))
167167

168168
# Execute parallel affinity propagation updates

sklearn/cluster/tests/test_affinity_propagation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,16 @@ def test_affinity_propagation_convergence_warning_dense_sparse(centers):
231231
assert_array_equal(ap.predict(X),
232232
np.zeros(X.shape[0], dtype=int))
233233
assert len(record) == 0
234+
235+
236+
def test_affinity_propagation_float32():
237+
# Test to fix incorrect clusters due to dtype change
238+
# (non-regression test for issue #10832)
239+
X = np.array([[1, 0, 0, 0],
240+
[0, 1, 1, 0],
241+
[0, 1, 1, 0],
242+
[0, 0, 0, 1]], dtype='float32')
243+
afp = AffinityPropagation(preference=1, affinity='precomputed',
244+
random_state=0).fit(X)
245+
expected = np.array([0, 1, 1, 2])
246+
assert_array_equal(afp.labels_, expected)

0 commit comments

Comments
 (0)
0