Affinity propagation does not ask for a number of clusters. Every point starts as a candidate exemplar, and points exchange two messages until a set of exemplars emerges. Each point then joins the exemplar it is most similar to.
Implement affinity_propagation(X, preference=None, damping=0.5, max_iter=200). Use similarity S(i, k) = negative squared Euclidean distance. Put preference on the diagonal of S, or use the median of the off-diagonal similarities when it is None.
For each iteration:
Points with A(k, k) + R(k, k) > 0 are exemplars. Label the exemplars 0, 1, 2 in index order. Each exemplar gets its own label, and every other point gets the label of its most similar exemplar.
damping must satisfy 0.5 ≤ damping < 1, otherwise raise ValueError.[0].X.Build S once with broadcasting, then fill its diagonal with the preference using np.fill_diagonal.
For each row, the max over k' ≠ k can be found from the top two values of A + S in that row.
No results yet
Run your code to check it against the examples, or submit to run every test.