Harman Patil (Editor)

K medoids

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
K-medoids


The k-medoids algorithm is a clustering algorithm related to the k-means algorithm and the medoidshift 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 datapoints as centers (medoids or exemplars) and works with a generalization of the Manhattan Norm to define distance between datapoints instead of l 2 . This method was proposed in 1987 for the work with l 1 norm and other distances.

Contents

k-medoid is a classical partitioning technique of clustering that clusters the data set of n objects into k clusters known a priori. A useful tool for determining k is the 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. i.e. it is a most centrally located point in the cluster.

Algorithms

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, 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

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

  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.

Demonstration of PAM

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

Consider a data set of ten objects as follows :

Step 1

Initialize k centers.

Let us assume x2 and x8 are selected as medoids, so the centers are c1 = (3,4) and c2 = (7,4)

Calculate distances to each center so as to associate each data object to its nearest medoid. Cost is calculated using Manhattan distance (Minkowski distance metric with r = 1). Costs to the nearest medoid are shown bold in the table.

Then the clusters become:

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

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

So the total cost involved is 20.

Where cost between any two points is found using formula

c o s t ( x , c ) = i = 1 d | x i c i |

where x is any data object, c is the medoid, and d is the dimension of the object which in this case is 2.

Total cost is the summation of the cost of data object from its medoid in its cluster so here:

total cost = { c o s t ( ( 3 , 4 ) , ( 2 , 6 ) ) + c o s t ( ( 3 , 4 ) , ( 3 , 8 ) ) + c o s t ( ( 3 , 4 ) , ( 4 , 7 ) ) }   + { c o s t ( ( 7 , 4 ) , ( 6 , 2 ) ) + c o s t ( ( 7 , 4 ) , ( 6 , 4 ) ) + c o s t ( ( 7 , 4 ) , ( 7 , 3 ) )   + c o s t ( ( 7 , 4 ) , ( 8 , 5 ) ) + c o s t ( ( 7 , 4 ) , ( 7 , 6 ) ) } = ( 3 + 4 + 4 ) + ( 3 + 1 + 1 + 2 + 2 ) = 20

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 c1 and O′ are new medoids, calculate the total cost involved

By using the formula in the step 1

total cost = 3 + 4 + 4 + 2 + 2 + 1 + 3 + 3 = 22

So cost of swapping medoid from c2 to O′ is

S = current total cost past total cost = 22 20 = 2 > 0.

So moving to O′ would be a bad idea, so the previous choice was good. So we try other nonmedoids and found that our first choice was the best. So the configuration does not change and algorithm terminates here (i.e. there is no change in the medoids).

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, where the authors also introduce a heuristic to choose the initial k medoids.

Software

  • ELKI includes several k-means variants, including an EM-based k-medoids and the original PAM algorithm.
  • Julia contains a k-medoid implementation in the JuliaStats clustering 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.
  • References

    K-medoids Wikipedia