What Is Cosine Similarity? A Plain Explanation for Beginners
Cosine similarity is a number that measures how alike two vectors are by the angle between them, ignoring how long the vectors are. It runs from -1 to 1, where 1 means the vectors point the same way, 0 means they are unrelated, and -1 means they point in opposite directions.
Why "similarity" becomes an angle
An embedding represents a piece of text as a list of numbers, which you can picture as an arrow in space. Two pieces of text with similar meanings end up with arrows that point in roughly the same direction. Text with different meanings points in different directions.
Cosine similarity reads that angle. If the arrows are close together, the angle is small and the score is high. If they are far apart, the score is low.
The formula in plain terms
The score is the dot product of the two vectors divided by the product of their lengths:
cosine(A, B) = (A · B) / ( |A| × |B| )
In everyday steps:
- Multiply each pair of matching numbers in the two vectors.
- Add the results, which gives the dot product.
- Divide by the length of each vector so the result depends on direction, not size.
Reading the score
| Score | Meaning | Example intuition |
|---|---|---|
| 1 | Same direction | Very close in meaning |
| Near 1 | Small angle | Strongly related |
| 0 | Perpendicular | No clear relation |
| Negative | Opposing directions | Contrasting or opposite |
A related number, cosine distance, is simply 1 - cosine similarity. With that version, a smaller value means the vectors are closer, which is handy in search ranking.
Why size is ignored
The key property is that cosine similarity looks at orientation and is not affected by the magnitude of a vector. A short document and a long document can still point in the same direction if they are about the same idea. This makes the measure robust to differences in length and is the main reason it is popular for text.
Many embedding models return normalized vectors, meaning each arrow has a unit length. For normalized vectors, the cosine score and the dot product give the same ranking, which keeps retrieval simple.
Where it is used
Cosine similarity is a default comparison in systems built on embeddings:
- Semantic search ranks results by how close their vectors are to the query.
- RAG and vector databases use it to find the passages most worth retrieving.
- Recommendation systems compare items or users in the same way.
- Contrastive training uses the score to pull related examples together and push unrelated ones apart.
How it compares with other measures
| Metric | What it focuses on | Note |
|---|---|---|
| Cosine similarity | Angle between vectors | Ignores magnitude; safe default for text |
| Dot product | Direction and magnitude | Equals cosine for normalized vectors |
| Euclidean distance | Straight-line gap | Sensitive to vector length |
For most modern text embeddings, cosine similarity is a sensible default. Switching to another metric is usually based on the model's documentation or a specific reason rather than preference.
FAQ
What does a cosine similarity of 1 mean?
The two vectors point in exactly the same direction, so they are treated as maximally similar.
Does a score of 0 mean the texts are identical?
No. A score near 0 means the vectors are perpendicular, which indicates no clear relationship rather than sameness.
Why is it called "cosine"?
Because the value is the cosine of the angle between the two vectors, the same trigonometric idea used with triangles.
Is cosine similarity the same as the dot product?
They give the same ranking when vectors are normalized to unit length. Otherwise, the dot product also reflects magnitude while cosine does not.
What is cosine distance?
It is one minus the cosine similarity, flipped so that lower values mean closer matches.
Key takeaways
- Cosine similarity measures the angle between two vectors, from -1 to 1.
- It ignores vector length and focuses on direction, which suits text embeddings.
- It powers semantic search, RAG, and vector databases.
- For normalized vectors it matches the dot product, keeping retrieval straightforward.
Continue with what are embeddings and what is semantic search.
Sources
Learn by doing
Read a guide, then practice with bite-sized AI lessons you can take offline.
Start learning free