The idea
The dot product is the one operation underneath almost every notion of similarity in data science. Computationally it is trivial:
Geometrically it is a signed alignment score:
where is the angle between the vectors. Same direction: large and positive. Orthogonal: zero. Opposite: negative. Two things scale it — how long the vectors are, and how aligned they are. Every similarity measure you use is a choice about which of those two factors you want to keep.
From the dot product you get norms (size):
and Euclidean distance (separation), tied back to the dot product by an identity worth memorizing:
Cosine similarity strips magnitude out and keeps only alignment:
The decision rule between them: is magnitude signal or nuisance? Document length, purchase counts, and embedding norms are usually nuisance — use cosine. Sensor readings, standardized features, and coordinates are usually signal — use Euclidean. And the two choices are secretly one after normalization: for unit vectors, , so Euclidean ranking on L2-normalized vectors is cosine ranking. That is why the standard production pattern is "normalize once at index time, then use whichever the index computes fastest."
One more reusable tool: the projection of onto ,
is "the part of that lies along " — the geometric primitive behind least squares, PCA reconstruction, and orthogonalization.
Drag the two vectors below and watch dot product and cosine similarity respond differently to length versus direction:
Drag either vector: stretching one along its own direction moves the dot product but leaves cosine similarity unchanged — magnitude is nuisance, alignment is signal.
Worked example
Word-count vectors over the vocabulary (data, model, pizza):
| Doc | Vector | Description |
|---|---|---|
| short ML article | ||
| the same article pasted twice | ||
| restaurant review |
Compute similarities of and against :
For against :
For against :
Read the punchline off the numbers: Euclidean distance claims the pasted duplicate (distance ) is nearly as far from as the pizza review (distance ). Cosine correctly says is the same document () and is off-topic (). Magnitude — here, document length — was pure nuisance, and Euclidean distance charged for it.
Now normalize everything to unit length and recompute Euclidean distance: , so , and . The normalized Euclidean ranking now agrees with cosine exactly, as the identity promises.
Cheat sheet
Core formulas
| Quantity | Formula | Notes |
|---|---|---|
| Dot product | signed alignment | |
| L2 norm | rotation-invariant | |
| L1 norm | robust to one big coordinate | |
| Euclidean distance | squared: | |
| Cosine similarity | magnitude-blind | |
| Projection of on | basis of least squares and PCA |
Decision rules
- Magnitude is signal (measurements, scaled features) → Euclidean.
- Magnitude is nuisance (counts, text, embeddings) → cosine.
- Can normalize at index time → do it; then the two rankings coincide via on the unit sphere.
- Mixed-scale raw features + any distance method (k-NN, k-means) → standardize first or one feature silently dominates.
Gotchas
- Cosine similarity is undefined for the zero vector — guard empty documents and all-zero embeddings.
- Cosine distance () violates the triangle inequality — the angle itself is the metric, if you need one.
- means orthogonal, not "opposite" — opposite is .
- Raw dot-product similarity (no normalization) favors long vectors — in recommenders this systematically over-ranks popular items.
- In high dimensions, random vectors are nearly orthogonal and distances concentrate — neighbor contrast comes from real structure, not from the geometry being kind.