Lensa ML

Cosine vs Euclidean: why embeddings compare angles

An embedding is a list of numbers that stands for the meaning of a word, sentence or document, drawn here as an arrow. Search systems find related items by comparing embeddings. The two common ways to compare them can disagree about which item is closest.

2 min read · Lensa ML
length 126°2.061.80A (4, 1)B (2, 0.5)C (3, 2.5)
from A todistancecosinedistance at length 1
B2.061.0000.00
C1.800.9010.45
Boxed: the nearer vector by each measure. Ringed dots: each vector divided by its length.
  1. Euclidean distance is the straight-line gap between two arrow tips, √((a₁ − b₁)² + (a₂ − b₂)²). Cosine similarity is the cosine of the angle between two arrows, a·b / (|a| |b|). Here a·b multiplies matching entries and adds the results, and |a| is an arrow's length. Cosine runs from 1 for the same direction, through 0 at a right angle, to −1 for opposite directions.
  2. B is A at half the length, so it points exactly the same way. By distance, B is 2.06 from A. C is only 1.80 from A, so Euclidean distance calls C the nearer one. But C points 26° away from A. Its cosine with A is 0.901, while B's is 1.000, the highest possible. By angle, B is the nearer one.
  3. Length can record things other than meaning. In a word-count vector, where each entry counts one word, a document pasted twice doubles every count: twice the length, same direction, same topic. In word2vec, a classic word-embedding method, a word's vector length varies with how often the word appears in training, while its direction carries its meaning. Many embedding models are trained so that cosine similarity reflects meaning, which is why search over embeddings usually ranks by angle.

Takeaway

Normalizing, dividing each vector by its length, puts every vector on the dashed circle of length 1 and ends the disagreement. A and B land on the same point, distance 0, and C sits 0.45 away. For vectors of length 1, squared distance equals 2 − 2 × cosine, so both measures rank neighbors the same way. Keep raw Euclidean distance when length carries meaning, as with physical measurements.

Go deeper · interactive

Norms & Distance Metrics

On the Cosine Similarity step, drag the Angle of vector 1 and Angle of vector 2 sliders and watch cos(θ) fall as the angle opens.

Open the explainer

More bites

References