Cosine Similarity Explained: Formula, Worked Example and Python Code
Cosine similarity measures how closely two vectors point in the same direction, from -1 (opposite) to 1 (identical direction), ignoring their length. Learn the formula, work one example by hand, run it in Python and NumPy, and see why AI search and RAG depend on it.
- Cosine similarity = dot product ÷ (length of A × length of B). It compares direction, not size.
- For [3, 4] and [6, 8] it is exactly 1.0, even though one vector is twice as long; perpendicular vectors give 0 and opposite ones give -1.
- In our word-count test, cosine ranked a long, repetitive document second while Euclidean distance ranked it last.
- With vectors normalised to length 1, cosine similarity equals a plain dot product, which is why vector databases often normalise first.
Every time a search engine finds documents “like” your query, or a chatbot retrieves the right paragraph from your files, there is a good chance cosine similarity is doing the comparison. It is one of the most useful ideas in machine learning, yet it needs only school maths. All numbers below came from code we ran on Python 3.13.6, with NumPy 2.5.4 for the vectorised version.
The cosine similarity formula
cos(A, B) = (A · B) / (‖A‖ × ‖B‖)
A · B = a1×b1 + a2×b2 + ... + an×bn (dot product)
‖A‖ = √(a1² + a2² + ... + an²) (length, or norm)
The result is the cosine of the angle between the two vectors. Two arrows pointing the same way have a 0° angle, and cos 0° = 1. Arrows at right angles have cos 90° = 0. Arrows pointing opposite ways have cos 180° = -1. Because the lengths are divided out, only the direction counts.
A worked example by hand
Take A = [3, 4] and B = [6, 8]:
- Dot product: 3×6 + 4×8 = 18 + 32 = 50.
- Length of A: √(9 + 16) = 5. Length of B: √(36 + 64) = 10.
- Cosine similarity: 50 ÷ (5 × 10) = 1.0.
B is just A doubled, so they point in exactly the same direction. Our code agreed: cos(A,B) = 1.000. The Euclidean distance between them, however, was 5.0, so a distance-based measure would call them quite different.
What the values mean
| Pair | Vectors | Cosine | Meaning |
|---|---|---|---|
| Same direction | [3, 4] and [6, 8] | 1.000 | Identical direction |
| Perpendicular | [3, 4] and [4, -3] | 0.000 | Unrelated |
| Opposite | [3, 4] and [-3, -4] | -1.000 | Opposite direction |
With word counts, which are never negative, scores stay between 0 and 1. With embeddings, negative values are possible, but in practice most related text scores well above 0. Cosine distance is simply 1 minus cosine similarity, used where a library expects “smaller is closer”.
Calculate it in pure Python
import math
def cos(a, b):
dot = sum(x * y for x, y in zip(a, b))
na = math.sqrt(sum(x * x for x in a))
nb = math.sqrt(sum(y * y for y in b))
return dot / (na * nb)
print(cos([3, 4], [6, 8])) # 1.0
Ten lines, no libraries. It is perfect for learning and small jobs. Note what happens with a zero vector: cos([0, 0], [1, 2]) raised ZeroDivisionError: float division by zero in our run, because a vector with no length has no direction. Guard against it in real code.
Fast version with NumPy
import numpy as np
A, B = np.array([3, 4]), np.array([6, 8])
print(np.dot(A, B) / (np.linalg.norm(A) * np.linalg.norm(B))) # 1.0
For many vectors at once, normalise each row and use one matrix multiplication. We created 10,000 random 384-dimension vectors (384 is a common embedding size) and compared all of them with a query vector:
Mn = M / np.linalg.norm(M, axis=1, keepdims=True)
scores = Mn @ (q / np.linalg.norm(q))
top = np.argsort(-scores)[:3]
The full comparison and ranking took 25.5 ms. The top hit was the query itself with a score of 1.0, and the next best random vectors scored only about 0.18, as you would expect from unrelated random data. scikit-learn offers the same calculation as sklearn.metrics.pairwise.cosine_similarity, which we did not run here.
Tip: normalise vectors once when you store them. After that, cosine similarity is just a dot product. In our 768-dimension test, the dot product of normalised vectors matched the full formula to six decimal places (0.885104 both ways).
A text example: word counts
Turn each document into counts over a small vocabulary: python, error, install, snake, bite, pip. Then compare each with the query “python pip install error”:
| Doc | Text | Cosine | Euclidean distance |
|---|---|---|---|
| d1 | python install error pip | 1.000 | 0.00 |
| d4 | python error error error install pip pip | 0.904 | 2.24 |
| d2 | pip install python python | 0.816 | 1.41 |
| d3 | snake bite python | 0.289 | 2.24 |
Cosine ranked d4 second: it is about exactly the same topic, just longer and more repetitive. Euclidean distance tied it with d3, the snake document, because extra words increase distance whatever they mean. That is the core reason text search prefers cosine: document length should not decide relevance. Real systems use embeddings instead of raw counts, so “install failed” can match “setup error” even without shared words; see what are embeddings.
Cosine similarity vs Euclidean distance
| Aspect | Cosine similarity | Euclidean distance |
|---|---|---|
| Measures | Angle (direction) | Straight-line gap |
| Affected by length | No | Yes |
| Range | -1 to 1 | 0 to infinity |
| Best for | Text, embeddings, recommendations | Coordinates, sensor readings, clustering raw measurements |
For vectors normalised to length 1, the two give the same ranking, so the choice matters most for raw, unnormalised data.
Why AI search and RAG use it
In retrieval-augmented generation, the pattern that lets a large language model answer from your own documents, every document chunk is turned into an embedding and stored. A question is embedded the same way, and the system returns the chunks with the highest cosine similarity. We simulated this with 768-dimension vectors: a vector made by adding random noise (half the signal’s strength) to another scored 0.885 against it, while an unrelated random vector scored 0.011. Related meanings sit close in direction; unrelated ones are near 0. Vector databases speed this up with approximate nearest-neighbour indexes; see our vector database guide and what is RAG. How you split documents first also matters, as our RAG chunking strategies test showed.
From score to angle
Because the score is a cosine, you can turn it back into an angle with math.degrees(math.acos(score)). For our text example, d4 at 0.904 sat at about 25.3° from the query, d2 at 0.816 at 35.3°, and the snake document at 0.289 at 73.2°. Angles make the intuition concrete: small angles mean the same topic, angles near 90° mean little in common.
How to choose a similarity threshold
Many systems keep only matches above a cut-off, such as 0.75. There is no universal right number, because each embedding model spreads its scores differently. A practical process:
- Collect 30 to 50 real queries and mark which documents are truly relevant.
- Compute cosine scores for all query and document pairs.
- Look at the score ranges of relevant and irrelevant pairs; they usually overlap a little.
- Pick the threshold that balances missed answers against wrong ones for your use case, then re-check it whenever you change models.
Often it is safer to return the top few matches by score and let the application or the language model decide, instead of trusting one fixed cut-off.
Pitfalls to avoid
- Zero vectors: they cause division by zero; skip or handle them.
- Mixing models: embeddings from different models live in different spaces, so their cosine scores are meaningless together.
- Treating scores as percentages: 0.8 is not “80% similar”, and good thresholds differ by model. Calibrate on your own data.
- Ignoring magnitude when it matters: if size carries meaning, such as purchase amounts, cosine throws that information away.
- Integer overflow or precision: use floating point and normalise once, not on every query.
Interview-ready summary
“Cosine similarity measures the angle between two vectors: the dot product divided by the product of their lengths. It ranges from -1 to 1 and ignores magnitude, which makes it ideal for comparing text and embeddings of different lengths. Cosine distance is 1 minus the similarity, and for normalised vectors cosine similarity equals the dot product, which vector databases use for speed.” Back it up with the [3, 4] and [6, 8] example, which scores exactly 1.0 even though the vectors differ in length.
Final thoughts
Cosine similarity is the dot product of two vectors divided by their lengths, and it answers one question: do these point the same way? That makes it ideal for text and embeddings, where length is noise and direction is meaning. Learn it by hand once, use NumPy or your vector database in practice, normalise early, and calibrate thresholds on your own data.
Frequently asked questions
What is cosine similarity in simple terms?
It is a score from -1 to 1 that shows how closely two vectors point in the same direction. 1 means the same direction, 0 means unrelated and -1 means opposite.
What is the cosine similarity formula?
Cosine similarity equals the dot product of A and B divided by the product of their lengths: (A · B) / (‖A‖ × ‖B‖).
What is the difference between cosine similarity and cosine distance?
Cosine distance is 1 minus cosine similarity. Similarity is higher for closer vectors, while distance is lower for closer vectors.
Why is cosine similarity used for text and embeddings?
It ignores vector length, so a long document and a short one about the same topic still score as similar. Only the direction, which reflects meaning, counts.
How do I calculate cosine similarity in Python?
Use np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) with NumPy, sklearn.metrics.pairwise.cosine_similarity for batches, or a short pure Python function with math.sqrt.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.