Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

Scalars, vectors and spaces

P1.B125 min●○○ BeginnerReviewed Nov 30, 2026

Why this matters for machine learning

Almost everything a machine-learning model touches is a vector: the data it reads, the numbers it learns and the answers it gives.

  • Every training example is a vector of features (P3.A1), and a linear model's weights are a vector of the same length. (P3.A2: Linear regression, coming soon)
  • An image goes into a neural network as one long vector of pixel values. (P4.10: Build an MLP from scratch (NumPy), coming soon)
  • Every word or token is turned into a vector (an embedding), so that similar meanings get nearby vectors. (P4.21: Embedding layers, coming soon)
  • Nearby points in a space of vectors are similar items, which is the whole idea behind k-nearest neighbors. (P3.B1: k-Nearest Neighbors, coming soon)

Before this: Sets, notation and reading math

After this lesson you can

  • Tell a scalar from a vector, and read one vector as an arrow, a list of numbers and a point
  • Read the components of a vector, and explain why their order matters
  • State the dimension of a vector, say what Rn\mathbb{R}^n is, and explain what “space” means
  • Explain that an arrow can slide without changing, and recognise the zero vector
  • Describe how data rows, images, embeddings and model weights are vectors, and make vectors in NumPy

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 η\eta, a bias bb, the loss LL. On this site scalars are written in plain italics: xx, ww, bb.

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: (3,1)(3, 1) can mean “3 steps right and 1 step up”. On this site vectors are written in bold lowercase: v\mathbf{v}, x\mathbf{x}, w\mathbf{w}.

One object, three pictures

The vector (3,1)(3, 1) 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.

Drag the tip of the arrow v

−4−224−4−224v₁ = 3v₂ = 1

As an arrow

From the origin, go 3 right, 1 up.

Length ≈ 3.16 (Pythagoras; P1.B4)

As a list

v = (3, 1)

entries v1 = 3, v2 = 1

As a column

31

2 entries: in ℝ²

As a point

the point at x = 3, y = 1

where the arrow's tip lands

One object, three pictures: the arrow, the list (3, 1) and the point share the same two numbers.

One vector, three views (the list is shown both sideways and as a column). Drag the tip of the arrow, or Tab to it and use the arrow keys. The arrow, the list, the column and the point always show the same two numbers. Try 'Swap the two entries', 'Zero vector' and the copies checkbox.
  • As an arrow: it starts at the origin (the point (0,0)(0, 0)) and has a length and a direction.
  • As a list: two numbers in a fixed order, here (3,1)(3, 1).
  • 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:

ViewBest forWeak spot
ArrowDirections, movements and “which way is downhill?”; seeing length and angleYou can only draw up to three entries
ListComputing, and writing code; works for any number of entriesHides the geometry
PointData: “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: (3,1)(3, 1) as a point means “3 right and 1 up from the origin”.

Math

A vector with nn entries is written

x=(x1,x2,…,xn),xi∈R\mathbf{x} = (x_1, x_2, \dots, x_n), \qquad x_i \in \mathbb{R}

and we say x∈Rn\mathbf{x}\in\mathbb{R}^n (as in P1.A4: “the set of all lists of nn real numbers”). The numbers x1,…,xnx_1, \dots, x_n are the entries or components. The small number ii 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: a=b\mathbf{a}=\mathbf{b} means ai=bia_i=b_i for each ii. So (3,1)≠(1,3)(3,1)\ne(1,3), and (3,1)≠(3,1,0)(3,1)\ne(3,1,0).

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 (3,1)(3, 1) becomes the arrow for (1,3)(1, 3), a different vector. Swapping flips the arrow over the diagonal line y=xy = x. Order matters.

