Explore K-means clustering with interactive points and centroids. Follow cluster assignments and centroid updates to see how unlabeled data forms groups.
1Drop k centres anywhere
Nothing is labelled. Pick how many groups you want (k = 3 here) and place that many centres, at random or spread out with k-means++.
2Every point joins its nearest centre
Colour each point by the closest centre. The first pass is rough: a few points land with the wrong group, and the next steps fix that.
3Move each centre to the middle, then repeat
Each centre jumps to the average of its points. Some points are now closer to a different centre, so assign again. Stop when nothing changes.
- Round
- 0
- Inertia
- –
- Changed
- –
- Points
- 42
Cluster sizes
- 0
- 0
- 0
Press Start to watch the centres find the groups.
How it works: K-Means Clustering
Step by step
- Centre
- Points, coloured by cluster
- How far a centre moved
Press Start or Step: the step the animation is on lights up below.
- 1
Place k centres
Random points, or k-means++, which picks each new centre far from the ones already placed.
centres = init(points, k) - 2
Each point joins its nearest centre
Measure the distance to every centre and colour the point by the closest one.
label[p] = argmin dist(p, centre) - 3
Move centres to the middle
Each centre jumps to the mean of the points assigned to it. The arrows show how far it moved.
centre[c] = mean(points in c) - 4
Stop when nothing changes
If no point switches cluster, the centres won't move again, so we're done.
if labels unchanged: stop
K-means splits unlabelled points into k groups, where k is a number you pick. Nobody tells it what the groups are: it finds points that sit close together.
It alternates two simple moves. Assign: every point joins the centre nearest to it. Update: every centre moves to the average position of its points. Then assign again, because some points are now closer to a different centre.
Each move can only reduce the total squared distance from points to their centres, so it always stops. But where it stops depends on where the centres started, which is why k-means++ spreads the first centres out.
Where you'll meet it
- Customer segments, grouping similar documents, compressing an image's colours to a palette of k.
- As a quick first look at unlabelled data before deciding how to model it.
- When the groups are roughly round and similar in size. For long, curved or nested shapes, use something else (DBSCAN, spectral clustering).
Each assign step compares every point with every centre. It usually settles within a handful of rounds.