Vectors
Last updated
A penguin, to a model, is a short list of numbers: bill length, bill depth, flipper length, body mass. That list is a vector. Seeing it as an arrow as well as a list is what lets you say two penguins are close or far apart, and nearly every model in this course leans on that.
By the end of this lesson you can
- Read a vector both as an arrow and as a list of numbers
- Add, subtract and scale vectors, by hand and with NumPy
- Work out a vector’s length (its norm) and the distance between two points
A vector as an arrow and as a list
A vector is a list of numbers in a fixed order. The vector has two components: 3 and 1. Maths writes vectors in bold, and means the first component, the second.
With two components you can also draw it: an arrow from the origin, 3 steps right and 1 step up. Both views are the same thing. The list is how a computer stores it (a 1-D NumPy array); the arrow is how you reason about it.
A penguin with bill length 39.1 mm and bill depth 18.7 mm is the vector : a point on a scatter plot, like the ones in lesson 0.8. With all four measurements it's a vector with 4 components. You can't draw four dimensions, but every rule in this lesson works the same way: the sums just get longer.
Adding and scaling
To add two vectors, add the matching components:
As arrows: put ’s tail on ’s tip, and the sum goes from the start of to the tip of . To scale a vector by a number , multiply every component by : . That stretches the arrow, or flips it round if is negative. Subtracting is adding .
Drag the sliders. Switch to "a − b" to see the arrow that goes from ’s tip to ’s tip:
a + b = (4, 3): walk along a, then along b. Lengths: ‖a‖ = 3.16, ‖b‖ = 2.24, ‖a + b‖ = 5.
In NumPy all of this is the element-by-element maths from lesson 0.4:
Gradient descent (Stage 3) is exactly this: the new weights are the old weight vector plus a small scaled vector, .
Length (the norm)
A vector's length, called its norm, is written . For it's the long side of a right triangle with sides 3 and 4, so Pythagoras gives it:
In words: square every component, add them up, and take the square root. is the number of components, and means "add up, for from 1 to ". In NumPy: np.sqrt((a ** 2).sum()), or just np.linalg.norm(a).
has length 3, because . Three components, same rule. This kind of length is sometimes called the L2 norm; Stage 3 uses it to keep a model's weights small (regularisation, lesson 3.8).
Distance between two points
The distance between two points is the length of the arrow from one to the other, and that arrow is their difference:
In words: subtract, then take the length. In NumPy: np.linalg.norm(p - q). Here it is for three real penguins, using bill length and bill depth:
The first two penguins (an Adelie and a Gentoo) are 8.9 mm apart. The first and third (both Adelie) are only 1.39 mm apart. So "near in measurements" lines up with "same species" here. That idea is a whole model: k-nearest neighbours (Stage 2) labels a new penguin by looking at the penguins nearest to it, measured exactly like this.
Wrong way: vectors of different lengths
Adding only makes sense when both vectors have the same number of components. Here's a vector with 3 components plus one with 2:
NumPy stops with operands could not be broadcast together with shapes (3,) (2,). That's the right answer: there's no sensible way to pair them up. In real work this usually means one row of data lost a column, or the features were picked differently for two arrays. When you see it, print both .shapes and find where they parted.
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: vectors (2.1), adding them (2.1) and norms (2.5)
- NumPy: numpy.linalg.norm: the length of a vector
- NumPy: Broadcasting: why arrays of lengths 3 and 2 can’t be added