K-means clustering
Place points on the canvas, pick k, and watch the algorithm iterate: assign each point to its nearest centroid, then move each centroid to the mean of its cluster. Repeat until convergence. The simplest unsupervised learning algorithm — and one of the most useful.
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.