Imagine a pile of customer purchase records lands on your desk — how much each person spent, how often they visited, what they bought — but nobody has told you what "types" of customer exist. There's no column labelled "occasional big spender" or "frequent bargain hunter." Nobody has sorted them for you at all.
Step into the
It's everywhere once you start looking: grouping customers into segments, organising news articles into topics nobody named in advance, spotting friend groups inside a social network, even compressing the millions of colours in a photo down to a tidy palette. Whenever the question is "what natural categories are secretly in this data?", clustering is the tool you reach for.
Underneath every clustering method sits one simple idea: define a notion of distance
(or, equivalently, similarity) between two data points, then group together
whatever is close and separate whatever is far. If each data point is a vector of measurements —
say, (spending amount, visit frequency) for a shopper — then "distance" can be the ordinary,
geometric straight-line distance you already know: the
For two points
Small
Straight-line distance isn't the only option, either — it's simply the most natural one to start with. For text documents or user-preference vectors, "closeness" is often measured instead as the angle between two vectors rather than the gap between their tips (the same dot-product machinery gives you that angle too), because two customers who buy the same proportions of things, just at different volumes, should probably count as similar. The exact recipe for "distance" changes from problem to problem; what never changes is the basic move — pick a notion of closeness, then group whatever comes out close together.
Before trusting any algorithm, try it by hand. Here are five points on a simple grid:
A quick distance check confirms what your eye probably already saw:
Now scale that up: a real retailer's shopper data might place thousands of customers by (spending amount, visit frequency). A cluster sitting at (high spending, low frequency) is quietly telling you "occasional big spenders" exist; a cluster at (low spending, high frequency) reveals "frequent small spenders." Nobody typed those labels in anywhere — the algorithm never sees the words "big spender" at all. It only ever sees coordinates and distances; the human reading the result afterwards is the one who attaches a sensible name to each discovered group.
Here's that same idea as runnable code: given two candidate cluster centres, a new shopper is assigned to whichever one it's closer to, using nothing more than the distance formula above.
Run it and the shopper — who spends a lot but visits rarely — lands in the "big spenders" cluster, simply because their coordinates sit closer to that centre than to the other one. Nothing here knows or cares what the word "spender" even means; it's pure arithmetic on coordinates.
These points clearly fall into clumps — but how many? Choose the number of clusters
Unlike supervised classification, where the number of classes is simply whatever the labels say it
is, clustering hands you no such certainty. Nothing in the data itself carries a tag reading "there
are exactly 3 groups here" — three tight little sub-clusters might really be three genuine
categories, or they might just be one broad, slightly lumpy category that an over-eager choice of
Clustering has two traps that catch out almost every newcomer:
Clustering isn't only for spreadsheets. Streaming services cluster viewers by what they actually watch, not by any genre a human editor typed in — which is exactly how a service can notice, say, a cluster of people who love both true-crime documentaries and baking competition shows, a genre-crossing "taste group" no one had ever named before it showed up in the data.
Astronomers do the same thing to the sky itself. Feed a clustering algorithm the positions, brightness and motion of millions of stars from a survey telescope, and it can pick out clumps that turn out to be genuine star clusters, previously-unrecognised streams of stars torn from a dwarf galaxy, or new structure in how galaxies group together across the universe — patterns that were sitting in the data all along, waiting for an algorithm (rather than a human squinting at a star chart) to notice they were even there.
Without labels, "correct" is genuinely ambiguous — different notions of similarity give different
clusterings, and choosing