Lably
Skip to content

K-medoids

Clustering algorithm minimizing the sum of distances to k representatives

  • 11 min read
  • 6 chapters

Related on Lably

Through Unsupervised learning, Data set and K-means clustering

The medoid of a cluster is defined as the object in the cluster whose sum (and, equivalently, the average) of dissimilarities to all the objects in the cluster is minimal. That is, it is the most centrally located point in the cluster. Unlike certain objects used by other algorithms, the medoid is an actual point in the cluster.

As every point is typically assigned to its nearest medoid, the k-medoids method (like k-means clustering) does not work well if clusters vary significantly in diameter. If clusters vary in diameter or shape, other clustering methods, such as Gaussian mixture modeling or density-based clustering, may work better.

Algorithms

In general, the k-medoids problem is NP-hard to solve exactly. As such, multiple heuristics to optimize this problem exist.

Partitioning around medoids (PAM)

PAM uses a greedy search method which may not find the optimum solution, but is faster than exhaustive search. It works as follows:

  1. (BUILD) Initialize: greedily select k of the n data points as the medoids to minimize the cost
  2. Associate each data point to the closest medoid.
  3. (SWAP) While the cost of the configuration decreases:
    1. For each medoid m, and for each non-medoid data point o:
      1. Consider the swap of m and o, and compute the cost change
      2. If the cost change is the current best, remember this m and o combination
    2. Perform the best swap of mbest and obest, if it decreases the cost function. Otherwise, the algorithm terminates.

The runtime complexity of the original PAM algorithm per iteration of (3) is O(k(n−k)2), by only computing the change in cost. A naive implementation recomputing the entire cost function every time will be in O(n2k2). This runtime can be further reduced to O(n2), by splitting the cost change into three parts such that computations can be shared or avoided (FastPAM). The runtime can further be reduced by eagerly performing swaps (FasterPAM), at which point a random initialization becomes a viable alternative to BUILD.

Alternating optimization

Algorithms other than PAM have also been suggested in the literature, including the following Voronoi iteration method known as the "Alternating" heuristic in literature, as it alternates between two optimization steps:

  1. Select initial medoids randomly
  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

k-means-style Voronoi iteration tends to produce worse results, and exhibits "erratic behavior". Because it does not allow reassigning points to other clusters while updating means it only explores a smaller search space. It can be shown that even in simple cases this heuristic finds inferior solutions as compared to swap based methods.

Hierarchical clustering

Multiple variants of hierarchical clustering with a "medoid linkage" have been proposed. The Minimum Sum linkage criterion directly uses the objective of medoids, but the Minimum Sum Increase linkage was shown to produce better results (similar to how Ward linkage uses the increase in squared error). Earlier approaches simply used the distance of the cluster medoids of the previous medoids as linkage measure, but this tends to result in worse solutions, as the distance of two medoids does not ensure there exists a good medoid for the combination. These approaches have a runtime complexity of O(n3), and when the dendrogram is cut at a particular number of clusters k, the results will typically be worse than the results found by PAM. Hence these methods are primarily of interest when a hierarchical tree structure is desired.

Other algorithms

Other approximate algorithms such as CLARA and CLARANS trade quality for runtime. CLARA applies PAM on multiple subsamples, keeping the best result. By setting the sample size to O(n), a linear runtime (just as to k-means) can be achieved. CLARANS works on the entire data set, but only explores a subset of the possible swaps of medoids and non-medoids using sampling. BanditPAM uses the concept of multi-armed bandits to choose candidate swaps instead of uniform sampling as in CLARANS.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Robotics