K-Means Clustering
Study guides: Exam SRM, Exam MAS-II, Exam ACIA-CAPSTONE
K-Means Clustering is a clustering method that partitions n observations into a number K of non-overlapping clusters C_1, \ldots, C_K, specified in advance, making the total within-cluster variation as small as possible. The within-cluster variation of cluster C_k is
W(C_k) = \frac{1}{|C_k|}\sum_{i, i' \in C_k}\sum_{j=1}^{p}(x_{ij} - x_{i'j})^2
|C_k|is the number of observations inC_k.- The sum runs over every ordered pair
i, i'of observations inC_k.
K-means chooses the clusters to minimize the total within-cluster variation W(C_1) + W(C_2) + \cdots + W(C_K). Each cluster is a subgroup of observations that are close to each other.
One cluster holds the observations
(0, 0),(2, 0)and(1, 3). What is its within-cluster variation?
- The squared distances between the three unordered pairs are
4,10and10, which total24.- Each pair is counted in both orders, so the sum is
48.W(C_k) = 48/3 = 16.
Within-cluster variation from the centroid
The centroid of a cluster is the vector of the means of each feature over its observations. The within-cluster variation is also
W(C_k) = 2\sum_{i \in C_k}\sum_{j=1}^{p}(x_{ij} - \bar{x}_{kj})^2
\bar{x}_{kj}is the mean of featurejover the observations inC_k.
The sum of squared distances from the centroid, the within-cluster sum of squares, is half of W(C_k), so minimizing either picks the same clusters.
The cluster holds
(0, 0),(2, 0)and(1, 3). What is its within-cluster variation from the centroid?
- The centroid is
(1, 1).- The squared distances to it are
2,2and4, which total8.W(C_k) = 2 \times 8 = 16, as from the pairs.
The K-means algorithm
- Randomly assign each observation to one of the
Kclusters. - Compute the centroid of each cluster.
- Assign each observation to the cluster whose centroid is nearest in Euclidean distance.
- Repeat steps 2 and 3 until the assignments stop changing.
The algorithm has converged when an iteration changes no assignment, and the centroids then are the final ones. The means are the values that minimize a sum of squared deviations, so no iteration can raise the objective. The algorithm finds a local optimum, not necessarily the best clustering, and its result depends on the random start. It is run from several random starts, and the run with the smallest total within-cluster variation is kept.
The observations are 2, 5 and 9 on one variable, and the starting centroids are 2 and 5. What clusters does K-means with
K = 2end with?
- In one dimension the Euclidean distance is the absolute difference.
- 2 is nearest the centroid 2, and 5 and 9 are nearest the centroid 5, since
|9 - 5| = 4 < |9 - 2| = 7.- The new centroids are 2 and
(5 + 9)/2 = 7.- Reassigning changes nothing, since 5 is
2from 7 and3from 2, so the clusters are\{2\}and\{5, 9\}.
Choosing the number of clusters
K is fixed during the algorithm, so a different K needs a new run. The best total within-cluster variation never rises as K grows, and at K = n every observation is its own cluster and the total is zero. Minimizing it therefore cannot choose K. There is no single agreed method: several values are tried and the most useful and interpretable result kept, with heuristics such as the elbow of a plot of the total within-cluster variation against K.
The total within-cluster variation for
K = 1, \ldots, 5is 100, 40, 25, 22 and 20. WhichKdoes the elbow suggest?
- The drops are 60, 15, 3 and 2.
- The large drops stop after
K = 3, so the elbow suggests three clusters.
K-means does not require standardized variables. Standardizing to mean zero and standard deviation one is often advisable, because K-means is distance-based and the variable with the largest scale would otherwise dominate. It works best when the clusters are roughly spherical and of similar size.
Pairwise or centroid form The pairwise form
W(C_k)is twice the within-cluster sum of squares about the centroid. For(0, 0),(2, 0)and(1, 3)one is 16 and the other 8, so check which one a question asks for.
Practice questions
- CAS Exam 8 Fall 2012, Question 1
- CAS Exam 8 Fall 2013, Question 4
- CAS Exam 8 Fall 2015, Question 6
- CAS Exam 8 Fall 2017, Question 2
- CAS Exam 8 Fall 2019, Question 4
- Exam DISC-DA Practice Question 227
- CAS Exam MAS-II Fall 2019, Question 40
- CAS Exam MAS-II Spring 2019, Question 42
- Exam PA Practice Question 18
- SOA Exam SRM Sample Question 2
- SOA Exam SRM Sample Question 15
- SOA Exam SRM Sample Question 16
- SOA Exam SRM Sample Question 34
- SOA Exam SRM Sample Question 40
- SOA Exam SRM Sample Question 43
- SOA Exam SRM Sample Question 59
- SOA Exam SRM Sample Question 60