AI engineering glossary · Linear algebra

What is cosine similarity?

Cosine similarity: A score between -1 and 1 for how closely two vectors point in the same direction, ignoring their lengths. It equals the dot product divided by the product of the two norms, and is widely used to compare text embeddings.

Level: beginnerTopic: Linear algebra

Cosine similarity: an example

For a = [1, 0] and b = [1, 1], cos = (1) / (1 * 1.414) = 0.707, an angle of 45 degrees.

Cosine similarity in Python

A short example you can run (Python 3.11, NumPy). The output below is what it really prints.

import numpy as np

def cosine(a, b):
    return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))

a = np.array([1, 2, 3])
print(round(cosine(a, np.array([2, 4, 6])), 3))    # same direction
print(round(cosine(a, np.array([3, 0, -1])), 3))   # perpendicular
print(round(cosine(a, -a), 3))                     # opposite

Output

1.0
0.0
-1.0

Related terms

More Linear algebra terms

Where you learn it

Cosine similarity is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.