Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Vectors in linear algebra

After this lesson: I can add vectors, scale them and measure their length.

60 min lesson3 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

What is a vector?

A vector is an ordered list of numbers, such as the temperature, the rain and the bike hires of one day. Vectors are the basic data of linear algebra, which graphics, search and machine learning all rely on.

This lesson shows how to add two vectors, scale one and measure its length.

By the end of this lesson

Note

I can add vectors, scale them and measure their length.

Key words

WordWhat it meansExample
Vectoran ordered list of numbers[3, 4]
Componentone number inside a vector
Scalara single number used to scale a vector
Lengthhow far a vector reaches from zero
Unit vectora vector whose length is 1

The cells students fill in are left blank in this preview.

A vector is an ordered list of numbers

Each number in a vector is a component. The order matters, so [3, 4] and [4, 3] are different vectors. The number of components is the vector's dimension.

A day with a temperature, a rainfall and a count of hires is a vector with three components. A vector with two components can be drawn as an arrow: 3 across and 4 up.

Worked example: three days as vectors

DayTemperature in degreesRain in millimetresHiresThe vector
1 January0.90.0137[0.9, 0.0, 137]
2 January4.20.0212[4.2, 0.0, 212]
3 January3.30.0173[3.3, 0.0, 173]

The cells students fill in are left blank in this preview.

Add two weeks of hires

Command
docker exec jupyter python3 -c "import pandas as pd; k = pd.read_csv('hires.csv')['hires'].to_numpy(); w1 = k[0:7]; w2 = k[7:14]; print(w1.tolist(), w2.tolist(), (w1 + w2).tolist())"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Two weeks of hires make two vectors

The Run takes the hires of days 1 to 7 and of days 8 to 14. Each week is a vector of seven components. It printed [137, 212, 173, 327, 204, 101, 243] [221, 109, 240, 244, 281, 175, 192] [358, 321, 413, 571, 485, 276, 435].

The last list is the sum: 137 + 221 = 358, 212 + 109 = 321, and so on down the list.

Add vectors one component at a time

To add two vectors, add the components in the same place. The vectors must have the same dimension.

To scale a vector, multiply every component by a number. That number is a scalar. Scaling by 2 doubles the length. Scaling by -1 turns the vector round to point the other way.

Worked example: add and scale [3, 4]

OperationWorkingResult
a + b, where b is [1, 2]3 + 1 and 4 + 2[4, 6]
a - b3 - 1 and 4 - 2[2, 2]
2 times a2 x 3 and 2 x 4[6, 8]
-1 times a-1 x 3 and -1 x 4[-3, -4]
0.5 times a0.5 x 3 and 0.5 x 4[1.5, 2.0]

The cells students fill in are left blank in this preview.

Add, subtract and scale in NumPy

Command
docker exec jupyter python3 -c "import numpy as np; a = np.array([3, 4]); b = np.array([1, 2]); print(a + b, a - b, 2 * a, -a)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think + adds two lists of numbers component by component. In plain Python, + joins two lists end to end. Add vectors with a loop over the positions, or use a NumPy array, which adds by component.

Joining is not adding

Code: python
# Plain Python lists do not add by component.
a = [3, 4]
b = [1, 2]
print(a + b)

total = []
for i in range(len(a)):
    total.append(a[i] + b[i])
print(total)
# Change b to [10, 20] and run it again.

The length of a vector comes from Pythagoras

The length of [3, 4] is the distance from the origin to the point 3 across and 4 up. Square each component, add the squares, then take the square root. For [3, 4] that is 9 + 16 = 25, and the square root of 25 is 5.

The same recipe works for any number of components.

Worked example: the length of four vectors

VectorSquares addedLength
[3, 4]9 + 16 = 255
[6, 8]36 + 64 = 10010
[1, 2, 2]1 + 4 + 4 = 93
[5, 12]25 + 144 = 16913

The cells students fill in are left blank in this preview.

A unit vector keeps only the direction

A unit vector has length 1. To make one, divide every component by the length. [3, 4] has length 5, so its unit vector is [0.6, 0.8].

Check it: 0.36 + 0.64 = 1. The unit vector points the same way as the original and drops its size.

Measure a vector in Python

Code: python
# The length of a vector: square, add, square root.
def length(v):
    total = 0
    for x in v:
        total = total + x * x
    return total ** 0.5

print(length([3, 4]))
# Change the vector to [5, 12]. Predict the length first.

Measure one week of hires

Command
docker exec jupyter python3 -c "import numpy as np, pandas as pd; k = pd.read_csv('hires.csv')['hires'].to_numpy(); print(round(float(np.linalg.norm(k[0:7])), 1))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

One number says how big a week is

The Run measures the first week of hires as one vector of seven components. It printed 558.1.

A length turns a whole list into one number. A busier week gives a longer vector.

Going further

In your own notebook, load hires.csv and take the hires of the last seven days of the year as a vector. Find its length and compare it with the first week's 558.1.

Then divide it by its length to make a unit vector, and check the squares add up to 1.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.