Embeddings and Vector Search · Vectors · lesson 2 of 8
Cosine similarity by hand
about 18 minutes · free · runs in your browser
The angle is the answer
To compare two embeddings you compare their directions, not their positions. The measure is cosine similarity: the cosine of the angle between them.
a · b
cos = ---------
|a| · |b|
It runs from 1 (same direction — the same meaning) through 0 (unrelated) to −1 (opposite). Length is deliberately divided out, which is why a one-line question can match a paragraph: what matters is where they point, not how much text there was.
In NumPy the whole thing is three operations:
import numpy as np
a, b = np.array(v1), np.array(v2)
similarity = float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))
When both vectors are already unit length — as they are here, and as most APIs return — the denominator is 1 and cosine similarity is just the dot product. Libraries that skip the division are not cutting corners; they are relying on that.
Your turn: write cosine(v1, v2) doing the full calculation, dividing by the norms
rather than assuming them.
You start from this, and edit it in the browser:
import numpy as np
import fake_embeddings
def cosine(v1, v2):
"""Cosine similarity between two vectors, as a float."""
return 0.0