Step 1 of 6
Vectors, spaces & linear combinations
What vectors really are (not just arrows), how they span spaces, and why basis and dimension matter for everything that follows.
14 min read
Vectors are the atoms of machine learning. Every data point, every embedding, every gradient is a vector - a list of numbers with both magnitude and direction. But the real insight isn't about arrows on a grid: it's about what vectors do when you combine them.
What is a vector?#
Physicists say a vector is an arrow with length and direction. Computer scientists say it's an ordered list of numbers. Mathematicians say it's anything that follows the rules of addition and scaling. All three are right - and the mathematical view is the one that matters most for ML.
A vector in ℝⁿ is an n-tuple of real numbers. A point in 2D space is (x, y); a word embedding might live in ℝ₇₆₈. The dimension is just the number of entries - and everything we build here scales to any dimension.
A vector v in ℝ³. Each component measures how far along one axis.
Addition and scaling#
Two operations define a vector space: addition (combine two vectors component-wise) and scalar multiplication (stretch or shrink a vector by a number). Every other concept - span, basis, linear transformation - is built from these two.
Vector addition: combine component by component. Scalar multiplication: stretch every component by the same factor.
The parallelogram rule gives addition its geometric meaning: place two vectors tail to tip, and the diagonal of the parallelogram they form is the sum. Scalar multiplication stretches the arrow by factor k - negative k flips it.
Linear combinations and span#
A linear combination of vectors v₁, v₂, ..., vₙ is any sum of the form a₁v₁ + a₂v₂ + ... + aₙvₙ, where the aᵢ are scalars. The set of all possible linear combinations is the span of those vectors.
Two vectors in 2D that don't point in the same direction span the entire plane - you can reach any point by scaling and adding them. Two vectors that do point the same way span only a line. This distinction - whether vectors are linearly independent or not - determines the dimensionality of your data.
Basis and dimension#
A basis is a minimal set of linearly independent vectors that spans the whole space. In ℝ², the standard basis is î = (1, 0) and ĵ = (0, 1) - but any two non-parallel vectors work just as well. The number of vectors in any basis is the dimension of the space.
Any vector in ℝ² is a unique linear combination of the basis vectors.
In ML: choosing a basis is choosing a coordinate system. PCA picks the basis that aligns with maximum variance. Autoencoders learn a compressed basis. The entire point of representation learning is to find a basis where the important structure lines up with the axes.
- PCA (Principal Component Analysis)
- A technique that finds the directions of greatest variation in your data and re-expresses it along those directions, reducing dimensionality while keeping the most information.
- Autoencoders
- Neural networks that compress input into a small representation and then reconstruct it - forcing the network to learn the most important features.
- Representation learning
- Training a model to discover useful features from raw data automatically, instead of hand-engineering them.
The dot product - measuring similarity#
The dot product of two vectors is the sum of their component-wise products. Geometrically, it measures how much two vectors point in the same direction:
The dot product equals the product of lengths times the cosine of the angle between them.
| Dot product | Angle | Meaning |
|---|---|---|
| Positive | θ < 90° | Vectors point roughly the same way |
| Zero | θ = 90° | Vectors are orthogonal (perpendicular) |
| Negative | θ > 90° | Vectors point in opposite directions |
In ML: cosine similarity is the dot product of unit vectors. Semantic search is nearest-neighbor lookup by cosine similarity. Attention scores are dot products between query and key vectors. Logits in a classification head are dot products between the hidden state and each class embedding.
- Cosine similarity
- Measures the angle between two vectors, ignoring their magnitude - 1 means identical direction, 0 means unrelated, −1 means opposite.
- Semantic search
- Finding documents or passages by meaning rather than keyword matching, using vector similarity between embeddings.
- Attention scores
- Numbers that tell a transformer how much each token should focus on every other token when building its representation.
- Logits
- Raw, unnormalized scores a model outputs before converting them into probabilities (via softmax).
Norms - measuring size#
The norm of a vector is its length. The L2 (Euclidean) norm is the one you know from geometry - the straight-line distance from the origin:
L1 norm: sum of absolute values (Manhattan distance). L2 norm: square root of sum of squares (Euclidean distance).
In ML: L2 regularization penalizes the squared norm of the weight vector, pushing weights toward zero. L1 regularization penalizes the L1 norm, which drives some weights to exactly zero (sparsity). Gradient clipping caps the norm of the gradient to prevent exploding updates.
- L2 regularization
- Adds the sum of squared weights to the loss function, discouraging any single weight from growing too large - keeps models simpler and less prone to overfitting.
- L1 regularization
- Adds the sum of absolute weights to the loss, which pushes unimportant weights all the way to zero - effectively selecting only the features that matter.
- Gradient clipping
- Rescales the gradient if its norm exceeds a threshold, preventing an unstable training step from blowing up the model's weights.
- Sparsity
- When most values in a vector or matrix are zero - sparse models are smaller, faster, and often more interpretable.
Where it shows up in ML#
Embeddings are vectors
Every token, word, or entity lives as a point in a high-dimensional vector space. Similar meanings = nearby points.
Dot products are everywhere
Attention scores, logits, cosine similarity, kernel functions - all dot products under the hood.
Span determines expressiveness
A model's hidden layer spans a subspace; wider layers span more directions and can represent more.
Norms control magnitude
Regularization, gradient clipping, layer norm, and weight decay all operate on vector norms.