k-medoids is a partitional clustering algorithm that divides a dataset into k clusters and represents each cluster by a medoid, the actual data point whose total dissimilarity to all other members of the cluster is smallest. In machine learning, it is the robust counterpart of k-means clustering: instead of computing an average that may not exist in the data, it selects real observations as cluster centers.
The best-known algorithm for the problem is Partitioning Around Medoids (PAM), introduced by Kaufman and Rousseeuw, which is why k-medoids is also called partitioning around medoids. Because it needs only a pairwise dissimilarity matrix, k-medoids works with any distance measure, including Manhattan distance, Gower distance for mixed data and edit distance for strings.
How k-medoids chooses its cluster centers
k-medoids minimizes the sum of dissimilarities between each point and the medoid of its cluster. Formally, it searches for a set of k medoids among the n observations so that the total assignment cost is as low as possible, where each point is assigned to its nearest medoid.
PAM works in two phases. The BUILD phase greedily selects initial medoids one by one, each time adding the point that lowers the cost the most. The SWAP phase then evaluates every exchange of a current medoid with a non-medoid point and performs the swap that reduces the cost the most, repeating until no swap helps.
Finding the exact optimum is NP-hard, so PAM and its variants reach a local minimum. In practice, PAM produces high-quality solutions that are usually close to the global optimum, and it is deterministic for a given dissimilarity matrix.
The table below summarizes the main parameters that a practitioner sets or inspects.
| Parameter | Meaning | Practical note |
|---|---|---|
| Number of clusters k | How many medoids to select | Choose with silhouette or domain knowledge |
| Dissimilarity measure | How distance between points is defined | Any metric or non-metric measure works |
| Initialization | How starting medoids are picked | BUILD, k-medoids++ or random |
| Total cost | Sum of point-to-medoid distances | Lower is better for fixed k |
| Medoid indices | Positions of chosen representatives | Directly interpretable records |
Algorithm variants and their scalability
The original PAM evaluates k(n – k) swaps per iteration, each costing O(n – k), which makes it roughly O(k(n – k)²) per iteration. This cost limits classic PAM to a few thousand points, and several variants were developed to scale it.
| Algorithm | Approach | Typical dataset size |
|---|---|---|
| PAM | Exhaustive swap search after greedy BUILD | Up to a few thousand points |
| CLARA | Runs PAM on several random samples | Tens of thousands of points |
| CLARANS | Randomized search over neighbor solutions | Medium to large datasets |
| FastPAM | Evaluates all k swaps for a point at once | About k times faster than PAM |
| Alternating k-medoids | k-means-style assign and update steps | Fast but lower quality |
| BanditPAM | Samples distances with bandit methods | Hundreds of thousands of points |
The alternating method, sometimes called Voronoi iteration, assigns points to medoids and then picks the best medoid inside each cluster. It is quick but often gets stuck in poorer local minima than swap-based methods, so FastPAM is generally the better default.
k-medoids compared with other clustering methods
k-medoids differs from k-means mainly in what a center is and which distances are allowed. k-means minimizes squared Euclidean distance to a mean, while k-medoids minimizes arbitrary dissimilarity to a member of the data.
| Method | Cluster representative | Outlier sensitivity | Distance requirement |
|---|---|---|---|
| k-medoids | Actual data point | Low | Any dissimilarity |
| k-means clustering | Arithmetic mean | High | Squared Euclidean |
| k-medians | Coordinate-wise median | Low | Manhattan distance |
| Hierarchical clustering | None, a tree of merges | Depends on linkage | Any dissimilarity |
| DBSCAN | None, density regions | Labels outliers as noise | Any metric |
The robustness of k-medoids comes from using unsquared distances. A single extreme point pulls a k-means centroid noticeably, whereas a medoid moves only if another real observation becomes a better representative.
Choosing k and preparing the data
The number of clusters is usually selected with the average silhouette width, a measure that originates from the same research line as PAM. Values above about 0.5 indicate a reasonable cluster structure, and values below 0.25 suggest that no strong structure exists.
Data preparation matters as much as the algorithm. Feature scaling is required whenever numeric features have different units, and mixed numeric and categorical data is commonly handled with Gower distance. Because k-medoids stores an n by n matrix, memory grows quadratically: 100,000 points need about 40 GB in 32-bit precision.
| Situation | Recommendation | Reason |
|---|---|---|
| Outliers or heavy tails | Prefer k-medoids over k-means | Medoids resist extreme values |
| Categorical or mixed data | Use k-medoids with Gower distance | No mean exists for categories |
| Very large numeric data | Use k-means or CLARA | Full PAM is too costly |
| Need for real prototypes | Use k-medoids | Each center is an actual record |
| Non-convex cluster shapes | Use DBSCAN or spectral clustering | k-medoids assumes compact groups |
Limitations and common mistakes
k-medoids shares several structural assumptions with k-means, even though it is more robust. Users should know where these assumptions break.
- Fixed k in advance: the algorithm never discovers the number of clusters on its own.
- Compact clusters assumed: elongated or nested shapes are split or merged incorrectly.
- Quadratic memory: the full distance matrix becomes the bottleneck long before compute time.
- Unscaled features: one large-range variable silently dominates every distance.
- Local optima: alternating implementations should be restarted or replaced by FastPAM.
Put simply
Imagine you must pick three meeting points for a large group of friends spread across a city. You could compute an average location. That spot might be in the middle of a lake.
k-medoids does something more practical. It picks real places where friends already live. Each friend then goes to the nearest of these homes. The algorithm keeps swapping homes until total travel is as short as possible. Because it uses real points, one friend living far away does not drag the meeting point off course.
In practice
A regional hospital network wants to understand typical patient pathways in its emergency departments. Each of 6,000 patient records combines age, triage level, diagnosis category, number of tests and length of stay, so the data mixes numeric and categorical fields.
Analysts compute Gower distances between all records and run FastPAM for k from 3 to 10. The silhouette peaks at k = 6, so they keep six clusters. Each medoid is a real, anonymized patient record, which clinicians can read as a concrete profile, such as an elderly patient with a moderate triage level, several imaging tests and a stay of about nine hours. A few unusual multi-day stays do not distort the profiles. The network uses the six profiles to plan staffing and imaging capacity, and clinicians accept the results more readily because every representative is a believable case.