k-Means
About this app
Lloyd's algorithm as a chain of states without labels: assign and move centres, Voronoi cells, inertia per half-step, random vs. k-means++, local minima, elbow with silhouette and typical failure cases.
The idea
k-means is an unsupervised method: the data carry no labels and the groups only emerge from the algorithm. The goal is to minimise the inertia J, the sum of squared distances from each point to its centre. Lloyd’s algorithm alternates two steps: assignment (each point joins its nearest centre) and update (each centre moves to the mean of its points). Neither step can increase J, so the algorithm terminates. The result, however, is only a local minimum and depends on the start.
What you see
Grey points, k starting centres and the Voronoi cells that mark each centre’s territory. Every half-step can be run on its own, and a caption reports how many points switched and how far J dropped. A chart plots J after each assignment and update, with earlier runs shown faintly. The elbow chart plots J for k = 1 to 8 together with the silhouette. “Compare 10 starts” shows how many different minima the runs end in.
Things to try
Step through “3 blobs” and confirm that J falls at every half-step. Compare random starting centres with k-means++ and run ten starts. Then load “moons” and “uneven”: k-means cuts with straight boundaries and assumes round groups of similar size. On “units”, the feature with the larger scale dominates the distance until you standardise.
Frequently asked questions
- How does k-means clustering work?
- k-means starts with k centres and repeats two steps: each point is assigned to its nearest centre, then each centre moves to the mean of its points. This continues until no assignment changes. In doing so it minimises the sum of squared distances to the centres.
- What is k-means++?
- k-means++ is a way to choose the starting centres. The first centre is picked at random, each further one with probability proportional to the squared distance to the nearest centre already chosen. This spreads the starting centres out and makes poor local minima less likely.
- How do you choose the number of clusters k?
- A common heuristic is the elbow method: plot the inertia against k and look for the bend after which extra clusters add little. The silhouette coefficient, between −1 and 1, additionally measures how clearly points belong to their cluster. The subject matter often has the final say.
- What can I do in this app?
- You choose a dataset, k and the initialisation method, place centres and points yourself, and step through the algorithm half-step by half-step or play it. The app shows Voronoi cells, the inertia history, the elbow curve with silhouette, and a comparison of ten starts.
Subject: Machine Learning
More from Machine Learning
- AdaBoost
- Anatomy of a prompt
- Data privacy traffic lights
- Decision Tree
- Ensemble: Bagging vs. Boosting
- Finding faces: masks versus learning from examples
- k-Nearest Neighbors (kNN)
- AI theatre
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps