Careeroza
Measuring Similarity
Three Common Measures
| Measure | Formula | Notes |
|---|---|---|
| Dot product | Σ aᵢ·bᵢ | Fast; affected by vector length |
| Cosine similarity | a·b / (|a|·|b|) | Only the angle matters; from -1 to 1. The usual choice for text |
| Euclidean distance | √Σ (aᵢ-bᵢ)² | Smaller = more similar |
If vectors are normalized (length 1), dot product and cosine give the same ranking, and Euclidean distance ranks the same way too. Many embedding APIs return normalized vectors.
Length Changes the Dot Product, Not the Cosine
Example
Vector b is just a made 3× longer (the same direction, like the same topic said more strongly):
import math
def dot(a, b): return sum(x * y for x, y in zip(a, b))
def norm(a): return math.sqrt(dot(a, a))
def cosine(a, b): return dot(a, b) / (norm(a) * norm(b))
def euclid(a, b): return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))
a = [0.6, 0.8, 0.0]
b = [1.8, 2.4, 0.0] # same direction, 3x longer
c = [0.0, 0.6, 0.8] # different direction
for name, v in [("b", b), ("c", c)]:
print(f"a vs {name}: dot={dot(a, v):.2f} cosine={cosine(a, v):.2f} euclid={euclid(a, v):.2f}")Output
a vs b: dot=3.00 cosine=1.00 euclid=2.00 a vs c: dot=0.48 cosine=0.48 euclid=1.02