Part of the Pristone Academy AI Technical Track
K-means clustering, explained the way it should click.
How a machine sorts a pile of points into clean groups — with nobody telling it what the groups are.
Sometimes the groups are real, but nobody has labeled them yet.
A streaming service has ten million viewers and no idea who they are. No survey, no tags — just a log of what each person watched. And yet the viewers clearly aren't all alike: somewhere in that data are the horror fans, the comfort-sitcom crowd, the documentary devotees. The groups are real. They're just unmarked.
That's the job of clustering: find the natural groupings in data when no one has told you the answers. Unlike a spam filter — trained on emails already labeled “spam” or “not” — clustering gets no labels at all. It has to discover the structure on its own. That's why it's called unsupervised learning: no teacher, no answer key, just the raw shape of the data. K-means is the most widely used clustering method there is, and the easiest to picture.
The payoff is everywhere: customer segments for marketing, grouping genes that behave alike, compressing an image down to its handful of dominant colors, spotting communities in a social graph. Anywhere you suspect “there are a few kinds of these things,” clustering is how you find the kinds.
k-means will always hand you groups — even if the data has no real groups at all. It finds structure whether or not structure exists. Deciding the clusters are meaningful is your job, not the algorithm's.
Guess the centers. Assign. Move. Repeat until nothing changes.
Up close, k-means is almost suspiciously simple — two steps, looped. You first pick how many groups you want, called k. Then:
Drop k centers
Scatter k points — the centroids — onto the data, usually by picking k random points to start. These are your first, almost certainly wrong, guesses at where the groups sit.
Assign every point
Each data point joins whichever centroid is nearest. The cloud splits into k teams, colored by which center they chose.
Move each center
Slide every centroid to the average position of the points that joined it — the balance point of its crowd.
Repeat until still
Re-assign, re-average, re-assign, re-average. Each pass, points switch teams and centers drift less. When a full round changes nothing, the groups are stable — and you're done. This back-and-forth is called Lloyd's algorithm.
Don't just read it — run it. Press Run and watch the two steps alternate: points grabbing their nearest center, centers sliding into their crowds. Or hit Step once to walk through it one move at a time. The number below the points is the inertia, and it falls every round.
Two things to feel in your hands. First, the inertia only ever goes down — every assign-and-move step makes the groups tighter, never looser, which is exactly why the loop is guaranteed to settle. Second, hit Re-seed a few times: same points, different random start, and sometimes a different final grouping. k-means finds a good answer, not always the best one.
What “nearest” and “best” actually mean.
The whole algorithm rests on two precise ideas: a way to measure distance, and a single number that says how good a grouping is.
Distance is just the Pythagorean theorem
“Nearest center” needs a ruler. For two points, the straight-line Euclidean distance is the hypotenuse of a right triangle — the same a² + b² = c² you already know, one feature per axis. Add more features and you just add more terms under the square root.
“Best” is the smallest total spread
That number under the lab has a name: inertia, or within-cluster sum of squares. Add up, for every point, the squared distance to its own center. Tight, well-separated groups make it small; sprawling ones make it big. k-means is nothing but a search to minimize it.
Here's the quiet elegance: the two steps you watched are exactly the two ways to shrink that number. Assigning each point to its nearest center is the best you can do with the centers fixed; moving each center to its crowd's average is the best you can do with the assignments fixed. Alternate them and the total spread can only fall — which is the downhill curve you saw. That's the kind of connection that's easy to miss alone and obvious once someone walks you through it, which is exactly what a 1:1 session is for.
Every honest use of k-means comes down to two questions.
k-means never picks k for you. Run it for several values and plot the inertia against k — it always falls, but at some point the gains flatten out. That bend, the elbow, is a common rule of thumb for “enough groups.”
Because the first centers are random, a single run can settle into a mediocre grouping. The standard fix is to run many times from different starts and keep the lowest-inertia result — the idea behind k-means++.
k-means finds the grouping that minimizes within-cluster spread — but how many groups to look for, and whether the groups mean anything, are decisions you make from outside the algorithm.
Frequently asked questions
More free lessons
What AI actually is, and how learning from data differs from following rules.
How a machine keeps the few directions that matter and throws away the rest.
The self-attention idea behind every modern transformer and LLM.
How AI redistributes work across the layers of a job — and where humans stay scarce.
Curious who's behind these lessons? See the proof of work.