The idea
Cosine similarity measures the angle between two vectors, ignoring their length. A score of 1 means the same direction, 0 means unrelated, and negative means opposite. For text embeddings, most real scores fall between about 0.2 and 0.9.
Formula: dot(a, b) / (|a| × |b|). If you normalise vectors to length 1 when you store them, cosine similarity is just the dot product, which is faster. Vector databases offer cosine, dot product and Euclidean (L2) distance; with normalised vectors they rank results the same way.
Scores are relative, not absolute. A 0.62 might be a great match with one model and a poor one with another. Use scores to rank, and tune any cut-off threshold on real examples.
It is the scoring function behind every nearest-neighbour search you will run.
Words to know
- Dot productSum of pairwise products of two vectors.
- NormalisationScaling a vector to length 1.
- Cosine distance1 − cosine similarity; used by some databases.
Try it without code
Do it by hand
- Take vectors a = [1, 2] and b = [2, 4].
- Compute dot / (lengths). You get 1: same direction.
- Try b = [-2, 1] and get 0.
Use it in code
Cosine from scratch and with normalised vectors.
import numpy as np
def cosine(a, b):
return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))
a, b, c = np.array([1, 2]), np.array([2, 4]), np.array([-2, 1])
print(cosine(a, b), cosine(a, c))
an = a / np.linalg.norm(a); bn = b / np.linalg.norm(b)
print(float(an @ bn)) # same as cosine once normalisedPick a similarity threshold for "is this a duplicate question?" by scoring 20 labelled pairs and choosing the cut-off with fewest mistakes.
Common mistakes
- Treating 0.8 as universally "very similar".
- Using cosine on unnormalised vectors with an index set to dot product.