What Is a Vector?
A is an ordered list of numbers. That is it. Nothing mysterious.
Vectors are the universal data format of machine learning: a training example is a vector of features, a model's weights are a vector of parameters, and a prediction is a vector of scores. Every operation you will learn — dot products, matrix multiplication, gradients — is built on vectors. Comfort here pays dividends in every subsequent lesson.
Think of GPS coordinates: your location might be - latitude and longitude. That is a 2D vector. Or think of a house described by . Every feature is one component.
Order matters. and are different vectors. Same numbers, completely different meaning - like swapping latitude and longitude.
Writing Vectors
A vector with components is written (row vector) or as a vertical column (column vector):
- the i-th component of the vector
- dimension - number of components
By convention in ML, vectors are column vectors. The number of components is the dimension of the vector. The house vector above is 4-dimensional.
Geometric Picture
In 2D, the vector is an arrow starting at the origin and ending at 3 units right and 4 units up. Every vector is an arrow. The direction says "which way," and the length says "how far."
In 3D, points 1 unit in x, 2 in y, 3 in z. ML routinely works in hundreds or thousands of dimensions - you cannot visualize that - but every rule you learn for 2D and 3D extends unchanged.
Vector Addition
Add two vectors by adding corresponding components:
- first vector
- second vector
Rule: both vectors must have the same dimension. You cannot add a 3D vector to a 4D vector.
Geometrically, adding to is like walking 3 right and 4 up, then 1 more right and 2 more up. You end at . Vector addition chains arrows end-to-end.
⬆ Positive — vectors point in similar directions
The dot product measures alignment. In ML, it's how a neuron "scores" its input — high positive = strong match, near zero = no signal, negative = opposite.
Scalar Multiplication
A is a plain number. Multiplying a vector by a scalar scales every component:
- scalar - a plain real number
- vector to be scaled
Special cases:
- Multiplying by flips the direction:
- Multiplying by gives the zero vector:
- Multiplying by a fraction shrinks:
Everyday intuition: if a map direction is , then doubling it to means "go the same way, but twice as far." Scalar multiplication changes size while keeping the basic direction.
Magnitude (Norm)
The (also called norm or length) of a vector uses the Pythagorean theorem extended to multiple dimensions:
- Euclidean norm - the length of vector x
- dimension of the vector
Examples:
- Example: (classic 3-4-5 triangle)
- Example:
- Example: - only the zero vector has length zero
A has magnitude exactly 1. To normalize a vector - convert it to unit length - divide every component by its magnitude:
- unit vector - same direction as x but length 1
Check: if , then and ✓
Why Vectors Matter in ML
Almost everything in machine learning is a vector operation:
- Each training example is a vector. A 28×28 pixel image flattens to a 784-dimensional vector. A sentence encoded over a 10,000-word vocabulary is a 10,000-dimensional vector.
- Model weights are a vector. A linear model with 10 inputs has weights - a 10D vector of learned numbers, one per input.
- Predictions are vectors. A 10-class classifier outputs a 10D probability vector - one entry per class.
- The training dataset is a collection of vectors - stack them as rows and you get a matrix (next lesson).
Virtually every computation in a neural network is a combination of vector addition, scalar multiplication, and the operation we cover next: the dot product.
import numpy as np # Vectors in NumPy are just arrays house = np.array([1200, 3, 2, 15]) # [sqft, bedrooms, bathrooms, age] x = np.array([3.0, 4.0]) # Vector addition a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) # → [5, 7, 9] (component-wise) # Scalar multiplication print(2.0 * a) # → [2., 4., 6.] # Magnitude (L2 norm) mag = np.linalg.norm(x) print(mag) # → 5.0 (√(9+16)) # Unit vector (normalize) x_hat = x / np.linalg.norm(x) print(x_hat) # → [0.6, 0.8] print(np.linalg.norm(x_hat)) # → 1.0 ✓
⬆ Positive — vectors point in similar directions
The dot product measures alignment. In ML, it's how a neuron "scores" its input — high positive = strong match, near zero = no signal, negative = opposite.