Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7836435

Browse files
authored
FIX AffinityPropagation assigning multiple clusters for equal points (#28121)
1 parent fd814a0 commit 7836435

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

doc/whats_new/v1.4.rst

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
.. currentmodule:: sklearn
44

5+
.. _changes_1_4_1:
6+
7+
Version 1.4.1
8+
=============
9+
10+
**In Development**
11+
12+
Changelog
13+
---------
14+
15+
:mod:`sklearn.cluster`
16+
......................
17+
18+
- |Fix| :class:`cluster.AffinityPropagation` now avoids assigning multiple different
19+
clusters for equal points.
20+
:pr:`28121` by :user:`Pietro Peterlongo <pietroppeter>` and
21+
:user:`Yao Xiao <Charlie-XIAO>`.
22+
23+
524
.. _changes_1_4:
625

726
Version 1.4.0

sklearn/cluster/_affinity_propagation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _affinity_propagation(
5353
"All samples have mutually equal similarities. "
5454
"Returning arbitrary cluster center(s)."
5555
)
56-
if preference.flat[0] >= S.flat[n_samples - 1]:
56+
if preference.flat[0] > S.flat[n_samples - 1]:
5757
return (
5858
(np.arange(n_samples), np.arange(n_samples), 0)
5959
if return_n_iter

sklearn/cluster/tests/test_affinity_propagation.py

+11
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,14 @@ def test_sparse_input_for_fit_predict(csr_container):
308308
X = csr_container(rng.randint(0, 2, size=(5, 5)))
309309
labels = af.fit_predict(X)
310310
assert_array_equal(labels, (0, 1, 1, 2, 3))
311+
312+
313+
def test_affinity_propagation_equal_points():
314+
"""Make sure we do not assign multiple clusters to equal points.
315+
316+
Non-regression test for:
317+
https://github.com/scikit-learn/scikit-learn/pull/20043
318+
"""
319+
X = np.zeros((8, 1))
320+
af = AffinityPropagation(affinity="euclidean", damping=0.5, random_state=42).fit(X)
321+
assert np.all(af.labels_ == 0)

0 commit comments

Comments
 (0)