1
$\begingroup$

I ran kmeans in r with k = 20 centers and 7 scaled variables to cluster with on a data frame with n = 100K.

Using dplyr group_by I was able to view summary data for each of the 20 clusters: the mean of var 1, var 2 etc.

I thought I understood the kmeans algorithm. Start with k centers, apply each observation to the closest centroid, recalculate the mean and repeat till no more movement.

I'm confused about the resulting centroid. I assumed that it was a single number in 7 dimensional space. However when I type (R) mycluster_object$centers I get back a 20 * 7 table with a value for each cluster and variable.

Example:

library(dplyr)
set.seed(123)
myclustering <- kmeans(select(iris, -Species), centers = 3)

 myclustering$centers
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1     5.006000    3.428000     1.462000    0.246000
2     6.850000    3.073684     5.742105    2.071053
3     5.901613    2.748387     4.393548    1.433871

What exactly are these numbers?

Separately, my initial goal was to order the clusters by "closeness". This is why I thought ordering by what I thought centroids to mean would help.

  1. What does myclustering$centers actually show?
  2. Is there a way to order by clusters by how similar or close together they are, accounting for all the variables used?
$\endgroup$

1 Answer 1

1
$\begingroup$

The numbers reported for the centers are the coordinates of each center in n-dimensional space. For example, the iris data has 3 centers in 4 D space thus the 3 rows and 4 columns. Your original problem had specified 20 centers(ie rows) and then 7 columns for each dimension.

The kmeans parameter withinss (myclustering$withinss) is the measure of the cluster's sum of the square error, thus a measure of how close each point of the cluster is to the center.

To compute the distance between the centers, the dist() function is helpful.

dist(myclustering$centers)
#          1        2
#2 5.017569         
#3 3.356935 1.797182

thus centers 2 &3 are the closest to each other and centers 1&2 are the farthest apart.

$\endgroup$
2
  • $\begingroup$ Thanks. I'm looking at the distance matrix and wondering if there's a way of ordering the clusters. I can see visually the smallest number for each row, telling me that the corresponding cluster is closest. How could I boil this down to a single vector with which to order by clusters by closeness? $\endgroup$
    – Doug Fir
    Commented Oct 25, 2018 at 19:14
  • $\begingroup$ You can try this where n is the number of centers and d is the result from the dist function: g<-expand.grid(r=1:n, c=1:n); g<-g[g$c<g$r, ]; g$d<-as.vector(d) $\endgroup$
    – Dave2e
    Commented Oct 25, 2018 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.