◆ AI All-in-One

Level 3 In-Depth AI · Math Foundations

Vectors

A vector is an ordered list of numbers, which you can also picture as an arrow. Everything an AI model sees, from a house to a word to a photo, is turned into vectors before any learning happens.

Level
●●●●● Beginner
Reading
11 min
To learn it well
40 minutes to read, an hour to practise
↗ Plan a path here

Read first

Nothing — start here.

This leads to

The PerceptronDot Product, Norms and SimilarityMatrices and Matrix MultiplicationPartial Derivatives and Gradients

Why it matters

A computer cannot look at a house, read a sentence or see a cat. All it can do is arithmetic on numbers. So the very first step of every AI system is to turn the thing it cares about into a list of numbers. That list is called a vector.

  • A house for sale becomes [120, 4, 30]: 120 square metres, 4 rooms, 30 years old. A price model learns from vectors like this.
  • A word becomes a list of a few hundred numbers called an embedding. Words with similar meanings get similar lists.
  • A small greyscale photo of 28 × 28 pixels becomes a list of 784 brightness values. A colour photo of 224 × 224 pixels becomes 150,528 numbers (224 × 224 × 3 colours). See Images as Data.
  • Inside a large language model such as GPT, every token is carried along as a vector of thousands of numbers while the model works out what comes next.

Once things are vectors, an AI system can compare them, combine them, and feed them into the matrix multiplications that make up a neural network. The same handful of operations from this chapter appear on every page of this guide. If "vector" feels comfortable by the end, you have the alphabet that the rest of AI is written in.

The intuition

There are two ways to think about a vector, and you will switch between them constantly.

A vector is a list of numbers in a fixed order. A shopping receipt that always lists [apples, bread, milk] is a vector: [3, 1, 2] means 3 apples, 1 loaf, 2 cartons. The order matters. [2, 1, 3] is a different shop. Each number in the list is called a component (or element, or entry). The number of components is the vector's dimension. The receipt is a 3-dimensional vector.

A vector is an arrow. Take a 2-dimensional vector such as [3,2][3, 2]. Start at the origin of a sheet of graph paper, walk 3 squares to the right and 2 squares up, and draw an arrow from where you started to where you stopped. That arrow has a length and a direction. The list tells you how to draw the arrow, and the arrow lets you see the list.

One vector, three views. On the left, v = [3, 2] drawn as an arrow from the origin: 3 across and 2 up. On the right, the same idea as a list of numbers, a house described by three features, and a word described by hundreds of numbers.

The arrow picture only works directly in 2 or 3 dimensions, because that is all we can draw. But the list picture works in any number of dimensions, and all the rules you learn with 2-D arrows carry over unchanged. A 768-dimensional word vector is an arrow in a space you cannot imagine, but you can still add it, stretch it, and measure its length using exactly the same arithmetic. This is the trick that makes linear algebra so useful for AI: build intuition in 2-D, then trust the algebra in 1,000-D.

Key idea A vector is an ordered list of numbers. You can picture it as an arrow from the origin. In AI, a vector is how anything — a house, a word, an image, a user — is described to the computer.

How it works

Notation

In this guide vectors are written as bold lowercase letters: x\mathbf{x}, v\mathbf{v}, w\mathbf{w}. A single ordinary number is called a scalar and is written in plain italic: aa, cc, η\eta. The components of a vector get a small number below them, called a subscript:

x=[x1,x2,x3]=[120,4,30]\mathbf{x} = [x_1, x_2, x_3] = [120, 4, 30]

So x1=120x_1 = 120 (the size), x2=4x_2 = 4 (the rooms) and x3=30x_3 = 30 (the age). A vector with nn components is said to live in Rn\mathbb{R}^n, read "R n", meaning "the set of all lists of nn real numbers". Our house is in R3\mathbb{R}^3, a 28 × 28 image in R784\mathbb{R}^{784}.

Mathematicians often write vectors standing up, as a column. [3,2][3, 2] written vertically is the same vector. The difference starts to matter only with Matrices and Matrix Multiplication.

Adding vectors

To add two vectors of the same dimension, add the matching components:

a+b=[a1+b1,  a2+b2,  …,  an+bn]\mathbf{a} + \mathbf{b} = [a_1 + b_1, \; a_2 + b_2, \; \dots, \; a_n + b_n]

With a=[3,1]\mathbf{a} = [3, 1] and b=[1,2]\mathbf{b} = [1, 2]:

[3,1]+[1,2]=[3+1,  1+2]=[4,3][3, 1] + [1, 2] = [3 + 1, \; 1 + 2] = [4, 3]

As arrows, addition means tip to tail: walk along a\mathbf{a}, then from where you ended up walk along b\mathbf{b}. The arrow from the start to the finish is a+b\mathbf{a} + \mathbf{b}. You can only add vectors with the same number of components. Adding a 3-D vector to a 2-D one is meaningless, and NumPy will refuse.

Scaling a vector

