AI State of the Art
Math/Linear Algebra/14 min
Section 318

Vector Spaces, Bases, and Projections

Listen as a podcast β†’

Introduction: structuring the data space

So far, we've manipulated vectors and matrices. But to truly understand what ML models do, you need to understand the space in which these vectors live.

A vector space is a mathematical framework that defines the rules of the game: which vectors exist, how you can combine them, which operations are allowed. It's the underlying structure that gives meaning to embeddings, projections, and the very notion of "proximity" between data points.

Vector space: intuitive definition

A vector space is a set of vectors that you can add together and multiply by scalars, and the result always stays in the set.

Formally, a vector space VV over R\mathbb{R} satisfies:

  • Closure under addition: if u,v∈V\mathbf{u}, \mathbf{v} \in V, then u+v∈V\mathbf{u} + \mathbf{v} \in V
  • Closure under scalar multiplication: if v∈V\mathbf{v} \in V and α∈R\alpha \in \mathbb{R}, then Ξ±v∈V\alpha \mathbf{v} \in V
  • Plus some natural properties (associativity, commutativity, existence of a zero vector, etc.)

The most common example: Rn\mathbb{R}^n, the set of all vectors with nn real components. That's the space your data lives in when you have nn features.

But vector spaces aren't limited to lists of numbers. The set of continuous functions, the set of polynomials of degree at most kk, the set of mΓ—nm \times n matrices, these are all vector spaces. This abstraction allows transposing the same mathematical tools to very different contexts.

Basis and dimension

Linear combination

A vector v\mathbf{v} is a linear combination of vectors v1,…,vk\mathbf{v}_1, \ldots, \mathbf{v}_k if it can be written as:

v=Ξ±1v1+Ξ±2v2+…+Ξ±kvk\mathbf{v} = \alpha_1 \mathbf{v}_1 + \alpha_2 \mathbf{v}_2 + \ldots + \alpha_k \mathbf{v}_k

for some scalars Ξ±1,…,Ξ±k\alpha_1, \ldots, \alpha_k.

Linear independence

Vectors are linearly independent if none of them can be expressed as a linear combination of the others. In other words: no vector is redundant, each one brings a new direction.

Basis

A basis of a vector space VV is a set of vectors that are:

  1. Linearly independent (no redundancy)
  2. Spanning VV (every vector in VV can be written as a linear combination of the basis)

The most well-known basis of R3\mathbb{R}^3 is the standard basis:

e1=(100),e2=(010),e3=(001)\mathbf{e}_1 = \begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix}, \quad \mathbf{e}_2 = \begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix}, \quad \mathbf{e}_3 = \begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix}

But there are infinitely many possible bases. Each basis offers a different "viewpoint" on the same data. PCA, for instance, finds the basis that maximizes variance along each axis.

Two drawings side by side. In each, two black arrows start from the same point and span a hatched plane, and a blue arrow labelled a is drawn inside that plane. The two pairs of black arrows point in different directions from one drawing to the other.
Two choices of basis for the same plane. The black arrows are the basis vectors, different on the left and on the right; the blue arrow a is the same vector in both cases. Changing basis does not change the vector, only the coordinates used to describe it, which is exactly what PCA does when it picks axes more convenient than the original ones.Source : Gregory Gundersen, Linear Independence, Basis, and the Gram-Schmidt algorithm

Dimension

The dimension of a vector space is the number of vectors in any basis. Rn\mathbb{R}^n has dimension nn. All bases of the same space have the same number of vectors, that's a fundamental theorem.

In ML, when we say an embedding is "dimension 768," it means each vector lives in R768\mathbb{R}^{768}, a space with 768 independent axes.

Subspaces

A subspace is a subset of a vector space that is itself a vector space. It must contain the zero vector and be closed under addition and scalar multiplication.

Examples:

  • In R3\mathbb{R}^3, a line through the origin is a 1-dimensional subspace
  • A plane through the origin is a 2-dimensional subspace
  • The set {0}\{0\} is a 0-dimensional subspace

In ML

  • The column space of a matrix A\mathbf{A} (the set of all vectors Ax\mathbf{A}\mathbf{x}) is a subspace. It's the set of possible outputs of the transformation.

  • The null space of A\mathbf{A} (the set of x\mathbf{x} such that Ax=0\mathbf{A}\mathbf{x} = \mathbf{0}) is a subspace. These are the inputs that the transformation crushes to zero, the lost information.

  • In PCA, you project the data onto a lower-dimensional subspace. The "principal components" form the basis of this subspace.

Dot product and orthogonality

Dot product

The dot product (or inner product) of two vectors u,v∈Rn\mathbf{u}, \mathbf{v} \in \mathbb{R}^n is:

⟨u,v⟩=uTv=βˆ‘i=1nuivi\langle \mathbf{u}, \mathbf{v} \rangle = \mathbf{u}^T \mathbf{v} = \sum_{i=1}^{n} u_i v_i

It's a single number that measures how much the two vectors "point in the same direction." Geometrically:

⟨u,v⟩=βˆ₯uβˆ₯β‹…βˆ₯vβˆ₯β‹…cos⁑(ΞΈ)\langle \mathbf{u}, \mathbf{v} \rangle = \|\mathbf{u}\| \cdot \|\mathbf{v}\| \cdot \cos(\theta)

Where ΞΈ\theta is the angle between the two vectors.

