The maths you need · lesson 3 of 12
Parts in this lesson
Aa @ bthe dot product (from lesson 0.6)
B(a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))cosine similarity
Cnp.degrees(np.arccos(cos))the angle between two vectors

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:

a⋅b=a1b1+a2b2+…+adbd=∑i=1daibi

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 a=(2,1) and b=(1,3): 2×1+1×3=5.

dot.ipynb

The angle formula

The same number has a second meaning. If θ (theta) is the angle between the two arrows:

a⋅b=‖a‖‖b‖cos⁡θ

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: cos⁡θ 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 a stays put; the pink "shadow" is how much of b lies along a:

Dot product diala · b = 4.24
ab

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.

dot.ipynb

Cosine similarity

Turn the angle formula round and you get the angle from the dot product:

cos⁡θ=a⋅b‖a‖‖b‖

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°:

dot.ipynb

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.

recipes.ipynb

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:

0.3×85+0.3×90+0.4×78=83.7
grade.ipynb

A linear model's prediction is exactly this: yˆ=w⋅x+b, 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:

zero.ipynb

(1,1) and (1,−1) 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

What is (1,2,3)⋅(4,0,−1)?
Predict, then check with the dial: two arrows point in exactly opposite directions. What is their cosine similarity?
Why does search by meaning use cosine similarity, not the plain dot product?

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.

All sources for Simple Machine Learning