# difference between k means and k medoid

I understand the difference between k medoid and k means. But can you give me an example with a small data set where the k medoid output is different from k means output

As you know, k-medoid is based on centroids (or medoids) calculating by minimizing the absolute distance between the points and the selected centroid, rather than minimizing the square distance. As a result, it's more robust to noise and outliers than k-means.

Here is a simple, contrived example with 2 clusters (ignore the reversed colors)

As you can see, the centroids are slightly different in each group. Also you should note that every time you run these algorithms, because of the random starting points and the nature of the minimization algorithm, you will get slightly different results. Here is another run:

And here is the code:

library(cluster)
x <- rbind(matrix(rnorm(100, mean = 0.5, sd = 4.5), ncol = 2),
matrix(rnorm(100, mean = 0.5, sd = 0.1), ncol = 2))
colnames(x) <- c("x", "y")
# using 2 clusters because we know the data comes from two groups
cl <- kmeans(x, 2)
kclus <- pam(x,2)
par(mfrow=c(1,2))
plot(x, col = kclus$clustering, main="Kmedoids Cluster") points(kclus$medoids, col = 1:3, pch = 10, cex = 4)
plot(x, col = cl$cluster, main="Kmeans Cluster") points(cl$centers, col = 1:3, pch = 10, cex = 4)

• @frc, if you think someone's answer is incorrect, don't edit it to correct it. You can leave a comment (once your rep is >50), &/or downvote. Your best option is to post your own answer w/ what you believe to be the correct information (cf, here). – gung Nov 22 '16 at 16:18
• K-medoids minimizes an arbitrarily chosen distance (not necessarily an absolute distance) between clustered elements and the medoid. Actually the pam method (an example implementation of K-medoids in R) used above, by default uses the Euclidean distance as a metric. K-means always uses the squared Euclidean. The medoids in K-medoids are chosen out of the cluster elements, not out of a whole points space as centroids in K-means. – hannafrc Nov 27 '16 at 16:40
• I have not enough reputation to comment, but wanted to mention that there is a mistake in the plots of Ilanman's answer: he ran the whole code, such that the data was modified. If you run only the clustering part of the code, the clusters are quite stables, more stable for PAM than for k-means by the way. – Julien Colomb Jun 14 '17 at 10:40

I don't have enough points to comment yet, so I have to post my comment here.

The accepted answer incorrectly implies that centroids and mediods are the same. A mediod has to be a member of the set, a centroid does not. Search for both terms on Wikipedia. Centroids are typically discussed in the context of solid, continuous objects, but there's no reason to believe that the extension to discrete samples would require the centroid to be a member of the original set.

• You need to wait to comment and not use a comment as an answer. – Michael Chernick Feb 1 '18 at 17:07
• The first comment above suggested it. Thanks. – headdab Feb 2 '18 at 17:43

Both k-means and k-medoids algorithms are breaking the dataset up into k groups. Also, they are both trying to minimize the distance between points of the same cluster and a particular point which is the center of that cluster. In contrast to the k-means algorithm, k-medoids algorithm chooses points as centers that belong to the dastaset. The most common implementation of k-medoids clustering algorithm is the Partitioning Around Medoids (PAM) algorithm. PAM algorithm uses a greedy search which may not find the global optimum solution. Medoids are more robust to outliers than centroids, but they need more computation for high dimensional data.