Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Dot product and cosine similarity

After this lesson: I can use the dot product to say how much two vectors agree.

60 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

What is the dot product?

The dot product multiplies two vectors to give one number that says how far they point the same way. Search tools and recommendation tools use it to score how alike two things are.

This lesson shows how to work it out, and how to turn it into a score between -1 and 1.

By the end of this lesson

Note

I can use the dot product to say how much two vectors agree.

Key words

WordWhat it meansExample
Dot productmultiply matching components, then add[1, 2] and [3, 4] give 11
Orthogonalat right angles, with a dot product of 0
Anglethe turn between two vectors
Cosine similaritythe dot product divided by both lengths
Similaritya score for how alike two things are

The cells students fill in are left blank in this preview.

The dot product multiplies and adds

Multiply the matching components, then add the products. For [1, 2, 3] and [4, 5, 6] that is 1 x 4 + 2 x 5 + 3 x 6 = 4 + 10 + 18 = 32.

The answer is a single number, not a vector. Both vectors need the same dimension.

Worked example: three bike stations

Time of dayStation AStation BStation CA x BA x C
Morning841328
Noon10606
Afternoon11717
Evening741287
Dot product6128

The cells students fill in are left blank in this preview.

Work out a dot product in NumPy

Command
docker exec jupyter python3 -c "import numpy as np; a = np.array([1, 2, 3]); b = np.array([4, 5, 6]); print(a @ b, np.dot(a, b))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The sign of the dot product shows agreement

A large positive dot product means the vectors point the same way. A dot product of 0 means they are at right angles, which is called orthogonal. A negative one means they point against each other.

The dot product also grows with length. A long vector gives a big number even when the direction is only a little alike.

Worked example: reading the sign

PairDot productWhat it says
[1, 0] and [1, 0]1Same direction
[1, 0] and [5, 0]5Same direction, a longer vector
[1, 0] and [0, 1]0Right angles
[1, 0] and [-1, 0]-1Opposite directions

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think the bigger dot product always means the more alike pair. It also grows with length. Compare directions with cosine similarity, which divides the length out, and use the plain dot product only when size matters.

Cosine similarity compares direction only

Cosine similarity is the dot product divided by the two lengths multiplied together. It cancels the size and leaves the direction.

The score is 1 for the same direction, 0 for right angles and -1 for opposite directions. It is the cosine of the angle between the vectors.

Worked example: which stations are alike?

PairDot productLengths multipliedCosine similarity
A and B6110.72 x 5.74 = 61.60.99
A and C2810.72 x 9.33 = 100.00.28

The cells students fill in are left blank in this preview.

Score the stations by cosine

Command
docker exec jupyter python3 -c "import numpy as np; a = np.array([8, 1, 1, 7]); b = np.array([4, 0, 1, 4]); c = np.array([1, 6, 7, 1]); print(round(float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b))), 2), round(float(a @ c / (np.linalg.norm(a) * np.linalg.norm(c))), 2))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Station A is like B, not C

Stations A and B are busy in the morning and the evening, and quiet in between. B has fewer bikes, so its dot product is smaller. The cosine ignores that, and scores 0.99.

Station C is busy at noon and in the afternoon. It scores 0.28 with A. A vector's length is the square root of its dot product with itself.

Score two stations in Python

Code: python
# Cosine similarity of two stations, in plain Python.
def dot(a, b):
    total = 0
    for i in range(len(a)):
        total = total + a[i] * b[i]
    return total

def length(a):
    return dot(a, a) ** 0.5

cosine = dot([8, 1, 1, 7], [4, 0, 1, 4]) / (length([8, 1, 1, 7]) * length([4, 0, 1, 4]))
print(round(cosine, 2))
# Replace the second vector with [1, 6, 7, 1], both times.
# Predict whether the score goes up or down first.

Going further

In your own notebook, take the first week of hires and the first week of temperatures as two vectors. Score them by cosine similarity.

Then score week one against week two of the hires. Both scores will be high, because every number is positive. Say why that makes cosine similarity a weak test here.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.