TL;DR
A vector is a list of numbers, such as . You can read the list as a point on a grid, or as an arrow from the origin to that point. Vectors can be added, stretched and shrunk, and measured for length, and those operations are the basic arithmetic of machine learning.
Where you'll see it: a data point with three measurements is a 3-dimensional vector, a colour is a vector of red, green and blue values, and a neural network layer takes a vector in and returns a vector out.
Intuition
A list that also has a direction
Suppose you walk 3 blocks east and 4 blocks north. You can describe that move with two numbers, : how far you went east and how far you went north. The pair records both how far and which way, and it does not care where you started. Walking 3 east and 4 north from home or from the library gives the same instruction.
That pair of numbers is a vector. Drawn as an arrow from the origin to the point , it has a length of 5 blocks and points north-east. Drawn as a point, it is just a location on the grid. Both pictures describe the same object, and which one helps depends on the problem.
The reason to bundle numbers into one object is that most real problems have many numbers that belong together. A house has a size, a number of bedrooms and an age. A pixel has a red, a green and a blue value. A step of gradient descent moves every weight of a model at once. Writing each group as one vector lets you name the whole group with one symbol and apply one rule to every part at once.
- points at length 5
Where vectors fit in the bigger picture
The idea shows up in three roles, and the rest of this graph builds on each of them. A vector can be a list of measurements, a point in space, or an arrow with a direction and a length. Stacking several vectors side by side gives a Matrix, and a matrix is the machinery that transforms many vectors at once. Two vectors can be combined by a dot product to measure how much they point the same way. And a Gradient is itself a vector, one entry per input variable, pointing in the direction in which a function grows fastest.
flowchart LR A["Vector: an ordered list of numbers"] --> B["Matrix: a stack of vectors"] A --> C["Dot product: how much two vectors agree"] A --> D["Gradient: a vector of slopes"] B --> E["Linear maps and neural network layers"] D --> F["Gradient descent"]
The math
Definition
A vector of dimension is an ordered list of real numbers, written as a column
Here is the set of all such lists, and is the -th component, the number in position . The order matters: and are different vectors. We write vectors in bold, such as , to tell them apart from single numbers, which are called scalars. Writing the list as a row, , is a common shorthand, and the column form is what you need once matrices appear.
Adding vectors and scaling them
Two vectors of the same dimension are added component by component:
Multiplying a vector by a scalar multiplies every component by :
Geometrically, addition is "walk along one arrow, then along the other", and scaling by stretches the arrow by a factor , flipping it if . Subtraction is addition of a negative: .
Length
The length, or norm, of a vector is the square root of the sum of its squared components:
In two dimensions this is Pythagoras' theorem: the arrow is the hypotenuse of a right triangle whose legs are and . The same formula works in any dimension, even when there is no picture to draw.
A vector of length 1 is a unit vector. Any nonzero vector can be turned into one by dividing by its length, . This keeps the direction and throws away the size, which is often exactly what you want.
Distance between points
If two points are described by vectors and , the vector from one to the other is , and the distance between them is its length, . Subtracting first and then measuring is the basic move behind nearest-neighbour search and most error measures.
Linear combinations
A linear combination of vectors is any sum with scalar weights . The set of all such combinations is the span of those vectors. A matrix times a vector is a linear combination of the matrix's columns, so this operation reappears throughout linear algebra.
Worked examples
Let and .
- Addition: .
- Scaling: .
- Subtraction: .
- Check the geometry: lands at , which is where you end up after walking along and then along .
Let .
- Squared components: and .
- Sum: .
- Square root: .
- Unit vector: . Its squared components add to , so its length is 1, as it should be.
- Interpretation: the arrow for is 5 units long, and points the same way with length 1.
A drone is at and a sensor is at , both in metres.
- Vector from the drone to the sensor: .
- Length: .
- Interpretation: the sensor is 5 metres away. The third component is zero because both points are at the same height.
A pixel can be stored as an RGB vector, with each channel from 0 to 255. Take and .
- Add them: .
- Divide by 2, which is the same as multiplying by : .
- Interpretation: the midpoint colour is a brownish tone. Averaging is just a scaled sum applied to each component, so one line of vector arithmetic blends the whole pixel at once.
Tradeoffs and failure modes
Vectors are the right tool whenever a group of numbers should be handled as one object. They stop helping in a few predictable ways.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Components have different units (metres and kilograms) | Length adds metres to kilograms, and the answer depends on the unit you picked | Rescale each component first, for example by standardising |
| Adding vectors of different sizes | Component-wise addition is not defined | Pad, project or use a different representation |
| Normalising a zero vector | Division by | Check for zero, or add a small constant |
| Confusing row and column shapes | Matrix products fail or quietly give the wrong shape | Write the shape next to every symbol |
| Treating vectors as arrows only | Misses data with many dimensions, where no picture exists | Trust the algebra and the length formula |
Also, a list of numbers is only a vector if the operations make sense for it. A list of product names is not a vector, even though you can write it down.
History
Arrow-like quantities were used in mechanics for centuries, for forces and velocities, long before anyone wrote them as lists of numbers. The modern idea grew out of 19th-century work on complex numbers and geometry. In 1843 William Rowan Hamilton introduced quaternions, which are four-component numbers with a vector part, while Hermann Grassmann published a general theory of extensions in 1844 that anticipated vector spaces.
Josiah Willard Gibbs and Oliver Heaviside then reshaped these ideas into the three-dimensional vector calculus that physics textbooks still use, in the late 1800s. Giuseppe Peano gave the first axiomatic definition of a vector space in 1888. The shift to lists of numbers caught on because it turned geometry into arithmetic that a computer could carry out, and let the same tools apply to data, signals and, later, neural networks.
Check yourself
QuestionCompute when and .Show answer
Scale first: . Then add component by component: .
QuestionWhat is the length of , and what is its unit vector?Show answer
The length is . The unit vector is , which has length 1 and points in the same direction as .
QuestionWhy does the order of the components matter, and give an example where swapping them changes the meaning.Show answer
A vector is an ordered list, so position carries meaning. Swapping components changes which coordinate is which. For example, as (height in cm, weight in kg) is a person, while is a different and nonsensical input to the same model.
QuestionA model takes a vector of features measured in metres and kilograms. A student computes the distance between two data points and gets a very large number that is dominated by the weight. What went wrong, and what would you do?Show answer
The Euclidean distance adds squared differences across components regardless of units, so a feature with large numbers (kilograms here) dominates. The fix is to standardise each feature, for example by subtracting its mean and dividing by its standard deviation, so every component is on a comparable scale before measuring distance.
Sources
- Euclidean vector (Wikipedia): https://en.wikipedia.org/wiki/Euclidean_vector
- Vector space (Wikipedia): https://en.wikipedia.org/wiki/Vector_space
- Linear algebra (Wikipedia): https://en.wikipedia.org/wiki/Linear_algebra