Cosine, Dot Product, and Euclidean Similarity
AI systems turn text, images, and users into embeddings: lists of numbers that capture meaning. To find related items, we need a way to compare those vectors.
Let us use:
A = [1, 2]B = [2, 4]— same direction as A, but largerC = [2, 1]— close to A, but points in a different direction
Cosine similarity: compare direction
Cosine similarity ignores magnitude and measures the angle between vectors. Its value usually ranges from -1 to 1; higher means more similar.
cosine(A, B) = 1.0cosine(A, C) = 0.8
Although B is larger, it points in exactly the same direction as A. Cosine similarity is a strong default for semantic search, where meaning matters more than vector size.
Dot product: compare direction and magnitude
The dot product multiplies matching dimensions and adds them. A larger score means stronger alignment, but vector magnitude also affects the result.
A · B = (1 × 2) + (2 × 4) = 10A · C = (1 × 2) + (2 × 1) = 4
Dot product is useful when magnitude carries information, and it is especially fast for ranking normalized embeddings. With normalized vectors, dot product and cosine similarity are equivalent.
Euclidean distance: compare physical closeness
Euclidean distance is the straight-line distance between two points. Here, smaller means more similar.
distance(A, B) = √5 ≈ 2.24distance(A, C) = √2 ≈ 1.41
C is physically closer to A, even though B points in the same direction. Euclidean distance works well when absolute position and scale matter, such as clustering spatial features.
Quick rule of thumb
- Use cosine similarity for direction or semantic meaning.
- Use dot product when direction and magnitude both matter.
- Use Euclidean distance when geometric closeness matters.
The best metric depends on what the embedding's magnitude represents—and whether it should influence similarity at all.