The dot product is at the heart of the attention mechanism in Transformers. The attention score between a query q\mathbf{q} and a key k\mathbf{k} is qTk\mathbf{q}^T\mathbf{k}, a dot product that measures their similarity.

Orthogonality

Two vectors are orthogonal if their dot product is zero: ⟨u,v⟩=0\langle \mathbf{u}, \mathbf{v} \rangle = 0. They are perpendicular, totally independent in terms of direction.

An orthonormal basis is a basis whose vectors are pairwise orthogonal and have unit norm. It's the "cleanest" possible basis, no correlation between axes.

Orthogonal matrices (QTQ=I\mathbf{Q}^T\mathbf{Q} = \mathbf{I}) preserve distances and angles. They are pure rotations, with no distortion.

Orthogonal projection

The idea

Projecting a vector v\mathbf{v} onto a subspace means finding the point in the subspace closest to v\mathbf{v}. It's the shadow of v\mathbf{v} on the subspace, as if the light came from perpendicular above.

A green plane labelled col X, spanned by two arrows X1 and X2 starting from the same point. A black arrow y leaves the plane upwards. A dashed vertical line drops from the tip of y to a point in the plane, with a small square marking the right angle.
Orthogonal projection, drawn. The green plane is the subspace spanned by the columns of the matrix, the vector y sticks out of it, and the marked point inside the plane is its shadow: the point of the plane closest to y. The small square shows that the dashed line joining y to its shadow is perpendicular to the plane, and it is that perpendicularity which guarantees no other point of the plane is closer. The dashed line itself is what the projection threw away.Source : Wikimedia Commons, Ordinary least squares article

Projection onto a vector

The projection of v\mathbf{v} onto the direction of u\mathbf{u} is:

proju(v)=⟨v,u⟩⟨u,u⟩u\text{proj}_{\mathbf{u}}(\mathbf{v}) = \frac{\langle \mathbf{v}, \mathbf{u} \rangle}{\langle \mathbf{u}, \mathbf{u} \rangle} \mathbf{u}

If u\mathbf{u} has unit norm, it simplifies to proju(v)=⟨v,u⟩u\text{proj}_{\mathbf{u}}(\mathbf{v}) = \langle \mathbf{v}, \mathbf{u} \rangle \mathbf{u}.

Projection onto a subspace

If you have a matrix P\mathbf{P} whose columns form an orthonormal basis of the subspace, the projection is:

proj(v)=PPTv\text{proj}(\mathbf{v}) = \mathbf{P}\mathbf{P}^T\mathbf{v}

The matrix PPT\mathbf{P}\mathbf{P}^T is a projection matrix. It's idempotent: projecting twice gives the same result ((PPT)2=PPT(\mathbf{P}\mathbf{P}^T)^2 = \mathbf{P}\mathbf{P}^T).

Attention as projection

In a Transformer, the attention mechanism can be seen as a weighted projection. Queries and keys compute attention weights (via the dot product), then these weights are used to make a linear combination of values. The result is a kind of "soft" projection of information onto directions deemed relevant by the model.

This geometric view helps understand why attention works: it selects and combines the most relevant information for each position, exactly as a projection selects the useful component of a vector.

The curse of dimensionality

The problem

In ML, you often work in very high dimensions: thousands, even millions of features. The geometric intuition built in 2D and 3D no longer works.

Here are some counterintuitive phenomena in high dimensions:

  • Volume concentrates in the corners: in dimension dd, the volume of a hypersphere inscribed in a hypercube tends toward 0 as dd increases. Almost all the volume is in the "corners" of the cube.

  • All points are far apart: the distance between two random points in a dd-dimensional hypercube concentrates around the same value. The notions of "close" and "far" lose their discriminating power.

  • Data is sparse: to uniformly sample a dd-dimensional hypercube with reasonable density, you need a number of points exponential in dd. With 100 dimensions, even a billion points is ridiculously few.

A decreasing curve titled Volume Concentration in Higher Dimensions. The x axis is the number of dimensions from 1 to 20, the y axis the hypersphere to hypercube volume ratio. The curve starts at 1 in dimension 1, drops below 0.1 around dimension 7 and is indistinguishable from zero beyond dimension 10.
The share of the cube taken up by the inscribed sphere, dimension by dimension. In dimension 2 the sphere still fills most of the square; by dimension 10 it occupies a negligible fraction. All the volume has moved into the corners, a region 3D intuition gives no warning about, yet where nearly every point sits once you work with hundreds of features.Source : Towards Data Science, The Surprising Behavior of Data in Higher Dimensions

Consequences in ML

  • Algorithms based on distance (k-NN, clustering) struggle in high dimensions because distances become uninformative.

  • Overfitting is easier when dd is large relative to the number of examples: the model has too much freedom to fit the noise.

  • Dimensionality reduction (PCA, t-SNE, UMAP, autoencoders) is often essential to bring data into a space where geometry makes sense.

  • Modern embeddings work precisely because they project very high-dimensional data (50,000-word vocabulary) into a reasonably-dimensional space (256-1024) where distances are meaningful.

Why this matters for AI

Vector spaces provide the theoretical framework in which ML data and models live:

  • Embeddings are vectors in a learned space
  • Projections are at the heart of attention and PCA
  • Subspaces explain what a model can and cannot represent
  • Dimensionality dictates the difficulty of the problem and the techniques needed

Without these concepts, you manipulate matrices without understanding their geometry. With them, you see that ML is fundamentally a science of high-dimensional geometry.

Where this is used. The dot product and the notion of direction in a space are what give word vectors their meaning.