← back to lab index

Points: 0
Iteration: 0
Inertia (WCSS):
Status: Click to add points
k (clusters) 3
Mode:
Speed 500ms
Within-cluster sum of squares (inertia) vs iteration

The algorithm

K-means alternates between two steps: assignment, where each point is labeled by its nearest centroid, and update, where each centroid moves to the mean of its cluster. This minimises within-cluster sum of squares (inertia) and always converges, though not always to the global optimum.

Choosing k

The “elbow method” plots inertia against k and looks for the bend where adding another cluster stops helping much. There is no universally correct k — the right number depends on the data and the question you are asking.

Initialization matters

Random initial centroid placement can lead to poor local minima. The k-means++ strategy seeds centroids far apart, dramatically improving convergence. Try dragging centroids to different starting positions and observe how the final clustering changes.

Applications

K-means is used in image compression (color quantization), customer segmentation, document clustering, anomaly detection, and as a preprocessing step for more complex algorithms. Its simplicity is its greatest strength.