Linear Algebra for Machine Learning

Projection

Projection onto a line or subspace, with the residual orthogonal to the target space.

Lesson
8

Goal

Understand projection as the closest-point problem for a line or subspace, and learn why the residual lands perpendicular to the space you project onto.

Projection is the geometric move that turns noisy, off-subspace data into the best available approximation.

Motivation

Suppose a point does not lie on a line, or a data vector does not lie in the model space.

What is the nearest point that does?

That question appears everywhere in machine learning. Linear regression, least squares, and PCA all ask some version of it.

Projection is the clean answer.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
uudirection vectorThe vector whose span we project onto.
vvtarget vectorThe vector being projected.
proju(v)\operatorname{proj}_u(v)projectionThe closest point to vv on the line through uu.
eeresidualThe difference vproju(v)v - \operatorname{proj}_u(v).
AAmatrixA matrix whose columns span a subspace.
col(A)\mathrm{col}(A)column spaceThe subspace reached by all vectors of the form AxAx.

Core Ideas

Projection onto a line

If uu is a direction vector, then the projection of vv onto the line spanned by uu is

proju(v)=vuuuu.\operatorname{proj}_u(v) = \frac{v \cdot u}{u \cdot u}u.

This formula says:

  1. Measure how much of vv points along uu.
  2. Scale uu by that amount.
  3. Use that scaled vector as the closest point on the line.

Why the residual is orthogonal

Let

e=vproju(v).e = v - \operatorname{proj}_u(v).

The shortest segment from a point to a line or plane hits the target at a right angle.

That is not an accident. If the residual had any component along the target space, you could move a little further in that direction and reduce the distance.

So the residual is perpendicular to the line or subspace you projected onto.

Projection onto a subspace

For a subspace spanned by several columns of a matrix AA, the same idea holds.

We want the point in col(A)\mathrm{col}(A) closest to bb.

That closest point is the projection of bb onto col(A)\mathrm{col}(A).

The residual must be orthogonal to every column of AA.

That geometric condition becomes the algebraic condition used in least squares.

The preview of the matrix formula

If the columns of AA are independent, the projection onto the column space can be written as

P=A(ATA)1AT.P = A(A^TA)^{-1}A^T.

You do not need to memorize that yet.

The important idea is that projection is the mechanism behind that formula, not the other way around.

Worked Example

Let

u=[21],v=[34].u = \begin{bmatrix} 2 \\ 1 \end{bmatrix}, \qquad v = \begin{bmatrix} 3 \\ 4 \end{bmatrix}.

Compute the projection:

vu=3(2)+4(1)=10,v \cdot u = 3(2) + 4(1) = 10, uu=22+12=5.u \cdot u = 2^2 + 1^2 = 5.

So

proju(v)=105u=2u=[42].\operatorname{proj}_u(v) = \frac{10}{5}u = 2u = \begin{bmatrix} 4 \\ 2 \end{bmatrix}.

The residual is

e=[34][42]=[12].e = \begin{bmatrix} 3 \\ 4 \end{bmatrix} - \begin{bmatrix} 4 \\ 2 \end{bmatrix} = \begin{bmatrix} -1 \\ 2 \end{bmatrix}.

Check orthogonality:

eu=(1)(2)+2(1)=0.e \cdot u = (-1)(2) + 2(1) = 0.

That zero dot product is the signature of a correct projection.

Why This Shows Up In ML

Projection is the geometry behind:

Whenever a model cannot match the data exactly, we usually ask for the closest match instead.

Projection is the mathematical word for “closest match in a subspace.”

Common Confusions

Exercises

  1. Let

    u=[12],v=[41].u = \begin{bmatrix} 1 \\ 2 \end{bmatrix}, \qquad v = \begin{bmatrix} 4 \\ 1 \end{bmatrix}.

    Compute proju(v)\operatorname{proj}_u(v).

  2. Using your answer from Exercise 1, compute the residual ee and verify that eu=0e \cdot u = 0.

  3. Explain, in your own words, why the shortest path from a point to a plane must meet the plane at a right angle.

  4. Why is minimizing squared error a better goal than demanding zero error when the data is noisy?

  5. If u=[34]u = \begin{bmatrix}3 \\ 4\end{bmatrix}, what is proju(u)\operatorname{proj}_u(u)?

Implementation Exercise

Write a TypeScript helper that projects one 2D vector onto another and returns both the projection and the residual.

An outline could look like this:

type Vector2 = [number, number];

type ProjectionResult = {
  projection: Vector2;
  residual: Vector2;
};

function dot(a: Vector2, b: Vector2): number;
function projectOntoVector(v: Vector2, u: Vector2): ProjectionResult;

The helper should:

Reflection Questions

Next Lesson

Next: Least Squares.

The bridge is immediate: least squares is projection when the target space is the column space of a matrix.