Lesson 2 · Linear algebra
Vectors: lists of numbers
The problem
You run a pizza shop. You want to guess how long each delivery will take.
Two things matter most: how far away the customer is, and how bad the traffic is. So you describe each delivery with two numbers. A 5 km trip in light traffic (level 1) becomes:
That pair of numbers is a vector. Most of machine learning works on lists of numbers like this one. A photo, a sentence and a game position all become vectors before a model can use them.
This chapter covers four things you can do with vectors: add them, scale them, measure their length, and take dot products. For each one, you'll see what it means as well as how to compute it.
A vector is a list of numbers
A vector is an ordered list of numbers. Order matters: is not the same delivery as .
We write vectors in bold, like . The entries get subscripts: is the first entry and is the second. For our delivery, (km) and (traffic level).
Each entry is one measurement. In ML these measurements are called features, so a list of them is a feature vector.
A vector with two entries can be drawn as an arrow. It starts at the origin, the point , and ends at the point . The arrow picture works in 2D and 3D. With more entries you can't draw it, but the maths stays the same.
Adding and scaling
There are two basic things you can do to vectors. Both work one entry at a time.
Vector addition adds matching entries:
Scalar multiplication multiplies every entry by the same number. A single number like this is called a scalar:
Drag the arrow tips below. In the Add tab, the dashed arrows are copies. One copy of starts at the tip of , and one copy of starts at the tip of . In the Scale tab, move the slider and watch the arrow stretch, shrink and flip.
a = (2, 1) · b = (1, 2)
a + b = (2 + 1, 1 + 2) = (3, 3)
Notice two things.
First, both dashed routes end at the same point. Going then gets you to the same place as then , so order doesn't matter for addition.
Second, scaling never moves an arrow off its line. It only changes how far along the line the arrow reaches, and which way it points. At the arrow shrinks to a single point.
Length
The length of a vector is the length of its arrow. Pythagoras gives it directly. For :
The double bars mean "length of". This length is one kind of norm, and it's the one this book means by "norm". With more entries you keep adding squares: .
A vector with length 1 is a unit vector. To get one, divide any vector by its length. For example, . A unit vector keeps only the direction.
The dot product
The dot product takes two vectors and gives back one number. You multiply matching entries, then add them up.
Worked example
Dot product of a = (2, 1, 3) and b = (4, 0, −1)
Multiply matching entries: , then , then .
For vectors with entries, you do the same thing times. Writing out every term gets long, so we use sum notation:
The (capital sigma) means "add up". Read the line as: for each from 1 to , multiply by , then add all of those together.
What the number means
Predict first
Two arrows meet at a right angle (90°). What is their dot product?
In the figure, the dashed line drops from the tip of straight down onto the line through . The thick coloured segment is the shadow of along . Its proper name is the projection of onto . Drag around and watch the shadow and the number change together.
a · b = 3 × 2 + 0 × 2 = 6
Angle between them: 45° · pointing roughly the same way
Here is what to notice:
- When points roughly the same way as , the shadow points along and the dot product is positive.
- At a right angle, there is no shadow, and the dot product is zero.
- When points away from , the shadow points backwards and the dot product is negative.
- Make either arrow longer and the dot product moves further from zero.
So the dot product measures how much two vectors point the same way, scaled by their lengths. As a formula:
Here is the angle between the arrows. Read it as: length of , times length of , times . The part is 1 when the arrows line up, 0 at a right angle, and when they point in opposite directions. So decides whether the answer is positive, zero or negative.
Check it with numbers. Take and . Their dot product is . Both have length , so . That gives , so .
Drag to and to in the figure, and the readout shows the same angle.
Divide the dot product by both lengths and you get on its own:
This is called cosine similarity. It compares only direction and ignores length. For and it's : fairly similar directions. Later in the book, it's how we measure whether two words or two documents mean similar things (see embeddings).
DeeperWhy the cosine formula is trueWork out one triangle side two ways and the formula drops out. · 4 min
We have two ways to get : multiply-and-add, and lengths times . Why do they always agree? Look at the triangle made by , and the arrow from the tip of to the tip of . That third side is .
‖a − b‖² = 1² + (-1)² = 2
‖a‖² + ‖b‖² − 2 (a · b) = 5 + 5 − 2 × 4 = 2
‖a‖ = 2.24, ‖b‖ = 2.24
First way: use entries. The squared length of any vector is its dot product with itself. So:
The second line multiplies out the brackets, like . That works because the dot product is multiply-and-add, entry by entry.
Second way: use the angle. The cosine rule from school geometry gives the same side from the triangle's angle:
Put them together. Both lines describe the same side. Cancel the matching , and what's left is .
With numbers: for and , the third side is , so its squared length is . From entries: . The figure shows both calculations for any arrows you choose.
The dot product as a score
Back to the pizza shop. Say each km adds about 4 minutes, and each traffic level adds about 6 minutes. Put those numbers in a weight vector .
For the delivery , the predicted time is:
Each weight says how much its feature matters. The dot product adds up feature × weight over all the features. A sum like this is a weighted sum. It appears in almost every ML model.
Change the weights below. Each bar shows one delivery's predicted time. The blue part comes from distance and the orange part comes from traffic.
Predict first
You raise 'minutes per traffic level' from 6 to 12. Which delivery's time goes up the most?
Key idea
A model that predicts with is a linear model. Real linear models usually add a constant too, , which we'll add later. "Learning" means finding the weights that make good predictions.
DigressionVectors with 768 numbersReal models use vectors with hundreds of entries. Nothing in this chapter changes. · 2 min
The vectors in real models are long. A word in a language model might be a vector with 768 entries, or many more. You can't draw an arrow in 768 dimensions.
You don't need to. Every operation in this chapter works entry by entry, so it works for any length. Adding is still adding matching entries. The dot product is still multiply-and-add. Cosine similarity still compares direction.
One thing does change. Make two long vectors whose entries are random numbers centred on zero. They almost always end up close to a right angle, so their cosine similarity is near zero. With 768 entries it's usually within about ±0.1. So in that setting, a cosine similarity well above zero is unlikely to be chance.
Real embeddings aren't fully random like this, and their similarities are often all a bit above zero. So treat it as a rule of thumb, not a test.
Common misconceptions
"Is a vector a point or an arrow?" Both pictures are useful. As a point, is a location. As an arrow, it's a step from the origin. Adding vectors makes most sense with arrows: take one step, then the other.
"The dot product gives a vector." It doesn't. It gives a single number. Adding and scaling give vectors.
"A dot product of zero means one vector is zero." Not necessarily. It also happens when the vectors are at a right angle, like and .
Recap
- A vector is an ordered list of numbers. In 2D you can draw it as an arrow.
- Adding vectors puts arrows tip to tail. Scaling stretches an arrow along its line.
- Length comes from Pythagoras: .
- The dot product multiplies matching entries and adds them. It's positive when vectors point the same way, zero at right angles, and negative when they point apart.
- A weighted sum scores a feature vector. This is the core of a linear model.
Section 2 shows how to compute these in PyTorch.