Choosing Distance Metrics (cosine, L2, dot)
When to pick cosine, L2 (Euclidean), or dot-product distance — and why most embedding models prefer cosine.
Three Common Metrics
Vector DBs let you pick a distance metric. The three you will see most:
- Cosine similarity — angle between vectors
- Dot product — projection of one onto the other
- Euclidean (L2) — straight-line distance
Cosine
Range: -1 to 1. Higher = more similar.
import numpy as np
def cosine(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))All lessons in this course
- Pinecone, Weaviate, Qdrant: Comparison
- Metadata Filtering for Hybrid Search
- Updating and Deleting Vectors
- Choosing Distance Metrics (cosine, L2, dot)