PPristone AcademyLearn · Mentor · Build

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.

200,000 FT
The Big PictureWhy it exists

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.

Imagine emptying a jar of mixed coins onto a table and, without being told the denominations, sorting them into piles purely by how similar they look. That sorting instinct — group the alike, separate the different — is exactly what k-means automates.

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.

The catch, stated once

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.

30,000 FT
The MechanicsHow it runs

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:

1

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.

2

Assign every point

Each data point joins whichever centroid is nearest. The cloud splits into k teams, colored by which center they chose.

3

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.

Live · k-means clustering
ready
A pile of points and a few randomly dropped centers. Press Run and watch each point grab its nearest center, then each center slide to the middle of its crowd.
Number of clusters (k)
Progress
ITERATION0
INERTIA (WCSS)
NEXT STEPassign
unassigned point
point in a cluster
◆ cluster center

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.

UNDER THE HOOD
The Two IdeasWhat powers it

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.

d = √( (x₁ − x₂)2+(y₁ − y₂)2 )the straight-line gap between two points

“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.

0 FT
What to Carry OffThe two real questions

Every honest use of k-means comes down to two questions.

How many clusters (k)?

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.”

Did the random start hurt me?

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++.

The one sentence to remember

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

K-means is a way to automatically sort data into groups when nobody has labeled it. You tell it how many groups to look for (called k), and it repeatedly assigns each point to the nearest group center, then moves each center to the average of its points — until the groups stop changing.
K-means never picks k for you. The common approach is to run it for several values of k and plot how tight the groups are (the inertia) against k. The curve always improves, but at some point the gains flatten — that bend, called the elbow, is a practical rule of thumb for 'enough groups.'
Unsupervised. There are no correct answers to learn from — no labels. K-means discovers structure purely from the shape of the data, which is what separates it from a classifier like a spam filter that trains on examples already marked spam or not-spam.
Because it starts from random center positions. A single run can settle into a mediocre grouping. The standard fix is to run it many times from different starts and keep the tightest result — the idea behind k-means++, a smarter way of spreading the initial centers out.
Inertia (also called the within-cluster sum of squares) is the total squared distance from every point to its assigned center. Tight, well-separated groups make it small; sprawling ones make it large. K-means is nothing more than a search to make that number as small as possible.

Curious who's behind these lessons? See the proof of work.

Competition math

Checking AMC 10 readiness for your student?

Take the free readiness check and get a quick band, strengths, and priority gaps before deciding on a diagnostic.

Take the check