k-medoids

Also known as: partitioning around medoids

On this page 7

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.

ParameterMeaningPractical note
Number of clusters kHow many medoids to selectChoose with silhouette or domain knowledge
Dissimilarity measureHow distance between points is definedAny metric or non-metric measure works
InitializationHow starting medoids are pickedBUILD, k-medoids++ or random
Total costSum of point-to-medoid distancesLower is better for fixed k
Medoid indicesPositions of chosen representativesDirectly interpretable records
Key parameters and outputs

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.

AlgorithmApproachTypical dataset size
PAMExhaustive swap search after greedy BUILDUp to a few thousand points
CLARARuns PAM on several random samplesTens of thousands of points
CLARANSRandomized search over neighbor solutionsMedium to large datasets
FastPAMEvaluates all k swaps for a point at onceAbout k times faster than PAM
Alternating k-medoidsk-means-style assign and update stepsFast but lower quality
BanditPAMSamples distances with bandit methodsHundreds of thousands of points
Main k-medoids algorithms

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.

MethodCluster representativeOutlier sensitivityDistance requirement
k-medoidsActual data pointLowAny dissimilarity
k-means clusteringArithmetic meanHighSquared Euclidean
k-mediansCoordinate-wise medianLowManhattan distance
Hierarchical clusteringNone, a tree of mergesDepends on linkageAny dissimilarity
DBSCANNone, density regionsLabels outliers as noiseAny metric
How k-medoids relates to neighboring algorithms

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.

SituationRecommendationReason
Outliers or heavy tailsPrefer k-medoids over k-meansMedoids resist extreme values
Categorical or mixed dataUse k-medoids with Gower distanceNo mean exists for categories
Very large numeric dataUse k-means or CLARAFull PAM is too costly
Need for real prototypesUse k-medoidsEach center is an actual record
Non-convex cluster shapesUse DBSCAN or spectral clusteringk-medoids assumes compact groups
Decision guide for applying k-medoids

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.