Skip to content
BytePatterns

Cosine Similarity

AI & ML: lesson 4 of 15

Compare direction, ignore magnitude.

Lesson 4 of 15 · 5 min

Cosine Similarity

Step 1 of 11

Cosine similarity compares direction and deliberately ignores magnitude.

The Idea

Cosine similarity is the cosine of the angle between two vectors: their dot product divided by both lengths. It runs from 1 for the same direction, through 0 for unrelated, to -1 for opposite.

Real-World Example

A wholesale buyer and a corner shop send in orders for the same mix of goods, one in pallets and one in single boxes. The quantities are wildly different; the shape of the order is identical.

The Code

import math

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    na = math.sqrt(sum(x * x for x in a))
    nb = math.sqrt(sum(y * y for y in b))
    return round(dot / (na * nb), 2)

doc = [1, 2, 0]
longer_same_topic = [3, 6, 0]        # same direction, three times the length
other_topic = [0, 0, 5]
print(cosine(doc, longer_same_topic))   # 1.0
print(cosine(doc, other_topic))         # 0.0

Your turn

What does this print?

import math

def cosine(a, b):
  dot = sum(x * y for x, y in zip(a, b))
  na = math.sqrt(sum(x * x for x in a))
  nb = math.sqrt(sum(y * y for y in b))
  return round(dot / (na * nb), 2)

print(cosine([1, 0], [1, 1]))

Mini quiz

1 / 3

Cosine similarity of two vectors pointing the same way is: