Lecture: Lecture 11 - Clustering
Mechanism
K-means is a partitional algorithm. Choose a number of clusters K, place one centroid in each cluster, assign every point to its nearest centroid, recompute each centroid as the mean of its assigned points, and repeat until assignments change very little.
For one-dimensional points 1, 2, 8 and 9 with K=2, start with centroids 2 and 8. The first assignment is:
| Point | Nearest centroid | Cluster |
|---|---|---|
| 1 | 2 | A |
| 2 | 2 | A |
| 8 | 8 | B |
| 9 | 8 | B |
The updated centroids are the means: A becomes (1+2)/2 = 1.5, and B becomes (8+9)/2 = 8.5. Repeating the assignment now makes no change, so the algorithm stops.
Distance and scaling
Euclidean distance is commonly used. For vectors p and q, d(p,q) = sqrt(sum((p_i-q_i)^2)). If income ranges from 10,000 to 1,000,000 while age ranges from 18 to 80, income can dominate the distance. Normalize attributes when the units should contribute comparably.
SSE and the elbow
The sum of squared error is:
SSE = sum over clusters and points of distance(point, centroid)^2
Lower SSE means points are closer to their centroids. Increasing K usually lowers SSE, even when extra clusters are not useful. Plot SSE for several K values and choose an elbow where additional centroids give only small improvements.
Initialization and limitations
Random starting centroids can lead to different results. Run the algorithm multiple times or use a better initialization strategy. K-means is most suitable for similarly sized, similarly dense, compact groups. It handles elongated shapes, unequal densities and outliers poorly.
Retrieval questions
Why does K matter? It determines how many centroids and final groups the algorithm must create.
Why is a lower SSE not enough? SSE tends to fall as K grows, so a very large K can produce compact but unhelpful fragments.
What is updated after assignment? Each centroid is replaced by the mean of the points currently assigned to it.