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
| Word | What it means | Example |
|---|---|---|
| Vector | an ordered list of numbers | [3, 4] |
| Component | one number inside a vector | |
| Scalar | a single number used to scale a vector | |
| Length | how far a vector reaches from zero | |
| Unit vector | a 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
| Day | Temperature in degrees | Rain in millimetres | Hires | The vector |
|---|---|---|---|---|
| 1 January | 0.9 | 0.0 | 137 | [0.9, 0.0, 137] |
| 2 January | 4.2 | 0.0 | 212 | [4.2, 0.0, 212] |
| 3 January | 3.3 | 0.0 | 173 | [3.3, 0.0, 173] |
The cells students fill in are left blank in this preview.
Add two weeks of hires
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]
| Operation | Working | Result |
|---|---|---|
| a + b, where b is [1, 2] | 3 + 1 and 4 + 2 | [4, 6] |
| a - b | 3 - 1 and 4 - 2 | [2, 2] |
| 2 times a | 2 x 3 and 2 x 4 | [6, 8] |
| -1 times a | -1 x 3 and -1 x 4 | [-3, -4] |
| 0.5 times a | 0.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
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
# 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
| Vector | Squares added | Length |
|---|---|---|
| [3, 4] | 9 + 16 = 25 | 5 |
| [6, 8] | 36 + 64 = 100 | 10 |
| [1, 2, 2] | 1 + 4 + 4 = 9 | 3 |
| [5, 12] | 25 + 144 = 169 | 13 |
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
# 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
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.