The maths you need · lesson 2 of 12
Parts in this lesson
Aa + b, 2 * a, a - badding, scaling and subtracting arrays (from Stage 0)
Bnp.linalg.norm(a)a vector’s length
Cnp.linalg.norm(p - q)the distance between two points

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 a=(3,1) has two components: 3 and 1. Maths writes vectors in bold, and a1 means the first component, a2 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 (39.1,18.7): 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:

a+b=(a1+b1,a2+b2)

As arrows: put b’s tail on a’s tip, and the sum goes from the start of a to the tip of b. To scale a vector by a number c, multiply every component by c: ca=(ca1,ca2). That stretches the arrow, or flips it round if c is negative. Subtracting is adding −b.

Drag the sliders. Switch to "a − b" to see the arrow that goes from b’s tip to a’s tip:

Arrow boarda = (3, 1) b = (1, 2)
a + bab

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:

vectors.ipynb

Gradient descent (Stage 3) is exactly this: the new weights are the old weight vector plus a small scaled vector, w−ηg.

Length (the norm)

A vector's length, called its norm, is written ‖a‖. For a=(3,4) it's the long side of a right triangle with sides 3 and 4, so Pythagoras gives it:

‖a‖=a12+a22+…+ad2=∑i=1dai2

In words: square every component, add them up, and take the square root. d is the number of components, and ∑i=1d means "add up, for i from 1 to d". In NumPy: np.sqrt((a ** 2).sum()), or just np.linalg.norm(a).

norms.ipynb

(1,2,2) has length 3, because 1+4+4=9. 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:

distance(p,q)=‖p−q‖

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:

norms.ipynb

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:

bad.ipynb

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

What is (2,5)+(1,−3)?
Predict, then check with np.linalg.norm: what is the length of (6,8)?
Two penguins are p and q. Which gives the distance between them?

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