Back
+20 XP
Concept

Cosine Similarity for Vector Search & AI Embeddings

Cosine similarity measures the angle between two multi-dimensional vectors: $\cos(\theta) = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\| \|\mathbf{v}\|}$. Ranging from $-1$ to $1$, it evaluates semantic alignment regardless of vector magnitude 🚀.

python
import math

def cosine_similarity(u, v):
    dot = sum(a * b for a, b in zip(u, v))
    nu = math.sqrt(sum(a * a for a in u))
    nv = math.sqrt(sum(b * b for b in v))
    return dot / (nu * nv) if nu and nv else 0.0

v1 = [1.0, 2.0, 0.0]
v2 = [2.0, 4.0, 0.0]  # Identical direction, 2x length
v3 = [0.0, 0.0, 5.0]  # Orthogonal (perpendicular)

print(f"Parallel: {cosine_similarity(v1, v2):.2f} 🚀")
print(f"Orthogonal: {cosine_similarity(v1, v3):.2f} 🛑")

# 54. Cosine Similarity for Vector Search & AI Embeddings Cosine similarity measures the angle between two multi-dimensional vectors: $\cos(\theta) = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\| \|\mathbf{v}\|}$. Ranging from $-1$ to $1$, it evaluates semantic alignment regardless of vector magnitude 🚀. ```python import math def cosine_similarity(u, v): dot = sum(a * b for a, b in zip(u, v)) nu = math.sqrt(sum(a * a for a in u)) nv = math.sqrt(sum(b * b for b in v)) return dot / (nu * nv) if nu and nv else 0.0 v1 = [1.0, 2.0, 0.0] v2 = [2.0, 4.0, 0.0] # Identical direction, 2x length v3 = [0.0, 0.0, 5.0] # Orthogonal (perpendicular) print(f"Parallel: {cosine_similarity(v1, v2):.2f} 🚀") print(f"Orthogonal: {cosine_similarity(v1, v3):.2f} 🛑") ``` Vector databases (like Pinecone, Milvus, and Qdrant) use cosine similarity to retrieve relevant chunks for Retrieval-Augmented Generation (RAG) pipelines 🧠. --- ### Question Why is cosine similarity preferred over Euclidean distance when comparing text embeddings of unequal document lengths? 🧠 - [ ] a. It isolates directional orientation and ignores vector magnitude differences caused by word counts 🚀 - [ ] b. It eliminates multiplication, guaranteeing O(1) constant runtime execution - [ ] c. It automatically compresses floating-point vectors into 8-bit integer buffers - [ ] d. Euclidean distance cannot be calculated when vectors exceed three dimensions --- ### Challenge Write `cosine_similarity(u, v)`. Compute the similarity between `u = [1, 2, 3]` and `v = [2, 4, 6]`. Print `round(val, 2)`. 🎯 <details> <summary>Solution</summary> ```python import math def cosine_similarity(u, v): dot = sum(a * b for a, b in zip(u, v)) nu = math.sqrt(sum(a * a for a in u)) nv = math.sqrt(sum(b * b for b in v)) return dot / (nu * nv) if nu and nv else 0.0 val = cosine_similarity([1, 2, 3], [2, 4, 6]) print(round(val, 2)) ``` </details>

Checkpoint

Why is cosine similarity preferred over Euclidean distance when comparing text embeddings of unequal document lengths? 🧠

Practice

Write `cosine_similarity(u, v)`. Compute the similarity between `u = [1, 2, 3]` and `v = [2, 4, 6]`. Print `round(val, 2)`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks