Drop pins. Assign. Move pins. Repeat. That’s the whole algorithm.
Ashley circled three groups by eye.
Felix wants the computer to do it, and to do it the same way twice.
The recipe turns out to be simple enough to do on a napkin: drop three pins, assign every song to its nearest pin, move each pin to the middle of its songs, repeat.
The short answer
It doesn’t circle. It plants three centers, lets each song pick the closest, moves the centers to the middle of their songs, and repeats until the picture stops changing. Four rounds, on the song map. Ashley’s finger-circles and the computer’s groups agree almost exactly.
Try it yourself
Press Step. Watch the centers claim their nearest songs, then move to the middle of what they claimed. Keep stepping until they stop moving.
What you just did has a name
K-means
K-means is the standard clustering algorithm.
K is how many groups you ask for.
It starts with k random centers, assigns each point to the nearest one, moves each center to the average of its points, and repeats until nothing changes.
Simple, fast, and everywhere.
A few more things
Two steps, over and over
Assign: every point picks its closest center. Update: every center moves to the middle of its points. Each round makes the groups a little tighter, and after a handful of rounds they stop changing.
The start matters
Different random starting pins can settle into different groups. Real tools run it several times from different starts and keep the tightest result. If your groups change a lot between runs, the clusters aren’t as real as they look.
It finds round groups
K-means draws each group as “everything closest to this center,” so it likes blobs. Long, curved or nested groups confuse it. Other algorithms exist for those shapes, but k-means is the one to learn first.
Go deeper
The longer version, if you want it.
What it’s minimizing
Each round of k-means lowers one number: the total squared distance from every point to its assigned center. Assigning points to their nearest center lowers it. Moving centers to the average lowers it. Since it can only go down and can’t go below zero, the algorithm has to stop, which is why it always settles. It just isn’t guaranteed to settle in the best possible place, which is the random-start problem.
The average of a group of points is its centroid, so the method is sometimes described as “find k centroids.” Same thing.
Quick check
Halfway through a k-means run, one center has 40 points assigned to it. What happens to that center next?
Let’s take it slower
Picture it with pins and string on a table. Toss k pins down at random. Tie each dot to whichever pin is nearest. Now slide each pin to the middle of the dots tied to it. Some dots are now closer to a different pin, so retie those. Slide again. After a few rounds nothing changes hands and the pins stop moving. That’s the answer. The only settings you chose were how many pins and where they happened to land, and the second one is why real tools run it more than once.
More examples
1
Placing three food trucks
A city wants three food trucks placed where the lunch crowd is. Guess three spots, send each person to the nearest truck, then move each truck to the middle of its crowd and repeat. After a few rounds the trucks stop moving, and that is k-means with wheels.
2
Shrinking a photo to 16 colors
Image tools reduce a photo to 16 colors using k-means: pick 16 starting colors, assign each pixel to the nearest, average each group and repeat. The picture ends up in 16 shades nobody chose by hand. The k was the only decision a person made.
Back to the question
So how does the computer circle the clumps?
Show the answer
It doesn’t circle. It plants three centers, lets each song pick the closest, moves the centers to the middle of their songs, and repeats until the picture stops changing. Four rounds, on the song map. Ashley’s finger-circles and the computer’s groups agree almost exactly.
One last question
Build the k-means loop.
Tap the pieces in order. Tap one again to take it back.
…
Talk Card
Talk Card · 17.2
K-means drops k centers, gives every point to its closest center, moves each center to the middle of its points, and repeats until nothing moves.