Cosine Similarity
How closely two vectors point in the same direction, as the cosine of the angle between them. It ignores length.
Synthetic dataExamples on this page use synthetic data created for illustration.
Definition
Cosine similarity measures how closely two vectors point in the same direction, as the cosine of the angle between them. It ranges from −1 to 1 and ignores length, so two texts with the same proportions of words score 1 however long they are.
In plain terms
Represent each text as a list of numbers, for example word counts or an embedding. Two lists are similar when their direction agrees, whatever their size. Cosine similarity computes that by taking the dot product and dividing by the product of the lengths.
A value of 1 means the same direction, 0 means unrelated (perpendicular), and −1 opposite. For word counts, which are never negative, the range is 0 to 1. The score depends on how the text was turned into numbers: it compares representations, not meanings.
Example
| Vector A | Vector B | Cosine similarity | Why |
|---|---|---|---|
| (3, 4) | (6, 8) | 1.00 | Same direction; B is just twice as long |
| (3, 4) | (4, −3) | 0.00 | At right angles |
| (1, 2, 0) | (2, 1, 1) | 0.73 | Roughly similar direction |
A short note and a long report that use the same words in the same proportions would score 1, although one is far longer; that is the point of ignoring length.
Common misunderstandings
- “Cosine similarity is a distance.” It is a similarity; a related distance is one minus the cosine.
- “A high score means the texts mean the same.” It means their vectors are close, which is only as meaningful as the representation.
- “Scores from different models are comparable.” A 0.8 in one embedding model is not a 0.8 in another.
References
- Christopher D. Manning, Prabhakar Raghavan, Hinrich Schütze. Dot products, in Introduction to Information Retrieval . Cambridge University Press, 2008.
- Daniel Jurafsky, James H. Martin. Speech and Language Processing . Stanford University (draft, 3rd edition).