Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.An election for representatives
Every point is a candidate to be an exemplar, a real data point that represents a cluster. Points send messages back and forth:
- “you’d make a good representative for me, better than my other options” (responsibility);
- “here’s how much support I’m getting, so choosing me is reasonable” (availability).
After a few dozen rounds a stable set of exemplars wins, and everyone joins the exemplar most similar to them. You don’t pick k; you set how eager each point is to stand as a candidate (the preference).
Affinity propagation picks real, representative cases: typical flight profiles, canonical fault signatures, representative test scenarios. It works with any similarity measure (DTW, alignment scores, learned similarities, even asymmetric ones), and it doesn’t need random restarts, because the result doesn’t depend on initial centres.
- 1. Exemplars in affinity propagation are…
- 2. The number of clusters is controlled mainly by…
- 3. Affinity propagation needs random restarts?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Messages by hand
Similarities (negative squared distances), with preference −1 on the diagonal:
S = [[−1, −2, −9], [−2, −1, −5], [−9, −5, −1]].
Responsibility r(i, k) = s(i, k) − max over k’ ≠ k of (a(i, k’) + s(i, k’)). With all availabilities 0:
- row 0: r(0, 0) = −1 − max(−2, −9) = 1, r(0, 1) = −2 − (−1) = −1, r(0, 2) = −9 − (−1) = −8;
- row 2: r(2, 0) = −9 − max(−5, −1) = −8, r(2, 1) = −5 − max(−9, −1) = −4, r(2, 2) = −1 − max(−9, −5) = 4.
Availability.
- a(i, k) = min(0, r(k, k) + Σ over i’ ∉ (i, k) of max(0, r(i’, k))).
- Self-availability: a(k, k) = Σ over i’ ≠ k of max(0, r(i’, k)).
Exemplars are the points with a(k, k) + r(k, k) > 0. Each other point joins the exemplar it is most similar to.
Damping blends new and old messages: m ← λ·m_old + (1 − λ)·m_new. With λ = 0.9, old 2 and new 4 give 2.2.
The steps in plain English:
- Compute similarities s(i, k), for example −‖xᵢ − xₖ‖². Set every s(k, k) to the preference (the median or minimum similarity, or tuned).
- Start all responsibilities and availabilities at 0.
- Alternately update all responsibilities, then all availabilities, each damped.
- Stop when the exemplar set stays the same for many iterations. Assign each point to its most similar exemplar.
- 1. With A = 0, r(1, 1) for the S above?
- 2. Damped value with λ = 0.5, old 0, new 6?
- 3. Point k is an exemplar when…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.What the messages optimise
The objective. Affinity propagation searches for exemplar assignments c(i) maximising Σᵢ s(i, c(i)), subject to consistency (if any point chooses k, then k must choose itself). This is the k-medoids objective with a per-exemplar cost equal to −(preference) instead of a fixed k. The messages come from max-sum (min-sum) belief propagation on a factor graph of these constraints (Frey and Dueck, 2007). The graph has many loops, so there is no optimality guarantee, but in practice the solutions are good.
The preference sets k. Each extra exemplar costs about −preference. Low (very negative) preferences make exemplars expensive, so there are few clusters; high ones give many. In exercise 2, on 4 well-separated blobs:
- preferences at or below the 25th percentile of similarities gave exactly 4 clusters (ARI 1.00);
- the common median default gave 4.7–6 on average (ARI 0.81–0.94);
- the 75th percentile gave about 11.
Damping. Undamped updates can oscillate between configurations. Typical λ is 0.5–0.9. In exercise 2 every run converged; λ = 0.7 was fastest (about 45 iterations), while λ = 0.9 was more cautious and slower (60–120). At the median preference the final answer even depended on λ (4.7, 5 or 6 clusters), which reminds you the method is heuristic.
Cost. It keeps two n × n message matrices and does O(n²) work per iteration. That is fine for thousands of points, too much for millions. Sparse similarity graphs reduce both.
from sklearn.cluster import AffinityPropagation
ap = AffinityPropagation(damping=0.7, preference=None, random_state=0).fit(X) # None = median similarity
ap.cluster_centers_indices_, ap.labels_- 1. Affinity propagation's messages come from…
- 2. Raising the preference gives…
- 3. Damping is used to…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Relation to k-medoids. Fix the number of exemplars, and the objective is k-medoids (sum of similarities to exemplars). Affinity propagation instead adds a cost per exemplar and lets the messages trade it off. Like a Lagrangian relaxation, sweeping the preference traces solutions for different k. Because messages consider all points as candidates at once, AP often finds better solutions than single runs of greedy swaps, but many-restart k-medoids can match it (Brusco and Köhn, 2008).
No initialisation. Messages start at zero, so for fixed similarities and damping the result is deterministic. Ties need care: scikit-learn adds tiny noise to break degeneracies, which is why exemplar identities can differ slightly between implementations even when the count agrees.
Asymmetric and non-metric similarities. Nothing in the updates requires s(i, k) = s(k, i) or the triangle inequality. AP can cluster with directed similarities, such as one-sided alignment scores.
Interview questions.
- When would you use affinity propagation? For moderate n with a meaningful similarity, when you want exemplars and no k, for example selecting representative scenarios.
- How do you set the preference? Start from the median (many clusters) or the minimum (few). Use a shared value unless some points should be favoured as exemplars, and check how the cluster count changes over a range.
- 1. With messages starting at zero, AP's result is…
- 2. AP requires similarities to be…
- 3. Memory for AP on n points?
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.How do the preference and the damping shape the result?
History.
- B. Frey and D. Dueck introduced affinity propagation in Science (2007), with exemplars for faces, genes and flight routes.
- M. Brusco and H.-F. Köhn (2008) compared it with many-restart vertex substitution heuristics.
- I. Givoni and B. Frey (2009) extended it to semi-supervised and hierarchical variants.
Exercise 2. 4 well-separated blobs of 40, 3 datasets. Each cell shows mean exemplars, ARI and iterations; the preference is a quantile of all pairwise similarities.
| preference quantile | λ = 0.5 | λ = 0.7 | λ = 0.9 |
|---|---|---|---|
| 0 (minimum) | 4.00, 1.000, 124 | 4.00, 1.000, 44 | 4.00, 1.000, 65 |
| 0.05 | 4.00, 1.000, 57 | 4.00, 1.000, 45 | 4.00, 1.000, 62 |
| 0.25 | 4.00, 1.000, 55 | 4.00, 1.000, 46 | 4.00, 1.000, 68 |
| 0.5 (median, the default) | 6.00, 0.81, 50 | 4.67, 0.94, 50 | 5.00, 0.91, 86 |
| 0.75 | 11.00, 0.47, 43 | 11.00, 0.48, 58 | 10.67, 0.50, 121 |
- The preference controlled k strongly and monotonically. Low quantiles gave exactly the 4 true clusters.
- The median default over-split, and its answer changed with the damping, a sign that it sits near a decision boundary.
- Damping mainly changed speed. No run oscillated on this easy data; λ = 0.7 was fastest.
Affinity propagation in AI, robotics and aerospace
- Representative scenario selection: choosing exemplar test flights or simulation cases from thousands, with a domain-specific similarity.
- Route and trajectory summarisation: Frey and Dueck’s own demo picked representative airline routes.
- Keyframe and place selection in visual mapping, choosing exemplar images to represent locations.
References
- B. J. Frey, D. Dueck (2007). “Clustering by passing messages between data points.” Science 315(5814).
- M. J. Brusco, H.-F. Köhn (2008). “Comment on ‘Clustering by passing messages between data points’.” Science 319(5864).
- I. E. Givoni, B. J. Frey (2009). “A binary variable model for affinity propagation.” Neural Computation 21(6).
- D. Dueck (2009). Affinity Propagation: Clustering Data by Passing Messages. PhD thesis, University of Toronto.
- scikit-learn developers. “AffinityPropagation.” Documentation.