Foundations to Frontier
Tests

k-Medoids (PAM)

k-Medoids clusters data around k representative data points, the medoids, chosen to minimise the sum of distances (not squared distances) from each point to its nearest medoid. Because the representatives are real points and the loss is not squared, it works with any dissimilarity (Manhattan, edit distance, dynamic time warping) and is much less affected by outliers than k-means. PAM (Partitioning Around Medoids; Kaufman and Rousseeuw) builds an initial set greedily and then makes the best medoid-for-non-medoid swap until none helps. It is slower than k-means, so CLARA, CLARANS and FastPAM exist for larger data. This page covers the objective, BUILD and SWAP, the cheaper alternating heuristic, cost, and robustness. In the exercises you implement PAM and compare it with k-means under outliers; then you measure how cluster recovery degrades as the outlier fraction grows.

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.

k-medoids: representatives that ignore outliers
worst distance from a true blob centre to its nearest representative: k-means (✕) 0.88 · k-medoids (●) 0.37
Dashed rings mark the true blob centres. With no outliers both methods agree. As outliers are added, k-means first drifts and then sends a whole centre out to the outliers, because squared distances make far points expensive. k-medoids uses plain distances and must pick real data points, so it stays on the blobs much longer. It is not immune: in exercise 2, with 20% outliers, PAM sometimes spent a medoid on them because that lowered its cost.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A medoid is…
  2. 2. Compared with k-means centres, medoids are…
  3. 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):

  1. 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.
  2. SWAP: for every medoid and every non-medoid, compute the cost change if they swapped. Make the best swap if it lowers the cost.
  3. Repeat SWAP until no swap helps. Assign every point to its nearest medoid.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Medoid of 1, 2, 3, 4, 100 under absolute distance?
  2. 2. Cost of 0, 1, 2, 10, 11 with medoids 1 and 10?
  3. 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:

  1. Assign each point to its nearest medoid.
  2. 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_
🐍 Exercise 1: PAM
Implement the distance matrix, the k-medoids cost, PAM BUILD and SWAP (vectorised over candidates), and the alternating heuristic; compare representatives with k-means on blobs with scattered outliers.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why can PAM escape situations the alternating heuristic cannot?
  2. 2. k-Medoids' loss uses distances that are…
  3. 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.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Breakdown point of the 1-D median?
  2. 2. FastPAM speeds PAM's SWAP up by about a factor of…
  3. 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.
🐍 Exercise 2 (research): robustness to outliers
On four blobs with 0–20% scattered outliers, compare k-means, PAM and alternating k-medoids by worst centre error, ARI on inliers, and the sum-of-distances cost.

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

  1. L. Kaufman, P. J. Rousseeuw (1990). Finding Groups in Data: An Introduction to Cluster Analysis. Wiley.
  2. R. T. Ng, J. Han (1994). “Efficient and effective clustering methods for spatial data mining.” Proc. VLDB.
  3. 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.
  4. H.-S. Park, C.-H. Jun (2009). “A simple and fast algorithm for K-medoids clustering.” Expert Systems with Applications 36.
  5. V. Arya et al. (2004). “Local search heuristics for k-median and facility location problems.” SIAM Journal on Computing 33(3).