How does a computer “see” a photo, a house for sale, or the word “cat”? It sees a list of numbers. This lesson shows why a list of numbers is also an arrow and a point, and why that one idea runs through all of machine learning.
Start with one number: scalars
A scalar is a single number: a temperature of 21.5 °C, a price of 4.00, a loss of 0.37. In ML you will meet scalars all the time: the learning rate , a bias , the loss . On this site scalars are written in plain italics: , , .
Some things need more than one number. If a car’s speed is 50 km/h, one number is enough. But its velocity is “50 km/h to the north-east”: a size and a direction, which takes more than one number to say. Things like this are vectors.
A vector is an ordered list of numbers. The simplest examples have two: can mean “3 steps right and 1 step up”. On this site vectors are written in bold lowercase: , , .
One object, three pictures
The vector can be seen in three ways. They are not three different things. They are three views of the same thing. Drag the tip of the arrow and watch all of them change together.
- As an arrow: it starts at the origin (the point ) and has a length and a direction.
- As a list: two numbers in a fixed order, here .
- As a point: the place where the arrow’s tip lands. To go from the point view to the arrow view, draw an arrow from the origin to the point.
Each view is best at a different job, so good problem solvers switch between them:
| View | Best for | Weak spot |
|---|---|---|
| Arrow | Directions, movements and “which way is downhill?”; seeing length and angle | You can only draw up to three entries |
| List | Computing, and writing code; works for any number of entries | Hides the geometry |
| Point | Data: “where does this item sit?”, and “which items are close together?” | Shows only where you end up, not the move that got you there; it also needs a chosen origin |
Intuition
Imagine giving directions to a friend: “walk 3 blocks east, then 1 block north”. That is an arrow (a list of moves). Now think of a street address: “the corner of 3rd and 1st”. That is a point. Both can use the same two numbers, 3 and 1. The difference is the starting place: the directions start from wherever your friend stands, and the address is measured from a fixed starting point on the map (the origin).
The analogy also shows what each view needs. The directions (the arrow) and the list of numbers need nothing else: “3 east, 1 north” makes sense wherever you stand. The address (the point) needs a map with an agreed origin, and so does the point view of a vector: as a point means “3 right and 1 up from the origin”.
Math
A vector with entries is written
and we say (as in P1.A4: “the set of all lists of real numbers”). The numbers are the entries or components. The small number is the index: it says which position the entry is in.
Two vectors are equal only if they have the same number of entries and every entry matches: means for each . So , and .
Code
import numpy as np
v = np.array([3.0, 1.0]) # the vector (3, 1)
print(v) # [3. 1.]np.array takes a Python list of numbers and makes a vector you can compute with. The bigger code cell near the end of this lesson explores it in detail.
Components, and why order matters
In the widget, the two numbers have jobs. The first entry says how far to go right (or left, if negative). The second says how far to go up (or down). Press Swap the two entries: the arrow for becomes the arrow for , a different vector. Swapping flips the arrow over the diagonal line . Order matters.
That is true in data too. A house with means area 120 m², 3 bedrooms, 15 years old. If you swap the first two numbers, you describe a house with 3 m² and 120 bedrooms. A vector only means something because every position always means the same thing.
Reading entries. We write the entries of as . For the house, is the number of bedrooms. Maths books count positions from 1. Python and NumPy count from 0, so the same entry is x[1]. Keep both in mind: in the formulas below we use the math way, and in the code we use the NumPy way.
Arrows are free to move
Look at the widget again and tick Show copies that start elsewhere. The dashed arrows begin at other points but go the same distance in the same direction. They have the same entries, so they are the same vector. A vector says how to move, not where to start.
This is the difference between a position and a displacement:
- A position is where something is: the point , measured from the origin.
- A displacement is how to get from one place to another. From to you move 3 right and 1 up: the displacement is . (You get it by subtracting entry by entry: and . P1.B2 treats this properly.)
When we draw a vector as a point, we are choosing to start every arrow at the origin. That is a convention that makes arrows and points match one to one. It does not mean arrows are tied to the origin.
Row or column? A note on notation
A list can be written sideways or stacked:
On this site the rule is: a vector is a column by default. We often write it inline as because that saves space; it is still the column. The small is the transpose, which turns a column into a row (and a row back into a column). For now the difference does not matter, because both hold the same numbers in the same order. It starts to matter with matrices (P1.B5, P1.B6), where “row times column” is how multiplication works.
One thing to know now: a plain NumPy vector, like np.array([3.0, 1.0]), is neither a row nor a column. It has shape (2,), just “2 entries”. A column would have shape (2, 1) and a row (1, 2). P1.B14 explains shapes in depth.
Dimension and ℝⁿ
The dimension of a vector is its number of entries. has dimension 2. has dimension 3. A vector with entries lives in :
| Entries | Lives in | Picture |
|---|---|---|
| 1 | (or just ) | the number line |
| 2 | a flat plane | |
| 3 | the space around you | |
| impossible to draw, easy to write |
Here is a vector with three entries. Move the sliders to set the entries, and turn the view to see the arrow from other sides.
We cannot draw four or more dimensions, but we do not need to. Everything you learn here works the same way for any number of entries: you add one more number to the list. In ML the lists are long. A picture of a handwritten digit has numbers, so it is a point in .
Dimension is not length. Two different ideas share the word “size”:
- The dimension is how many entries: and both have dimension 2.
- The length of the arrow is how far its tip is from the origin. By Pythagoras, has length and has length exactly . P1.B4 studies lengths (norms) properly.
Careful with a third meaning in code. In NumPy, ndim counts axes, not entries. A vector with 784 entries has ndim 1: it is a “1-D array”, even though it is a vector in .
What is a “space”?
When mathematicians say space, they mean the collection of all vectors of one kind, plus the freedom to move around in it. The space is every possible pair of real numbers: every point of the flat plane. is every possible list of 784 real numbers. Each possible list is exactly one point of the space, and each point is exactly one list.
In ML you will keep meeting three spaces:
- Feature space. Every possible data point with features is one point of . Similar items sit close together, which is what k-nearest neighbors uses (P3.B1).
- Embedding space. Words, images or users are mapped into so that similar meanings are close (P6.3).
- Parameter space. A model with adjustable numbers has one point in for every possible setting of those numbers. Training means moving through this space to a better point (P1.F3).
Here “space” is the friendly idea: the place where vectors live. The formal definition (what a subspace is, what a basis is) comes in P1.B7.
The zero vector
The vector whose entries are all zero is the zero vector, written . In the plane it is .
- As a point, it is the origin.
- As an arrow, it has length 0: it is a dot with no direction. “Which way does it point?” has no answer. (You will see that this matters when we define angles and cosine similarity in P1.B3.)
- As a displacement, it means “do not move”.
The widget shows it when you press Zero vector. A tiny point on the origin, but a perfectly valid vector. Note that is not the same object as the scalar : the vector has one zero for each entry, so the zero vector in is , and the zero vector in has 784 zeros.
Vectors everywhere in machine learning
Once you can see a list of numbers as a point or an arrow, an enormous amount of ML is vectors:
| What | As a vector | Example |
|---|---|---|
| A data row | one number per feature | house : area, bedrooms, age; it is a point in |
| A grayscale image | one number per pixel, read row by row (a colour image has 3 numbers per pixel: red, green, blue) | a picture is a vector with entries (that is the size of the MNIST handwritten digits) |
| A word (one-hot) | a 1 in the word’s slot, 0 elsewhere | with a 4-word vocabulary, “cat” can be (P6.1) |
| A word (embedding) | a learned list of numbers, so similar words are close | classic word embeddings (word2vec, P6.3) have a few hundred numbers per word; in GPT-2 small every token gets |
| Model weights | one weight per feature | a linear model for houses has , the same length as |
| All the parameters | every adjustable number in the model, stacked into one list | a point in parameter space; training moves it |
(The house and weights are invented for illustration. The 784 and the 768 are real: see the Fact-check in this lesson’s plan.)
How an image becomes a vector. A computer stores a picture as a grid of brightness numbers. To use it as one vector, we read the grid row by row and write the numbers in one long line. Try it on a tiny image: change the pixels and watch the vector.
No numbers are lost: the vector has 9 entries for 9 pixels, and we know which position goes with which pixel. One thing is harder to see, though: pixels 0 and 3 sit on top of each other in the picture but are 3 apart in the list, so the list hides which pixels are neighbours. Convolutional networks (P5.2) are built to use that neighbourhood again; P5.1 also covers colour images. Still, the vector is now something we can compute with. Notice also that we must always use the same reading order for every image; if one image were read by rows and another by columns, the same position would mean different pixels.
Vectors in NumPy
NumPy stores a vector as a 1-D array. Run the cell and read each line against the comment above it.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Things to take from it:
shape: (2,)means “a 1-D array with 2 entries”.len(v)is the dimension;ndimis the number of axes (here 1).- Indexing starts at 0, so in the math is
v[0]in code, and isv[n-1]. The index-1means “the last entry”. A slice likehouse[1:3]takes positions 1 and 2 (it stops before 3). np.zeros(3)is the zero vector of .flatten()reads a 2-D image row by row, giving a vector.- A 0-D array, such as
np.array(5.0), has shape(); a one-entry vector has shape(1,). They behave alike in many places but are different objects.
Go deeper A fourth view: anything you can add and scale
Mathematicians use the word “vector” for any kind of object that you can add together and stretch by a number, as long as a few sensible rules hold (the rules are the “axioms” of a vector space). Lists of numbers are the main example, and the only one you need for most of machine learning. But polynomials, for instance, also fit: adding and gives , and stretching by 2 gives . A polynomial like is then described by the pair , which is a vector in again.
So “what makes something a vector” is not that it looks like a list, but that adding and scaling behave well. P1.B2 teaches the adding and scaling for lists, and P1.B7 comes back to the formal definition, subspaces and basis. For this lesson, a vector is an ordered list of real numbers.
Go deeper Points and vectors are not exactly the same thing
Careful authors separate points (positions) from vectors (displacements). You can subtract two points to get a vector (B minus A is the displacement from A to B), but adding two points (“Paris plus Berlin”) makes no sense. In there is a special point, the origin, and it identifies each point with the displacement that gets you there from the origin. That is why we can freely move between “the point ” and “the vector ”. In spaces without a natural origin the two ideas must be kept apart.
Go deeper Columns by default, but data rows in code
We write vectors as columns so that the product of a matrix and a vector reads left to right: (P1.B5). Many ML libraries do it the other way for data: a table of examples with features is stored with one example per row, shape . Each row is then one example’s vector. Neither is wrong; they are conventions. When you read a paper or a library’s documentation, check which one is used.
Check your understanding
Which of these is a scalar?
A scalar is a single number. The other three each hold several numbers in a fixed order, so they are vectors.
What is the dimension of ?
The dimension is the number of entries, and there are 4 (the zero counts). The value is the length of the arrow, which is a different thing.
A image is read row by row into one vector. How many entries does it have?
One entry per pixel: 28 × 28 = 784. (56 would be 28 + 28, which counts rows and columns instead of pixels.)
One arrow goes from to . Another goes from to . How are they related?
A vector is defined by how far it moves: 3 right, 1 up, in both cases (4 − 1 = 3 and 3 − 2 = 1). Where an arrow starts does not change which vector it is.
In NumPy,
v = np.array([7., 8., 9.]). What isv[1]?NumPy counts positions from 0, so v[0] is 7.0, v[1] is 8.0 and v[2] is 9.0. In maths notation the same entry is v₂.
Exercises
- Write the vector for “3 steps left and 2 steps up”.
Show answer
: left is negative in the first entry, up is positive in the second. - Give the dimension of , of and of a image read into one vector.
Show answer
2, 5 and 400 (that is 20 × 20 pixels). - Find the displacement vector and its length for the arrow from to .
Show answer
. Its length is . - Is a vector in , or ?
Show answer
Only in , because it has exactly 3 entries. - A house is with entries (area, bedrooms, age). Which entry is the number of bedrooms, in maths notation and in NumPy?
Show answer
in maths (counting from 1) andx[1]in NumPy (counting from 0). Its value is 2. - Read the image with rows and into a vector row by row, and then column by column. Why must you pick one order and keep it?
Show answer
Row by row: . Column by column: . The two lists differ, so for the same position to always mean the same pixel, every image must use the same order. - In the code cell, what is the shape of
np.zeros(5), and what happens if you ask forv[5]on it?Show answer
The shape is(5,). Positions are 0 to 4, sov[5]raises anIndexError. - In the widget, what does “Swap the two entries” do to the arrow, and for which vectors does it change nothing?
Show answer
It reflects the arrow in the diagonal line , turning into . It changes nothing when the two entries are equal, such as or the zero vector.