k-medoids

From Wikipedia, the free encyclopedia
(Redirected from Partitioning Around Medoids)
Jump to navigation Jump to search

Template:Short descriptionkScript error: No such module "Check for unknown parameters".-medoids is a classical partitioning technique of clustering that splits the data set of nScript error: No such module "Check for unknown parameters". objects into kScript error: No such module "Check for unknown parameters". clusters, where the number kScript error: No such module "Check for unknown parameters". of clusters assumed known a priori (which implies that the programmer must specify k before the execution of a kScript error: No such module "Check for unknown parameters".-medoids algorithm). The "goodness" of the given value of kScript error: No such module "Check for unknown parameters". can be assessed with methods such as the silhouette method. The name of the clustering method was coined by Leonard Kaufman and Peter J. Rousseeuw with their PAM (Partitioning Around Medoids) algorithm.[1]

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 a most centrally located point in the cluster. Unlike certain objects used by other algorithms, the medoid is an actual point in the cluster.

Algorithms

File:K-Medoids Clustering.gif
PAM choosing initial medoids, then iterating to convergence for k=3 clusters, visualized with ELKI.

In general, the kScript error: No such module "Check for unknown parameters".-medoids problem is NP-hard to solve exactly.[2] As such, multiple heuristics to optimize this problem exist.

Partitioning Around Medoids (PAM)

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

  1. (BUILD) Initialize: greedily select Template:Mvar of the Template:Mvar 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 Template:Mvar, and for each non-medoid data point Template:Mvar:
      1. Consider the swap of Template:Mvar and Template:Mvar, 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(nk)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),[4] 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:[5][6][7]

  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 exhibit "erratic behavior".[8]Template:Rp Because it does not allow re-assigning 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 the swap based methods can solve.[4]

Hierarchical Clustering

Multiple variants of hierarchical clustering with a "medoid linkage" have been proposed. The Minimum Sum linkage criterion[9] 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,[10][11] but which 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 run time 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.[9] 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.[12]

Comparison to K-Means Clustering

The kScript error: No such module "Check for unknown parameters".-medoids problem is a clustering problem similar to kScript error: No such module "Check for unknown parameters".-means. Both the kScript error: No such module "Check for unknown parameters".-means and kScript error: No such module "Check for unknown parameters".-medoids algorithms are partitional (breaking the dataset up into groups) and 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 kScript error: No such module "Check for unknown parameters".-means algorithm, kScript error: No such module "Check for unknown parameters".-medoids chooses actual data points as centers (medoids or exemplars), and thereby allows for greater interpretability of the cluster centers than in kScript error: No such module "Check for unknown parameters".-means, where the center of a cluster is not necessarily one of the input data points (it is the average between the points in the cluster known as the centroid). Furthermore, kScript error: No such module "Check for unknown parameters".-medoids can be used with arbitrary dissimilarity measures, whereas kScript error: No such module "Check for unknown parameters".-means generally requires Euclidean distance for efficient solutions. Because kScript error: No such module "Check for unknown parameters".-medoids minimizes a sum of pairwise dissimilarities instead of a sum of squared Euclidean distances, it is more robust to noise and outliers than kScript error: No such module "Check for unknown parameters".-means.

Despite these advantages, the results of kScript error: No such module "Check for unknown parameters".-medoids lack consistency since the results of the algorithm may vary. This is because the initial medoids are chosen at random during the performance of the algorithm. kScript error: No such module "Check for unknown parameters".-medoids is also not suitable for clustering objects that are not spherical and may work inefficiently when dealing with large datasets depending on how it is implemented. Meanwhile, kScript error: No such module "Check for unknown parameters".-means is suitable for well-distributed and isotropic clusters and can handle larger datasets.[13] Similarly to kScript error: No such module "Check for unknown parameters".-medoids however, kScript error: No such module "Check for unknown parameters".-means also uses random initial points which varies the results the algorithm finds.

