Latticework

Command Palette

Search for a command to run...

Embeddings

Similarity Metrics

18 min

Explanation

Once you have vectors, you need a way to measure how "close" two of them are. Cosine similarity measures the angle between two vectors, not their distance — two vectors pointing in exactly the same direction score 1.0 regardless of how long each one is, opposite directions score -1.0, and perpendicular vectors score 0.0.

def dot(a, b):
    return sum(x * y for x, y in zip(a, b))

def magnitude(a):
    return sum(x ** 2 for x in a) ** 0.5

def cosine_similarity(a, b):
    return round(dot(a, b) / (magnitude(a) * magnitude(b)), 4)
Try it

magnitude() here is the exact same function from Linear Algebra's vectors-matrices module -- cosine similarity is just dot-product-over-magnitudes, built entirely from operations you've already implemented.

Loading editor…
Exercise

Write `cosine_similarity(a, b)`: return the cosine of the angle between two equal-length vectors, rounded to 4 decimal places. Cosine similarity is `dot(a, b) / (magnitude(a) * magnitude(b))`.

Quiz

Why is cosine similarity often preferred over raw Euclidean distance for comparing embedding vectors?

Checkpoint

You can compute cosine similarity from scratch and understand why it measures direction rather than distance.