Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Pick real points as cluster centres
k-Means uses the average of each cluster as its centre. A few far-away points drag an average a long way. k-Medoids instead chooses an actual data point, the medoid, as each cluster’s representative: the point with the smallest total distance to the others in its cluster. Like a median, it hardly moves when outliers appear.
Real telemetry has glitches, spikes and dropouts. k-Medoids gives representatives that are real recorded cases (a real flight profile, a real vibration spectrum), and it works with any distance you can compute, for example dynamic time warping between time series or edit distance between fault-code sequences.
- 1. A medoid is…
- 2. Compared with k-means centres, medoids are…
- 3. k-Medoids needs only…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Medoids by hand
One cluster, outlier included: x = 1, 2, 3, 4, 100.
- The mean is 22, nowhere near most of the data.
- The medoid minimises the total absolute distance. In 1-D that is a median, here 3, with cost 2 + 1 + 0 + 1 + 97 = 101.
Two medoids: x = 0, 1, 2, 10, 11 with medoids 1 and 10. Each point’s distance to its nearest medoid is 1, 0, 1, 0, 1, so the cost is 3.
A swap. With medoids 0 and 10, the cost is 0 + 1 + 2 + 0 + 1 = 4. Swapping medoid 0 for point 1 gives cost 3, a change of −1, so PAM makes it.
PAM cost. Each SWAP iteration evaluates k·(n − k) candidate swaps, each over n points. For n = 1000 and k = 10 that is 9900 candidate swaps and about 10⁷ distance lookups per iteration.
The steps in plain English (PAM):
- BUILD: start with the point with the smallest total distance to all others. Then repeatedly add the point that lowers the total cost most, until there are k medoids.
- SWAP: for every medoid and every non-medoid, compute the cost change if they swapped. Make the best swap if it lowers the cost.
- Repeat SWAP until no swap helps. Assign every point to its nearest medoid.
- 1. Medoid of 1, 2, 3, 4, 100 under absolute distance?
- 2. Cost of 0, 1, 2, 10, 11 with medoids 1 and 10?
- 3. Candidate swaps per PAM iteration for n = 100, k = 5?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.PAM, the alternating heuristic, and robustness
The objective is the total distance from each point to its nearest medoid,
J(M) = Σᵢ min over m in M of d(xᵢ, m),
where the medoid set M is restricted to data points. It is not squared, so a point twice as far costs twice as much, not four times.
Alternating (“Voronoi iteration”) k-medoids mimics Lloyd:
- Assign each point to its nearest medoid.
- In each cluster, choose as the new medoid the member with the smallest total distance to the others.
It is cheaper than PAM, but it only moves medoids within their current clusters, so it gets stuck more easily. PAM’s swaps can move a medoid anywhere.
Robustness. A k-means centre is a mean, so one far point shifts it by (distance)/(cluster size). A medoid moves only if a different member becomes more central, which a few far points cannot cause. Exercise 1, 3 blobs plus 15 outliers scattered on a circle of radius 40:
- the worst k-medoids representative was 0.43 from its true centre;
- the worst k-means centre was 3.92 away.
Not unconditionally robust. k is fixed and the objective sums distances. If outliers are numerous, giving them a medoid lowers the cost. Exercise 2 at 20% outliers: PAM found the lower objective that way, while the cheaper alternating method happened to stay on the blobs.
# pip install scikit-learn-extra (or the kmedoids package with FastPAM)
from sklearn_extra.cluster import KMedoids
km = KMedoids(n_clusters=4, metric="manhattan", method="pam", init="build").fit(X)
km.medoid_indices_, km.labels_- 1. Why can PAM escape situations the alternating heuristic cannot?
- 2. k-Medoids' loss uses distances that are…
- 3. With many outliers, k-medoids may…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Breakdown. The mean has breakdown point 0: one arbitrarily bad point can move it arbitrarily far. In 1-D the medoid under absolute distance is a median, whose breakdown point is 50%. In higher dimensions medoids inherit median-like robustness for the location of each cluster. But the partition can still break down when outliers form their own cheap cluster, as exercise 2 showed.
Complexity and scaling.
- PAM SWAP costs O(k(n − k)²) per iteration, plus O(n²) memory for the distance matrix.
- FastPAM (Schubert and Rousseeuw, 2019) shares work across medoids to get O(n²) per iteration, about k times faster.
- CLARA runs PAM on samples and keeps the best.
- CLARANS (Ng and Han) checks random swaps only.
Hardness. The k-medoids problem is the discrete k-median problem, which is NP-hard. PAM is a local search, and swap-based local search for k-median has a constant-factor approximation guarantee (Arya et al., 2004: 5 for single swaps).
Interview questions.
- k-means or k-medoids? k-medoids for non-Euclidean dissimilarities, interpretable exemplars, or outlier-contaminated data. k-means for large n with Euclidean features.
- How do you scale k-medoids? FastPAM / FasterPAM, CLARA on samples, or alternating with k-medoids++ seeding.
- 1. Breakdown point of the 1-D median?
- 2. FastPAM speeds PAM's SWAP up by about a factor of…
- 3. Single-swap local search for k-median approximates the optimum within…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.How many outliers can each method absorb?
History.
- L. Kaufman and P. Rousseeuw introduced PAM (1987) and CLARA in their book Finding Groups in Data (1990).
- R. Ng and J. Han proposed CLARANS (1994) for spatial data mining.
- H.-S. Park and C.-H. Jun (2009) popularised the fast alternating variant.
- E. Schubert and P. Rousseeuw (2019, 2021) gave FastPAM and FasterPAM, making PAM practical for much larger n.
Exercise 2. Four blobs of 50 points, plus outliers scattered at radius 20–50, 4 repetitions. Each cell shows the worst centre error, the ARI on inliers, and the total distance cost.
| outliers | k-means (best of 5) | PAM | alternating k-medoids (best of 5) |
|---|---|---|---|
| 0% | 0.30, 1.000, 243 | 0.51, 0.997, 245 | 0.51, 0.997, 245 |
| 5% | 4.74, 0.426, 964 | 0.49, 0.997, 548 | 0.49, 0.997, 548 |
| 10% | 5.71, 0.243, 1319 | 0.49, 0.997, 914 | 0.49, 0.997, 914 |
| 20% | 6.73, 0.000, 1944 | 3.99, 0.761, 1696 | 1.79, 0.914, 1722 |
- k-means broke at just 5% outliers. It sent centres out to them, and the inlier ARI fell to 0.43.
- k-medoids was unaffected up to 10%.
- At 20%, PAM found the lower objective by spending a medoid on the outliers, so recovery dropped (ARI 0.76). The alternating heuristic’s higher-cost solution recovered the blobs better. A better optimiser does not mean better clusters when the objective itself rewards modelling the outliers.
k-Medoids in AI, robotics and aerospace
- Representative flights or trajectories: clustering with DTW distance and reporting real exemplar trajectories for each mode.
- Fault-sequence clustering with edit distance over maintenance codes.
- Selecting prototype scenarios for simulation test suites: medoids are real, reproducible cases.
References
- L. Kaufman, P. J. Rousseeuw (1990). Finding Groups in Data: An Introduction to Cluster Analysis. Wiley.
- R. T. Ng, J. Han (1994). “Efficient and effective clustering methods for spatial data mining.” Proc. VLDB.
- E. Schubert, P. J. Rousseeuw (2021). “Fast and eager k-medoids clustering: O(k) runtime improvement of the PAM, CLARA, and CLARANS algorithms.” Information Systems 101.
- H.-S. Park, C.-H. Jun (2009). “A simple and fast algorithm for K-medoids clustering.” Expert Systems with Applications 36.
- V. Arya et al. (2004). “Local search heuristics for k-median and facility location problems.” SIAM Journal on Computing 33(3).