K-Medoids Vs. K-Means Table
Clustering Algorithm K-Medoids K-Means
Cluster Representation Uses medoids to represent clusters. Uses centroids to represent clusters.
Distance Metrics Can use any distance metric. Only optimizes squared Euclidean distance.
Computational Efficiency Runtime of most algorithms is quadratic in the size of the dataset. Linear in the size of the data set if the number of iterations is fixed.
Outlier Sensitivity Less sensitive to not only outliers but also

any noise.[14]

Highly sensitive to any noise/outliers.[13]
Cluster Shape Not suitable for clusters of arbitrary shape.[14] Best if clusters are normally distributed and isotropic.[15]

Packages

Template:Undue Several software packages provide implementations of k-medoids clustering algorithms. These implementations vary in their algorithmic approaches and computational efficiency.

scikit-learn-extra

The scikit-learn-extra[16] package includes a KMedoids class that implements k-medoids clustering with a Scikit-learn compatible interface. It offers two algorithm choices:

  1. The original PAM algorithm
  2. An alternate optimization method that is faster but less accurate

Parameters include:

  • n_clusters: The number of clusters to form (default is 8)
  • metric: The distance metric to use (default is Euclidean distance)
  • method: The algorithm to use ('pam' or 'alternate')
  • init: The medoid initialization method (options include 'random', 'heuristic', 'k-medoids++', and 'build')
  • max_iter: The maximum number of iterations (default is 300)

Example Python usage:

from sklearn_extra.cluster import KMedoids
kmedoids = KMedoids(n_clusters=2, method='pam').fit(X)
print(kmedoids.labels_)

python-kmedoids

The python-kmedoids[17] package provides optimized implementations of PAM and related algorithms:

  • FasterPAM: An improved version with better time complexity
  • FastPAM1: An earlier optimization of PAM
  • DynMSC: A method for automatic cluster number selection

This package requires precomputed dissimilarity matrices and includes silhouette-based methods for evaluating clusters.

Example Python usage:

import kmedoids
fp = kmedoids.fasterpam(dissimilarity_matrix, n_clusters=2)
print(fp.medoids)

Comparison

Feature scikit-learn-extra python-kmedoids
Algorithms PAM, Alternating FasterPAM, PAM, FastPAM1, Alternating
Distance metrics Various Precomputed
Automatic selection No Yes
API style scikit-learn Standalone and scikit-learn

Software

  • ELKI includes several k-medoid variants, including a Voronoi-iteration k-medoids, the original PAM algorithm, Reynolds' improvements, and the O(n²) FastPAM and FasterPAM algorithms, CLARA, CLARANS, FastCLARA and FastCLARANS.
  • Julia contains a k-medoid implementation of the k-means style algorithm (fast, 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
  • Python contains FasterPAM and other variants in the "kmedoids" package, additional implementations can be found in many other packages
  • R contains PAM in the "cluster" package, including the FasterPAM improvements via the options variant = "faster" and medoids = "random". There also exists a "fastkmedoids" package.
  • RapidMiner has an operator named KMedoids, but it does not implement any of above KMedoids algorithms. Instead, it is a k-means variant, that substitutes the mean with the closest data point (which is not the medoid), which combines the drawbacks of k-means (limited to coordinate data) with the additional cost of finding the nearest point to the mean.
  • Rust has a "kmedoids" crate that also includes the FasterPAM variant.
  • MATLAB implements PAM, CLARA, and two other algorithms to solve the k-medoid clustering problem.

References

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "Citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. a b Script error: No such module "Citation/CS1".
  5. Script error: No such module "Citation/CS1".
  6. T. Hastie, R. Tibshirani, and J. Friedman. The Elements of Statistical Learning, Springer (2001), 468–469.
  7. Script error: No such module "Citation/CS1".
  8. Script error: No such module "Citation/CS1".
  9. a b Script error: No such module "citation/CS1".
  10. Script error: No such module "citation/CS1".
  11. Script error: No such module "citation/CS1".
  12. Script error: No such module "Citation/CS1".
  13. a b Script error: No such module "citation/CS1".
  14. a b Script error: No such module "citation/CS1".
  15. Script error: No such module "citation/CS1".
  16. Script error: No such module "citation/CS1".
  17. Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".