How to use the k-means visualiser
- Generate a dataset (blobs, uniform, moons or rings), click to add your own points, or paste x,y values.
- Choose the number of clusters, k, and how the starting centroids are picked: k-means++ spreads them out, random picks any points.
- Press Step to watch one iteration at a time, or Run to convergence. The triangles are centroids; each point takes the colour of its nearest centroid.
- Use the elbow chart to choose a sensible k: look for the point where adding clusters stops reducing the inertia much.
How k-means clustering works
K-means is one of the most widely used unsupervised machine learning algorithms. It groups data into k clusters by repeating two simple steps: assign every point to its nearest centroid, then move each centroid to the average position of its points. Each round reduces the inertia, the total squared distance from points to their centroid, and the algorithm stops when centroids no longer move. It is used for customer segmentation, image colour quantisation, grouping documents by their embeddings, and as a first look at any new dataset.
The visualiser also shows its limits. The result depends on the starting centroids, which is why k-means++ initialisation and several restarts are standard. K-means assumes roughly round clusters of similar size, so it splits the moons and rings datasets in ways that look wrong to a human; density-based methods such as DBSCAN handle those better. And k must be chosen in advance: the elbow method plots inertia against k and looks for the bend. Everything runs in your browser.
How do I choose k?
Use the elbow chart, domain knowledge, or a score such as the silhouette coefficient. There is often no single right answer.
Why do I get different clusters each time?
The starting centroids are random. Different starts can converge to different local solutions.
Can k-means find ring-shaped clusters?
No. It always divides space into convex regions around centroids. Use DBSCAN or spectral clustering for such shapes.