From Wikipedia, the free encyclopedia
  (Redirected from K-medoid)
Jump to navigation Jump to search

The k-medoids or PAM algorithm is a clustering algorithm reminiscent to the k-means algorithm. Both the k-means and k-medoids algorithms are partitional (breaking the dataset up into groups) and both attempt to minimize the distance between points labeled to be in a cluster and a point designated as the center of that cluster. In contrast to the k-means algorithm, k-medoids chooses data points as centers (medoids or exemplars) and can be used with arbitrary distances, while in k-means the centre of a clusters is not necessarily one of the input data points (it is the average between the points in the cluster). The PAM method was proposed in 1987[1] for the work with norm and other distances.

k-medoid is a classical partitioning technique of clustering, which clusters the data set of n objects into k clusters, with the number k of clusters assumed known a priori (which implies that the programmer must specify k before the execution of the algorithm). The "goodness" of the given value of k can be assessed with methods such as silhouette.

It is more robust to noise and outliers as compared to k-means because it minimizes a sum of pairwise dissimilarities instead of a sum of squared Euclidean distances.

A medoid can be defined as the object of a cluster whose average dissimilarity to all the objects in the cluster is minimal, that is, it is a most centrally located point in the cluster.


The most common realisation of k-medoid clustering is the Partitioning Around Medoids (PAM) algorithm. PAM uses a greedy search which may not find the optimum solution, but it is faster than exhaustive search. It works as follows:

  1. Initialize: select k of the n data points as the medoids
  2. Associate each data point to the closest medoid.
  3. While the cost of the configuration decreases:
    1. For each medoid m, for each non-medoid data point o:
      1. Swap m and o, associate each data point to the closest medoid, recompute the cost (sum of distances of points to their medoid)
      2. If the total cost of the configuration increased in the previous step, undo the swap

The runtime complexity of the original PAM algorithm per iteration of (3) is , but this can be reduced to .[2]

Algorithms other than PAM have also been suggested in the literature, including the following Voronoi iteration method:[3][4]

  1. Select initial medoids
  2. Iterate while the cost decreases:
    1. In each cluster, make the point that minimizes the sum of distances within the cluster the medoid
    2. Reassign each point to the cluster defined by the closest medoid determined in the previous step.

However, Voronoi iteration finds much worse results, as it does not allow reassigning points to other clusters while changing means, and thus only explores a much smaller search space.[2]

The approximate algorithms CLARA and CLARANS trade runtime for optimality. CLARA applies PAM on multiple subsamples, keeping the best result. CLARANS works on the entire data set, but only explores a subset of the possible swaps of medoids and non-medoids.

Demonstration of PAM[edit]

Cluster the following data set of ten objects into two clusters i.e. k = 2.

Consider a data set of ten objects as follows :

Figure 1.1 – distribution of the data
X1 2 6
X2 3 4
X3 3 8
X4 4 7
X5 6 2
X6 6 4
X7 7 3
X8 7 4
X9 8 5
X10 7 6

Step 1[edit]

Figure 1.2 – clusters after step 1

Two observations ( and ) are randomly selected as medoids (cluster centers). Manhattan distances are calculated to each center to associate each data object to its nearest medoid (marked in bold).

Data object Distance to
1 (2, 6) 3 7
2 (3, 4) 0 4
3 (3, 8) 4 8
4 (4, 7) 4 6
5 (6, 2) 5 3
6 (6, 4) 3 1
7 (7, 3) 5 1
8 (7, 4) 4 0
9 (8, 5) 6 2
10 (7, 6) 6 2
Cost 11 9

Since the points (2,6) (3,4) (3,8) and (4,7) are closer to c1 hence they form one cluster whilst the remaining points form another cluster. Therefore, the clusters become:

Cluster1 = {(2,6)(3,4)(3,8)(4,7)}
Cluster2 = {(7,4)(6,2)(6,4)(7,3)(8,5)(7,6)}

The total cost of this clustering is the sum of the distance between a data point and its cluster center:

Step 2[edit]

Figure 1.3 – clusters after step 2

Select one of the nonmedoids O′

Let us assume O′ = (7,3), i.e. x7.

So now the medoids are c1(3,4) and O′(7,3)

If and O are new medoids, calculate the total cost involved

By using the formula in the step 1

i c1 Data objects (Xi) Cost (distance)
1 3 4 2 6 3
3 3 4 3 8 4
4 3 4 4 7 4
5 3 4 6 2 5
6 3 4 6 4 3
8 3 4 7 4 4
9 3 4 8 5 6
10 3 4 7 6 6
i O′ Data objects (Xi) Cost (distance)
1 7 3 2 6 8
3 7 3 3 8 9
4 7 3 4 7 7
5 7 3 6 2 2
6 7 3 6 4 2
8 7 3 7 4 1
9 7 3 8 5 3
10 7 3 7 6 3
Figure 2. K-medoids versus k-means. Figs 1.1a-1.1f present a typical example of the k-means convergence to a local minimum. This result of k-means clustering contradicts the obvious cluster structure of data set. In this example, k-medoids algorithm (Figs 2.2a-2.2h) with the same initial position of medoids (Fig. 2.2a) converges to the obvious cluster structure. The small circles are data points, the four ray stars are centroids (means), the nine ray stars are medoids.[5]

So cost of swapping medoid from c2 to O′ is

So moving to O′ would be a bad idea, so the previous choice was good. So we try other nonmedoids points to get minimum distance.

It may happen some data points may shift from one cluster to another cluster depending upon their closeness to medoid.

In some standard situations, k-medoids demonstrate better performance than k-means. An example is presented in Fig. 2. The most time-consuming part of the k-medoids algorithm is the calculation of the distances between objects. If a quadratic preprocessing and storage is applicable, the distances matrix can be precomputed to achieve consequent speed-up. See for example,[4] where the authors also introduce a heuristic to choose the initial k medoids.


  • ELKI includes several k-means variants, including an EM-based k-medoids and the original PAM algorithm.
  • Julia contains a k-medoid implementation of the k-means style algorithm (faster, but much worse result quality) in the JuliaStats/Clustering.jl package.
  • KNIME includes a k-medoid implementation supporting a variety of efficient matrix distance measures, as well as a number of native (and integrated third-party) k-means implementations
  • R includes variants of k-means in the "flexclust" package and PAM is implemented in the "cluster" package.
  • RapidMiner has an operator named KMedoids, but it does not implement the KMedoids algorithm correctly. Instead, it is a k-means variant, that substitutes the mean with the closest data point (which is not the medoid).
  • MATLAB implements PAM, CLARA, and two other algorithms to solve the k-medoid clustering problem.


  1. ^ Kaufman, L. and Rousseeuw, P.J. (1987), Clustering by means of Medoids, in Statistical Data Analysis Based on the –Norm and Related Methods, edited by Y. Dodge, North-Holland, 405–416.
  2. ^ a b Schubert, Erich; Rousseeuw, Peter J. (2018-10-12). "Faster k-Medoids Clustering: Improving the PAM, CLARA, and CLARANS Algorithms". arXiv:1810.05691 [cs.LG].
  3. ^ T. Hastie, R. Tibshirani, and J. Friedman. The Elements of Statistical Learning, Springer (2001), 468–469.
  4. ^ a b H.S. Park , C.H. Jun, A simple and fast algorithm for K-medoids clustering, Expert Systems with Applications, 36, (2) (2009), 3336–3341.
  5. ^ The illustration was prepared with the Java applet, E.M. Mirkes, K-means and K-medoids: applet. University of Leicester, 2011.