The dot product
Last updated
Multiply two vectors component by component and add up the results, and you get one number: the dot product. It's the most used operation in machine learning. Every prediction in Stages 3 to 6 starts with one, and it also measures how much two arrows point the same way, which is how search engines and word embeddings decide what's similar.
By the end of this lesson you can
- Compute a dot product by hand and with the @ operator
- Read the sign of a dot product as agree, unrelated or opposed
- Use cosine similarity to compare two vectors whatever their lengths
- Recognise a weighted sum as a dot product
Multiply and add
The dot product of two vectors with the same number of components multiplies the matching components and adds them up:
In words: pair up the components, multiply each pair, add. The answer is one number, not a vector. In NumPy: a @ b (lesson 0.6 met it as a weighted sum).
For and : .
The angle formula
The same number has a second meaning. If (theta) is the angle between the two arrows:
In words: the dot product is the two lengths multiplied, times the cosine of the angle between them. You don't need trigonometry beyond this: is 1 when the arrows point the same way, 0 when they're at a right angle, and −1 when they point opposite ways.
Turn the dial. Arrow stays put; the pink "shadow" is how much of lies along :
a · b = ‖a‖ ‖b‖ cos θ = 3 × 2 × 0.707 = 4.24. Positive: b points partly the same way as a. Its shadow on a’s line (pink) falls in front. The cosine on its own, 0.707, ignores the lengths.
So the sign tells you how two arrows relate: positive means they broadly agree, zero means they're at right angles, negative means they're opposed.
Cosine similarity
Turn the angle formula round and you get the angle from the dot product:
In words: the dot product divided by both lengths. This is cosine similarity: 1 for the same direction, 0 for unrelated, −1 for opposite. In NumPy: (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b)). For step 1's vectors it's 0.7071, an angle of 45°:
Here it is comparing recipes by how often each uses five words: rice, chicken, garlic, sugar and flour. Adobo and arroz caldo share their words; polvoron shares none.
The second cell shows why the division matters. A recipe ten times as long has a dot product ten times bigger (27 becomes 270), but its cosine similarity is still 0.813: it's about the same things. Cosine similarity compares direction and ignores length. Word embeddings (11.5), search by meaning (11.11) and matching pictures to captions (14.6) all use it.
A weighted sum is a dot product
A class grade where quizzes count 30%, the project 30% and the exam 40% is a dot product of the weights and the scores:
A linear model's prediction is exactly this: , a weight for each feature, dotted with the features, plus a bias. Stage 3 learns the weights from data, and every artificial neuron, from Stage 5 on, starts with the same dot product.
Wrong way: a dot product of 0
It's tempting to read a dot product of 0 as "nothing different", as if the vectors matched. It means the opposite:
and have the same length (each dotted with itself gives 2), but their dot product is 0: they're at a right angle, as unrelated as two directions can be. Vectors that are "the same" have a cosine similarity of 1. If you're checking whether two vectors match, compare them with np.allclose(a, b), not by their dot product.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 2: Linear Algebra: the dot product (2.2) and its angle form (2.5)
- NumPy: numpy.dot: the dot product of two 1-D arrays
- NumPy: numpy.matmul: the @ operator