Lesson 3 of 116 min
The dot product
Multiply two vectors' matching numbers and add them up: its sign says whether they lean the same way, and longer arrows make it bigger.
After this lesson you can compute it and read it as how much two vectors agree.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
The products, or their sum
In NumPy, * multiplies the matching numbers and stops there: you get a list of products, one for each movie. @ goes one step further and adds them up, so you get one number. .sum() on the products gives the same number.
import numpy as np
you = np.array([2, 1])
kai = np.array([-1, 2])
print(you * kai)
print((you * kai).sum())
print(you @ kai)[-2 2] 0 0
Any number of movies
The video used two movies so the arrows fit on a flat picture. The rule doesn't change with more: multiply each pair, then add them all. Here it is written out as a loop, then with @, for three movies.
you = np.array([2, 1, -2])
sam = np.array([2, 1, 1])
total = 0
for a, b in zip(you, sam):
total += a * b
print(total)
print(you @ sam)3 3
Why the ratings go from −2 to +2
Stars from 1 to 5 are never below zero, so every product is positive and two opposite tastes still get a big number. Take 3, the middle star, away from every rating first. Then "hated it" is a minus, and the sign means agree or disagree again.
you = np.array([5, 4, 1])
ola = np.array([1, 2, 5])
print(you @ ola)
print((you - 3) @ (ola - 3))18 -9
Every movie's score at once
An app doesn't loop over its movies one at a time. It stacks them as the rows of one table, and @ gives every row's dot product with you in one step. np.argsort then puts them in order; the minus sign makes it highest first.
names = ["Space Race", "Sea Song", "Big Hit", "Slow Rain"]
movies = np.array([[2, -1], [2, 1], [3, 4], [-1, 1]])
you = np.array([2, 1])
scores = movies @ you
print(scores)
order = np.argsort(-scores)
print([names[i] for i in order])[ 3 5 10 -1] ['Big Hit', 'Sea Song', 'Space Race', 'Slow Rain']
Run on Python 3.13 and NumPy 2.5.5. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Vectors
A model turns "My cat is asleep on the sofa." into a list of numbers. Whose list is closer to it: "A kitten naps on the couch." or "My car is parked on the street."?
2 of 3
You rate a comedy and a horror movie [2, 1]. Sam rates them exactly like you: dot product 5. Max rates them [2, 2]: dot product 6. Does Max's taste fit yours better than Sam's?
3 of 3
You rate two movies [1, 2]. A friend rates them [2, -2]. What is your dot product?
The ideas in this lesson
- Dot product
- Multiply the matching numbers of two vectors, then add them up: one number.
- Same way or opposite
- A dot product is above zero when two arrows lean the same way, zero at a right angle, below when they lean apart.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
The question
Here's a question. Take two movies: a comedy and a horror movie. Each one gets a score between minus two and plus two. You love the comedy: plus two.
And you like the horror movie: plus one. Your friend Kai gives the comedy minus one. But Kai loves the horror movie: plus two. So do your tastes agree, disagree, or neither?
A taste is a vector
Keep your guess.
First, your taste. Your two ratings, two and one, make a vector: an arrow, with the comedy across and the horror movie up.
A minus means you disliked a movie, so a minus points left, or down.
The dot product
How do you get one number out of two people's ratings?
Your friend Sam rates exactly like you: two, one.
Multiply the matching numbers. The comedy: two times two is four. What you get when you multiply is called a product. The horror movie: one times one is one.
Now add the two products up.
Five.
One number from two lists, made this way... is called the dot product.
Agree or disagree
So what does each product say?
If you both like a movie, or you both dislike it, the product is positive. Minus times minus is plus, just like in school. You agree on that movie.
If one of you likes it and the other doesn't, the product is negative. You disagree.
Now Kai.
The comedy: two times minus one gives minus two.
The horror movie: one times two gives plus two.
Neither
Add them up. Minus two, plus two... zero.
Neither. You agree on one movie exactly as much as you disagree on the other, so they cancel out. And look at the two arrows. They stand at a right angle. If you said "disagree": Kai dislikes your favorite movie, but agrees with you just as much on the other one. If you said "agree": Kai likes the horror movie, and dislikes your favorite just as much.
Turning away
Multiply the matching numbers, then add them up: that's the dot product.
So what happens to it as a friend's arrow turns away from yours?
Here's a faint circle. Sam's, Kai's and Ola's arrows all end on it, so they're just as long as yours. Only the direction changes. Start at Sam's place: five. Turn the arrow, and the number falls. It stays above zero while the arrow leans your way. At the right angle, it's zero. Past it, the number goes below zero. Above, zero or below: that's the number's sign. And when it points the opposite way, the number is at its lowest. Minus five. That's Ola, who hated what you loved: minus two, minus one.
Bigger, more alike?
So the bigger the number, the more alike you are. Is that always true?
A longer arrow
Here's Max, who rates everything high: two and two. Two times two is four. One times two is two. Add them: six.
That's more than Sam's five... and Sam's taste is exactly yours. So what's going on?
Max's arrow leans a little away from yours. And it's longer: it ends past the circle. Stretch an arrow to twice its length, the same way, and the number doubles. The agreement doesn't change at all. A longer arrow gets a bigger number... without agreeing more.
In a real app
This happens in real apps. An app that suggests what to watch holds you as a vector, and every movie too. The numbers are learned from what people watched. Then the app scores each movie for you with a dot product. Here's the catch. Movies that a lot of people watched tend to get long vectors. So, like Max, they can push the other movies down the list. In NumPy, the dot product of two arrays is the at sign.
In code: your list
Here's that app, in a few lines of code. You're two numbers, as before. And here are four movies, each with two numbers of its own. This loop takes each movie in turn, and prints its dot product with you. Which movie comes out on top?
Big Hit, with ten. Sea Song points exactly your way, and it gets only five. Big Hit leans away from you... but its arrow is more than twice as long. So the loop puts it first. That's how an app makes your list: one dot product for each movie, and the highest first. The top ten are your suggestions. A real app does the same for every movie it has. And a movie that a lot of people watched can still end up on top.
What you saw
So here's what you saw. Multiply the matching numbers, then add them up: that's the dot product. Above zero, two arrows lean the same way. Zero, they stand at a right angle. Below zero, they lean apart.
And a long arrow can win, without agreeing more.
Multiply in pairs, then add
So the dot product tells you how much two arrows agree. But watch out for long arrows. Now try it on this lesson's page, and take the quiz. Find a friend who disagrees with you on one movie. But their number stays above zero.
You use this lesson in Lab 1: Search a store by meaning, after Distance and cosine similarity.
Facts last checked on .
