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
- Builds on
- 0 concepts →
- Quiz
- 5 questions ↓
Read first
Nothing — start here.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 . 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.
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: , , . A single ordinary number is called a scalar and is written in plain italic: , , . The components of a vector get a small number below them, called a subscript:
So (the size), (the rooms) and (the age). A vector with components is said to live in , read "R n", meaning "the set of all lists of real numbers". Our house is in , a 28 × 28 image in .
Mathematicians often write vectors standing up, as a column. 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:
With and :
As arrows, addition means tip to tail: walk along , then from where you ended up walk along . The arrow from the start to the finish is . 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:
With , and . 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: . Component by component:
The picture is worth remembering: is the arrow from the tip of to the tip of . It says "how do I get from to ?" 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).
Linear combinations
Put scaling and adding together and you get a linear combination: scale several vectors, then add them up.
For example, with (one step right) and (one step up):
So the vector 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:
- 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.
- 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). - 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 . 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), and2 * [1, 2]gives[1, 2, 1, 2]. Convert tonp.arrayfirst. - 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]) / 3is fine, but writing into an integer array truncates:x = np.array([1, 2]); x[0] = 0.7stores0. 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
- Dot Product, Norms and Similarity multiplies two vectors into a single number that measures how much they agree. It underlies search, recommendation and attention.
- Matrices and Matrix Multiplication are grids of numbers that transform vectors. A neural network layer is a matrix acting on a vector.
- Tensors and Shapes extend vectors and matrices to any number of axes, such as a batch of colour images.
- Partial Derivatives and Gradients shows that the gradient, the direction of steepest uphill on a loss, is itself a vector, which is what Gradient Descent follows downhill.
- Word Embeddings and Sentence Embeddings are vectors learned so that meaning becomes geometry.
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
- By hand, compute . Check it in NumPy.
- Draw and on graph paper. Draw both ways round (a then b, and b then a). What shape do the four arrows make?
- 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.
Learn more
Hand-picked courses, books, videos and tutorials for going deeper. See all resources →
Essence of Linear Algebra
A visual intuition for vectors, matrices, determinants and eigenvectors. You will see the ideas, not just the rules.
Linear Algebra
Patient video lessons with exercises on vectors, matrices and transformations.
Immersive Linear Algebra
A free linear algebra book whose figures you can drag and rotate. Excellent for seeing vectors, dot products and matrices as geometry.
Mathematics for Machine Learning
The linear algebra, calculus, probability and optimisation that ML uses, in one free book.
The Illustrated Word2vec
A gentle, picture-heavy explanation of how words become vectors and why their dot products capture meaning.
NumPy: the absolute basics for beginners
The official, friendly introduction to arrays, shapes, indexing and broadcasting.
Facts checked as of 30 November 2026. Fast-moving details (products, prices, laws) change often. · Source: content/entries/math/vectors.md