Cosine Similarity
When comparing embedding vectors, the most common measure is cosine similarity. It looks at the angle between two vectors — not their length — capturing whether they point in the same direction (similar meaning) regardless of how "long" they are. This makes it the default choice for text similarity and semantic search.
💡 In one line: Cosine similarity measures the angle between two vectors — the smaller the angle, the more similar the meaning.
What is Cosine Similarity?
It measures the cosine of the angle between two vectors. The value ranges from −1 to 1 (for many text embeddings it lands roughly in 0 to 1). Crucially, it depends only on direction, not magnitude — two vectors pointing the same way score high even if one is much longer.
The Formula
- A · B — the dot product of the two vectors.
- ‖A‖, ‖B‖ — their magnitudes (lengths).
The result is the cosine of the angle between them.
Interpreting the Score
| Cosine | Angle | Meaning |
|---|---|---|
| 1 | 0° | Same direction — very similar |
| 0 | 90° | Perpendicular — unrelated |
| −1 | 180° | Opposite — opposite meaning |
Higher = more similar.
Why It's Preferred for Embeddings
- Ignores magnitude — a short and a long text about the same topic still score high.
- Focuses on direction — which is where the meaning lives.
- Robust in high dimensions — reliable even with hundreds of dimensions.
It's the default measure in most semantic-search and vector-database setups.
Cosine Similarity vs. Cosine Distance
They're two sides of the same coin:
cosine distance = 1 − cosine similarity- Similarity: higher = more alike.
- Distance: 0 = identical, higher = more different.
Example
Illustrative scores:
- "cat" vs "kitten" → 0.85 (very similar)
- "cat" vs "car" → 0.15 (barely related)
Code Example
Tip: if you normalise vectors to length 1 first, cosine similarity becomes just the dot product — which is faster at scale.
When to Use It
- Default for text and semantic similarity, where length shouldn't matter.
- If magnitude genuinely matters, consider Euclidean distance (next subtopic).
Summary
- Cosine similarity measures the angle between two vectors, ignoring magnitude.
- Formula: dot product ÷ product of magnitudes.
- 1 = same direction (similar), 0 = perpendicular (unrelated), −1 = opposite.
- It's the default for embeddings because meaning lives in direction, not length.
- Cosine distance = 1 − similarity; normalise vectors to reduce it to a fast dot product. EOF echo created