Interactive guide
"Distance" isn't one idea: it's a choice
KNN, K-Means, DBSCAN: every algorithm that reasons about "closest" first has to decide what closest even means. Straight-line distance is the obvious default, but it isn't the only option, and which one a model uses can change its answers. Four of the most common below, all on the same two draggable points.
1 · Euclidean Distance "as the crow flies"
The straight line between two points, Pythagoras applied to however many features there are. It's the default in most libraries, and the one distance that stays the same no matter how the axes are rotated.
A B dashed = Δx, Δy legs drag either point.
Drag point A or B.
- Δx
- —
- Δy
- —
2 · Manhattan Distance "city block"
No diagonals allowed, only travel along the axes, like a taxi restricted to a street grid. It's just as valid a way to measure distance as Euclidean, and it's never shorter: the dashed straight line below is always the shortest possible path between the two.
A B solid = grid path dashed = Euclidean, for comparison.
Drag either point.
- Manhattan
- —
- Euclidean
- —
3 · Minkowski Distance the family both belong to
Euclidean and Manhattan are the same formula with a different p: d = (|Δx|p + |Δy|p)1/p. p=1 is Manhattan, p=2 is Euclidean, and as p keeps climbing toward infinity the formula quietly turns into Chebyshev distance: just the single largest difference across any feature. The shape on the left is every point exactly distance 1 from the center, under that same p. Watch it morph from a diamond into a circle into a square.
Every point at distance 1 from the center, under the current p.
Same two points as above; drag either one.
—
- Distance
- —
4 · Cosine Similarity & Distance direction, not position
Every metric so far cares about magnitude: twice as far away means a bigger number. Cosine similarity doesn't: it only asks about the angle between two vectors, which is why it's the default for comparing text embeddings, where a document repeated twice should count as identical, not "twice as far."
vector A vector B drag either arrowhead.
—
- Angle θ
- —
- Cosine distance
- —
- Euclidean (tips)
- —
Same two points, three different numbers: Euclidean, Manhattan, and whatever Minkowski's p happens to be all disagree about "how far," and cosine won't even answer the same question. There's no universally correct distance, only one that matches what the features actually mean, and what the algorithm downstream is going to do with it.