The K-Means Algorithm
The Algorithm
K-Means partitions data points into clusters. Each cluster is represented by its centroid (mean vector of all points assigned to it).
Step-by-Step
- Initialise: choose initial centroids (standard approach: randomly select points from the data).
- Assign: for each point , assign it to the cluster of the nearest centroid using Euclidean distance:
- Update: recompute each centroid as the mean of all points currently assigned to it:
- Repeat steps 2-3 until assignments stop changing (convergence).
Convergence Proof
K-Means always converges to a local optimum. The WCSS (Within-Cluster Sum of Squares) never increases across iterations.
Proof sketch: each iteration has two phases. The assignment phase moves points to their nearest centroid, which can only decrease or leave unchanged each point’s distance to its centroid; therefore WCSS cannot increase. The update phase sets each centroid to the mean of its cluster points; for Euclidean distance, the mean is the point that minimises the sum of squared distances to the cluster members; therefore WCSS cannot increase in the update phase either. Since WCSS is bounded below by 0 and cannot increase, and each step either decreases WCSS or leaves it unchanged (in which case convergence is reached), the algorithm must converge in a finite number of steps.
The algorithm is not guaranteed to find the global optimum. Different random initialisations can lead to different final clusterings.
Sensitivity to Initialisation
Poor initial centroids (two centroids starting near each other within the same true cluster) can lead to:
- Empty clusters (a centroid gets no points assigned).
- Suboptimal local optima (poor WCSS).
- Slow convergence.
Solution: K-Means++ initialisation (see K-Means++) or multiple random restarts, keeping the clustering with the lowest final WCSS.
Full Worked Example
Data
Six points in 2D: , , , , , . Set .
Initialisation
Randomly select and as initial centroids:
Iteration 1: Assignment
Compute Euclidean distances to each centroid:
| Point | Assigned to | ||
|---|---|---|---|
| Cluster 1 | |||
| Cluster 1 | |||
| Cluster 1 | |||
| Cluster 2 | |||
| Cluster 2 | |||
| Cluster 2 |
Assignment: , .
Iteration 1: Update
Iteration 2: Assignment
| Point | To | To | Assigned to |
|---|---|---|---|
| 1 | 92.4 | Cluster 1 | |
| 1 | 89.1 | Cluster 1 | |
| 2 | 54.8 | Cluster 1 | |
| 61 | 1.1 | Cluster 2 | |
| 85 | 0.4 | Cluster 2 | |
| 89 | 1.1 | Cluster 2 |
Assignments unchanged: converged.
Final WCSS
Summary
| Aspect | Detail |
|---|---|
| Input | data points, desired number of clusters |
| Output | cluster centroids + cluster assignments |
| Objective | Minimise WCSS = |
| Convergence | Guaranteed to converge, but to a local optimum only |
| Sensitivity | Depends on initial centroid choices |
| Complexity | per iteration |
Past paper questions: y2024p3q7e