Multiplying a vector by a scalar multiplies every component by it:

c v=[c v1,  c v2,  …,  c vn]c\,\mathbf{v} = [c\,v_1, \; c\,v_2, \; \dots, \; c\,v_n]

With v=[2,1]\mathbf{v} = [2, 1],   2v=[4,2]\;2\mathbf{v} = [4, 2] and −0.5 v=[−1,−0.5]-0.5\,\mathbf{v} = [-1, -0.5]. As arrows, scaling stretches or shrinks the arrow without turning it. A negative number flips it to point the opposite way. This is why a scalar is called a scalar: it scales.

Subtracting vectors

Subtraction is adding the flipped vector: b−a=b+(−1) a\mathbf{b} - \mathbf{a} = \mathbf{b} + (-1)\,\mathbf{a}. Component by component:

[4,1]−[1,3]=[4−1,  1−3]=[3,−2][4, 1] - [1, 3] = [4 - 1, \; 1 - 3] = [3, -2]

The picture is worth remembering: b−a\mathbf{b} - \mathbf{a} is the arrow from the tip of a\mathbf{a} to the tip of b\mathbf{b}. It says "how do I get from a\mathbf{a} to b\mathbf{b}?" Its length is the distance between the two points. That is how a model measures how far apart two houses, two images or two words are (see Dot Product, Norms and Similarity).

Adding put the arrows tip to tail a = [3, 1] b a + b [3, 1] + [1, 2] = [4, 3] Scaling multiply by a single number v 2v −0.5v 2 × [2, 1] = [4, 2] Subtracting the arrow from one tip to the other a b b − a [4, 1] − [1, 3] = [3, −2]
Fig. 1 The three basic operations as arrows. Adding puts arrows tip to tail. Scaling stretches an arrow, and a negative number flips it. Subtracting gives the arrow from one tip to the other.

Linear combinations

Put scaling and adding together and you get a linear combination: scale several vectors, then add them up.

c1v1+c2v2+⋯+ckvkc_1 \mathbf{v}_1 + c_2 \mathbf{v}_2 + \dots + c_k \mathbf{v}_k

For example, with e1=[1,0]\mathbf{e}_1 = [1, 0] (one step right) and e2=[0,1]\mathbf{e}_2 = [0, 1] (one step up):

3 e1+2 e2=[3,0]+[0,2]=[3,2]3\,\mathbf{e}_1 + 2\,\mathbf{e}_2 = [3, 0] + [0, 2] = [3, 2]

So the vector [3,2][3, 2] literally is "3 steps right plus 2 steps up". Every 2-D vector is some combination of those two basic directions. Linear combinations are everywhere in AI. A neuron in a neural network computes a weighted sum of its inputs, which is a linear combination. Attention in a Transformer builds each output as a weighted mix of "value" vectors. And the famous word-vector arithmetic, where king − man + woman lands close to queen, is a linear combination of three embeddings.

Vectors as features

In machine learning a vector describing one example is called a feature vector, and each component is a feature. Choosing and preparing features is a large part of practical ML (see Feature Engineering). A few things to notice:

  1. The order is a contract. If component 1 means "size" for one house, it must mean "size" for every house. Mixing up the order silently produces nonsense.
  2. Units matter. In [120, 4, 30] the size is in the hundreds while the rooms are single digits. Many algorithms treat a difference of 1 in each component as equally important, so size would dominate. That is why data is usually rescaled first (see Data Cleaning and Preparation).
  3. Many examples make a table. Stack 1,000 house vectors as the rows of a table and you have a 1,000 × 3 matrix, the standard shape of a dataset.

Dimension: how big do vectors get?

Thing Typical vector size
A house in a toy price model 3 to 100 features
A small 28 × 28 greyscale digit image 784
A word in the original word2vec (2013) 300
A token inside BERT-base (2018) 768
A token inside GPT-3 (2020) 12,288
A colour photo of 224 × 224 pixels 150,528

High-dimensional spaces behave in surprising ways, for example almost all pairs of random vectors are nearly at right angles to each other. You will meet this in k-Nearest Neighbours as the "curse of dimensionality". But the arithmetic stays the same, one component at a time.

In code

NumPy stores vectors as arrays and does every operation component by component, with no loops needed.

import numpy as np

# Two houses as feature vectors: [size in m², rooms, age in years]
a = np.array([120.0, 4, 30])
b = np.array([80.0, 3, 5])

print(a.shape)          # (3,)                how many components
print(a + b)            # [200.   7.  35.]    add component by component
print(b - a)            # [-40.  -1. -25.]    how b differs from a
print(0.5 * a)          # [60.  2. 15.]       scale every component
print(a[0], a[-1])      # 120.0 30.0          first and last component (counting from 0)

# A dataset is a stack of vectors, one per row
houses = np.array([[120, 4, 30],
                   [ 80, 3,  5],
                   [200, 6, 60]], dtype=float)