That is true in data too. A house with x=(120,3,15)\mathbf{x}=(120, 3, 15) 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 x\mathbf{x} as x1,x2,x3x_1, x_2, x_3. For the house, x2=3x_2 = 3 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 (4,3)(4, 3), measured from the origin.
  • A displacement is how to get from one place to another. From A=(1,2)A=(1,2) to B=(4,3)B=(4,3) you move 3 right and 1 up: the displacement is (3,1)(3, 1). (You get it by subtracting entry by entry: 4−1=34-1=3 and 3−2=13-2=1. 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:

v=(3,1)orv=(31)or, as a row,v⊤=(31)\begin{gathered} \mathbf{v} = (3, 1) \\ \text{or}\quad \mathbf{v} = \begin{pmatrix} 3 \\ 1 \end{pmatrix} \\ \text{or, as a row,}\quad \mathbf{v}^\top = \begin{pmatrix} 3 & 1 \end{pmatrix} \end{gathered}

On this site the rule is: a vector is a column by default. We often write it inline as (3,1)(3, 1) because that saves space; it is still the column. The small ⊤\top 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. (3,1)(3, 1) has dimension 2. (120,3,15)(120, 3, 15) has dimension 3. A vector with nn entries lives in Rn\mathbb{R}^n:

EntriesLives inPicture
1R1\mathbb{R}^1 (or just R\mathbb{R})the number line
2R2\mathbb{R}^2a flat plane
3R3\mathbb{R}^3the space around you
nnRn\mathbb{R}^nimpossible 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.

A vector with three entries

xyz
v = (3, 2, 2.5)
3 entries, so v lives in ℝ³
Length ≈ 4.39 (the 3-D Pythagoras rule; P1.B4). The dashed staircase walks along x, then y, then z.

A screen is flat, so this is a projection: depth is only suggested. Turn the view to see the arrow from other sides. Beyond three entries there is nothing to draw, but the list works exactly the same.

A 3-D vector as an arrow from the origin. Sliders set x, y and z. The dashed staircase goes along x, then y, then z, which is how the tip gets its three coordinates. A screen is flat, so this is a projection: turn the view to see the depth.

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 28×2828\times28 picture of a handwritten digit has 784784 numbers, so it is a point in R784\mathbb{R}^{784}.

Dimension is not length. Two different ideas share the word “size”:

  • The dimension is how many entries: (3,1)(3, 1) and (100,100)(100, 100) both have dimension 2.
  • The length of the arrow is how far its tip is from the origin. By Pythagoras, (3,1)(3, 1) has length 32+12=10≈3.16\sqrt{3^2+1^2}=\sqrt{10}\approx 3.16 and (3,4)(3, 4) has length exactly 55. 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 R784\mathbb{R}^{784}.

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 R2\mathbb{R}^2 is every possible pair of real numbers: every point of the flat plane. R784\mathbb{R}^{784} 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 dd features is one point of Rd\mathbb{R}^d. Similar items sit close together, which is what k-nearest neighbors uses (P3.B1).
  • Embedding space. Words, images or users are mapped into Rd\mathbb{R}^d so that similar meanings are close (P6.3).
  • Parameter space. A model with pp adjustable numbers has one point in Rp\mathbb{R}^p 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 0\mathbf{0}. In the plane it is (0,0)(0, 0).

  • 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 0=(0,0)\mathbf{0}=(0,0) is not the same object as the scalar 00: the vector has one zero for each entry, so the zero vector in R3\mathbb{R}^3 is (0,0,0)(0,0,0), and the zero vector in R784\mathbb{R}^{784} 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:

WhatAs a vectorExample
A data rowone number per featurehouse (120,3,15)(120, 3, 15): area, bedrooms, age; it is a point in R3\mathbb{R}^3
A grayscale imageone number per pixel, read row by row (a colour image has 3 numbers per pixel: red, green, blue)a 28×2828\times28 picture is a vector with 784784 entries (that is the size of the MNIST handwritten digits)
A word (one-hot)a 1 in the word’s slot, 0 elsewherewith a 4-word vocabulary, “cat” can be (0,1,0,0)(0, 1, 0, 0) (P6.1)
A word (embedding)a learned list of numbers, so similar words are closeclassic word embeddings (word2vec, P6.3) have a few hundred numbers per word; in GPT-2 small every token gets 768768
Model weightsone weight per featurea linear model for houses has w=(w1,w2,w3)\mathbf{w}=(w_1, w_2, w_3), the same length as x\mathbf{x}
All the parametersevery adjustable number in the model, stacked into one list θ\boldsymbol{\theta}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 3×33\times3 image: change the pixels and watch the vector.

Click a pixel to change its ink (0, 0.5 or 1)

Read row by row, the image is the vector

x = (0, 1, 0, 1, 1, 1, 0, 1, 0)

9 entries, so x is a point in ℝ⁹. The small corner numbers are NumPy positions: they start at 0.

Here the numbers are just "how much ink". A 28 × 28 image works the same way, with 784 entries.

A 3 by 3 image and its vector. Click a pixel to change its ink (0, 0.5, 1). The vector lists the pixels row by row, so a 3 by 3 image is a point in R^9. The corner number on each pixel is its NumPy position, which starts at 0.

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.

Try it: vectors in NumPy

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; ndim is the number of axes (here 1).
  • Indexing starts at 0, so v1v_1 in the math is v[0] in code, and vnv_n is v[n-1]. The index -1 means “the last entry”. A slice like house[1:3] takes positions 1 and 2 (it stops before 3).
  • np.zeros(3) is the zero vector of R3\mathbb{R}^3.
  • 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 2+3x2+3x and 1+x1+x gives 3+4x3+4x, and stretching 2+3x2+3x by 2 gives 4+6x4+6x. A polynomial like a+bxa+bx is then described by the pair (a,b)(a, b), which is a vector in R2\mathbb{R}^2 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 Rn\mathbb{R}^n 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 (3,1)(3, 1)” and “the vector (3,1)(3, 1)”. 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: Wx\mathbf{W}\mathbf{x} (P1.B5). Many ML libraries do it the other way for data: a table of NN examples with dd features is stored with one example per row, shape (N,d)(N, d). 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

  1. Which of these is a scalar?

  2. What is the dimension of v=(2,−1,5,0)\mathbf{v} = (2, -1, 5, 0)?

  3. A 28×2828\times28 image is read row by row into one vector. How many entries does it have?

  4. One arrow goes from (1,2)(1, 2) to (4,3)(4, 3). Another goes from (0,0)(0, 0) to (3,1)(3, 1). How are they related?

  5. In NumPy, v = np.array([7., 8., 9.]). What is v[1]?

Exercises

  1. Write the vector for “3 steps left and 2 steps up”.
    Show answer(−3,2)(-3, 2): left is negative in the first entry, up is positive in the second.
  2. Give the dimension of (7,7)(7, 7), of (0,1,0,0,0)(0, 1, 0, 0, 0) and of a 20×2020\times20 image read into one vector.
    Show answer2, 5 and 400 (that is 20 × 20 pixels).
  3. Find the displacement vector and its length for the arrow from A=(2,5)A=(2, 5) to B=(6,2)B=(6, 2).
    Show answerB−A=(6−2, 2−5)=(4,−3)B-A=(6-2,\ 2-5)=(4, -3). Its length is 42+(−3)2=25=5\sqrt{4^2+(-3)^2}=\sqrt{25}=5.
  4. Is (1,2,3)(1, 2, 3) a vector in R2\mathbb{R}^2, R3\mathbb{R}^3 or R4\mathbb{R}^4?
    Show answerOnly in R3\mathbb{R}^3, because it has exactly 3 entries.
  5. A house is x=(95,2,40)\mathbf{x}=(95, 2, 40) with entries (area, bedrooms, age). Which entry is the number of bedrooms, in maths notation and in NumPy?
    Show answerx2x_2 in maths (counting from 1) and x[1] in NumPy (counting from 0). Its value is 2.
  6. Read the image with rows (0.2,0.9)(0.2, 0.9) and (0.7,0.1)(0.7, 0.1) into a vector row by row, and then column by column. Why must you pick one order and keep it?
    Show answerRow by row: (0.2,0.9,0.7,0.1)(0.2, 0.9, 0.7, 0.1). Column by column: (0.2,0.7,0.9,0.1)(0.2, 0.7, 0.9, 0.1). The two lists differ, so for the same position to always mean the same pixel, every image must use the same order.
  7. In the code cell, what is the shape of np.zeros(5), and what happens if you ask for v[5] on it?
    Show answerThe shape is (5,). Positions are 0 to 4, so v[5] raises an IndexError.
  8. In the widget, what does “Swap the two entries” do to the arrow, and for which vectors does it change nothing?
    Show answerIt reflects the arrow in the diagonal line y=xy=x, turning (a,b)(a, b) into (b,a)(b, a). It changes nothing when the two entries are equal, such as (2,2)(2, 2) or the zero vector.

Key takeaways

  • A scalar is a single number. A vector is an ordered list of numbers, such as (3, 1).
  • One vector has three faces: an arrow (direction and length), a list of numbers (good for computing) and a point (where the arrow's tip lands).
  • Order matters: (3, 1) and (1, 3) are different vectors. Math counts entries from 1 (x1x_1); NumPy counts from 0 (x[0]).
  • The dimension of a vector is its number of entries. Rn\mathbb{R}^n is the space of all vectors with n entries; each possible list is exactly one point in it.
  • An arrow can slide: a vector says how far to move, not where to start. The zero vector has every entry 0 and no direction.
  • Data rows, flattened images, word embeddings and model weights are all vectors, so one set of tools covers all of them.
Found a mistake? Tell us