Cosine Similarity: Measuring Direction, Not Distance
Cosine similarity measures the angle between two vectors, not their distance, to gauge similarity. It asks, "Do these point in the same direction?" This is fundamental in AI for comparing text embeddings, where a vector's direction represents its meaning. The main footgun is confusing it with Euclidean distance; cosine similarity ignores vector magnitude, so two vectors can be far apart in space but still be considered nearly identical if their orientation is the same.
### The Mental Model
Cosine similarity measures how similar the *direction* of two vectors is, ignoring their magnitude (or length). Imagine two arrows starting from the same point (the origin). If they point in the exact same direction, their similarity is 1. If they are perpendicular (90 degrees apart), their similarity is 0, indicating no relation. If they point in opposite directions, their similarity is -1.
### How It Works
Cosine similarity is calculated by taking the dot product of two vectors (A and B) and dividing it by the product of their magnitudes (lengths).
`Similarity = (A · B) / (||A|| * ||B||)`
The division by the magnitudes is the crucial step; it normalizes the vectors, effectively scaling them to a length of 1 before comparing them. This is why only the angle matters. In many applications like text analysis, where vector values are non-negative (e.g., word counts), the similarity score will range from 0 to 1.
### When to Use It
* **Text & Document Similarity:** To find documents that discuss similar topics, regardless of document length. A short article and a long book about AI could have a high cosine similarity. * **Word Embeddings:** To measure semantic similarity. In a trained model, the vector for `king` minus `man` plus `woman` will have a high cosine similarity to the vector for `queen`. * **Recommendation Engines:** To find users with similar tastes by comparing their preference vectors.
### When NOT to Use It
* **When Magnitude Matters:** If the magnitude of the vector is meaningful, cosine similarity can be misleading. For example, in comparing user ratings, you might care that one user rates movies frequently and highly (high magnitude) while another rates them rarely and lowly (low magnitude), even if their tastes are directionally similar. Euclidean distance might be better here.
### One Canonical Example
Consider three vectors representing documents: * `A = (2, 1)` (e.g., 2 mentions of "cat", 1 of "dog") * `B = (8, 4)` (e.g., 8 mentions of "cat", 4 of "dog") * `C = (1, -2)` (e.g., 1 mention of "cat", but is negatively associated with "dog")
**A and B:** These vectors are proportional (`B = 4 * A`). They point in the same direction, just with different magnitudes. Their cosine similarity is `1`, indicating they are about the same topic mix.
**A and C:** These vectors are orthogonal (perpendicular). Their dot product is `(2*1) + (1*-2) = 0`. Their cosine similarity is `0`, indicating they are unrelated.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.