K-Means Clustering
Partitioning data into k clusters by iteratively assigning points to the nearest centroid and re-computing centroids until convergence.
Restart replays the same initial positions. New init picks a different random start â results may vary.
K-means clustering partitions data points into groups (clusters) such that each point belongs to the cluster with the nearest centroid (cluster center).
The algorithm:
- Initialize centroids randomly
- Assign each point to its nearest centroid
- Update each centroid to the mean of its assigned points
- Repeat 2â3 until centroids stop moving
K-means minimizes the within-cluster sum of squares (WCSS, or inertia):
- WCSS never increases at any step of the algorithm â both assignment and update steps can only improve or maintain it
- Always converges in finitely many steps, since there are only finitely many possible partitions
- Assumes roughly spherical, similarly-sized clusters â its geometric blind spot
- Sensitive to the initial choice of centroids, which is why multiple random restarts are standard practice
- Forgetting to scale features before clustering: as with k-NN, unscaled features with large numeric ranges dominate the Euclidean distance
- Picking without any diagnostic: always check an elbow plot or silhouette score rather than guessing arbitrarily
- Applying k-means to non-convex or very differently-sized clusters: it will produce a confident but wrong partition rather than failing visibly
An online retailer clusters customers by (purchase frequency, average order value). K-means with finds: "bargain hunters" (high frequency, low value), "occasional splurgers" (low frequency, high value), and "loyal premium" (high frequency, high value). Marketing strategies can then be tailored per segment.
K-means is guaranteed to converge but not to find the global optimum. Why might it get stuck in a poor solution, and what's a simple remedy?
Solution
K-means can converge to a local minimum that depends on the initial centroids. Different initializations give different results.
The standard remedy is k-means++ initialization: instead of placing all centroids randomly, place each new centroid proportionally to its squared distance from the existing centroids. This tends to spread the initial centroids and significantly reduces the chance of poor solutions. Another approach is simply to run k-means multiple times with different random seeds and keep the best result.