print(houses.shape)          # (3, 3)   3 houses, 3 features each
print(houses.mean(axis=0))   # [133.33333333   4.33333333  31.66666667]  the "average house"

# Linear combination: 3*e1 + 2*e2
e1 = np.array([1.0, 0.0])
e2 = np.array([0.0, 1.0])
print(3 * e1 + 2 * e2)       # [3. 2.]

PyTorch tensors behave the same way, and additionally can live on a GPU and track gradients:

import torch
v = torch.tensor([3.0, 2.0])
print(2 * v + torch.tensor([1.0, 1.0]))   # tensor([7., 5.])

Common pitfalls

  • Counting from 0. Maths writes the first component as x1x_1. Python and NumPy call it x[0]. An "off by one" here is the most common beginner bug.
  • Mismatched dimensions. np.array([1, 2, 3]) + np.array([1, 2]) raises an error. That is good: it caught a bug. The dangerous case is when shapes look compatible but mean different things (see Tensors and Shapes for broadcasting surprises).
  • Python lists are not vectors. [1, 2] + [3, 4] in plain Python gives [1, 2, 3, 4] (gluing lists together), and 2 * [1, 2] gives [1, 2, 1, 2]. Convert to np.array first.
  • Features on different scales. A vector like [120000, 0.3] (salary, a ratio) lets the big number dominate distances and slow down Gradient Descent. Standardise features before training most models.
  • Integer arrays. np.array([1, 2]) / 3 is fine, but writing into an integer array truncates: x = np.array([1, 2]); x[0] = 0.7 stores 0. Use floats (dtype=float) for data.

Watch out A vector's meaning lives in its component order and units, which NumPy knows nothing about. Swap two columns of a dataset by accident and every model trained on it will still run, train and report numbers. Keep feature names next to your arrays (a Pandas and Working with Data DataFrame helps) until the very last step.

Where it leads

A bit of history

The idea of describing a point by a list of coordinates comes from René Descartes and Pierre de Fermat in the 1630s, which is why we still say "Cartesian coordinates". Arrows that can be added tip to tail were used for forces and velocities by physicists long before they had a name. In the 1840s Hermann Grassmann developed an algebra of quantities in any number of dimensions, and William Rowan Hamilton introduced the word "vector" (Latin for "carrier"). Around 1880 Josiah Willard Gibbs and Oliver Heaviside stripped these ideas down to the vector algebra that physics and engineering still use. Machine learning adopted the language wholesale: by the 1950s the The Perceptron already treated its inputs as a vector and learned a weight vector of the same size.

Try it

  1. By hand, compute [2,−1,4]+3×[1,1,0][2, -1, 4] + 3 \times [1, 1, 0]. Check it in NumPy.
  2. Draw a=[1,2]\mathbf{a} = [1, 2] and b=[3,1]\mathbf{b} = [3, 1] on graph paper. Draw a+b\mathbf{a} + \mathbf{b} both ways round (a then b, and b then a). What shape do the four arrows make?
  3. Describe your own home as a feature vector with five components. Which features would be on very different scales?

Hint for 2: addition does not care about order, so both paths end at the same point. The arrows form a parallelogram.

Check your understanding

5 quick questions. Pick an answer to see whether you were right and why. Your best score is saved on this device.

  1. A dataset stores each house as [size in m², rooms, age]. By accident, the columns for rooms and age are swapped for half the houses. What happens when you train a model on it?

  2. What is 2 × [1, −1, 3] + [0, 4, 1]?

  3. In plain Python you write 2 * [1, 2] and get [1, 2, 1, 2]. Why?

  4. A feature vector is [120000, 0.3] (salary, a ratio). Which problem does this cause, and what is the usual fix?

  5. A neuron computes 0.5 × input₁ + 2 × input₂ − 1 × input₃. In vector language, what is this?

Finished this chapter?

Learn more

Hand-picked courses, books, videos and tutorials for going deeper. See all resources →

Video

Essence of Linear Algebra

3Blue1Brown · Beginner · Free

A visual intuition for vectors, matrices, determinants and eigenvectors. You will see the ideas, not just the rules.

Course

Linear Algebra

Khan Academy · Beginner · Free

Patient video lessons with exercises on vectors, matrices and transformations.

Interactive

Immersive Linear Algebra

J. Ström, K. Åström & T. Akenine-Möller · Beginner · Free

A free linear algebra book whose figures you can drag and rotate. Excellent for seeing vectors, dot products and matrices as geometry.

Book

Mathematics for Machine Learning

Deisenroth, Faisal & Ong · 2020 · Intermediate · Free

The linear algebra, calculus, probability and optimisation that ML uses, in one free book.

Article

The Illustrated Word2vec

Jay Alammar · 2019 · Beginner · Free

A gentle, picture-heavy explanation of how words become vectors and why their dot products capture meaning.

Facts checked as of 30 November 2026. Fast-moving details (products, prices, laws) change often. · Source: content/entries/math/vectors.md