Linear Algebra

Orthogonality, Projections, and Least-Squares Geometry

Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.

status: publishedimportance: criticaldifficulty 3/5math: undergraduateread: 18mlive demo

Concept Structure

Orthogonality, Projections, and Least-Squares Geometry

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

3prerequisites
2next concepts
2related links

Learning map

Orthogonality, Projections, and Least-Squares Geometry
BeforeDot ProductNow4/4 sections readyTryManipulate one control and predict the visible change.NextLinear Regression & Least Squares

Object flow

4/4 sections readyAsk about thisResearch room
ConceptOrthogonality, Projections, and Least-Squares GeometryLinear Algebra
3 sources attachedLocal snapshot ready
concept:linear-algebra/orthogonality-projections-least-squares-geometry

Conceptual Bridge

What should feel connected as you move through this page.

Carry inDot Product

Bring the mental model from Dot Product; this page will reuse it instead of restarting from zero.

Work hereOrthogonality, Projections, and Least-Squares Geometry

Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.

Carry outLinear Regression & Least Squares

The next edge should feel earned: use the demo prediction here before following Linear Regression & Least Squares.

Test the linkManipulate one control and predict the visible change.Then continue to Linear Regression & Least Squares
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

Suppose you have a vector xx, but you are only allowed to explain it using directions from a subspace UU. You cannot usually reach xx exactly. The honest question is:

Which point in UU gets as close to xx as possible?

That closest point is the projection of xx onto UU. The leftover vector,

r=xπU(x),r = x - \pi_U(x),

is the part your subspace could not explain.

Here is the key geometric test: once you have reached the closest point, moving along any direction inside UU cannot make the miss smaller. If one direction inside UU still points toward the residual, you could slide that way and improve the fit. So at the best point, the residual must be perpendicular to every direction in the subspace.

That is the bridge to least squares. In linear regression, the model prediction XwXw must live inside the column space of the design matrix XX. Least squares finds the point in that column space closest to the target vector yy. The fitted residual yXw^y-X\hat w is not random leftover dust; it is the perpendicular part of yy that the chosen feature columns cannot represent.

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Let UU be a subspace of Rn\mathbb R^n spanned by columns

B=[b1,,bm]Rn×m.B = [b_1,\ldots,b_m] \in \mathbb R^{n\times m}.

Any point in UU can be written as BλB\lambda, where λRm\lambda\in\mathbb R^m is a coordinate vector. The projection of xRnx\in\mathbb R^n onto UU solves

πU(x)=argminzUxz2.\pi_U(x)=\arg\min_{z\in U}\|x-z\|_2.

Writing z=Bλz=B\lambda, the residual is

r=xBλ.r = x - B\lambda.

At the closest point, this residual is orthogonal to every spanning direction:

biTr=0i=1,,m.b_i^{\mathsf T}r = 0 \qquad i=1,\ldots,m.

Stacking those mm dot-product checks gives the normal equations for projection:

BT(xBλ)=0.B^{\mathsf T}(x-B\lambda)=0.

Equivalently,

BTBλ=BTx.B^{\mathsf T}B\lambda = B^{\mathsf T}x.

If the columns of BB are independent, then BTBB^{\mathsf T}B is invertible and

λ=(BTB)1BTx.\lambda = (B^{\mathsf T}B)^{-1}B^{\mathsf T}x.

The projected point is

πU(x)=B(BTB)1BTx.\pi_U(x)=B(B^{\mathsf T}B)^{-1}B^{\mathsf T}x.

The matrix

PU=B(BTB)1BTP_U = B(B^{\mathsf T}B)^{-1}B^{\mathsf T}

is the projection matrix onto the column space of BB under the usual dot product.

Two caveats matter:

  1. The projected point πU(x)\pi_U(x) is unique, but the coefficients λ\lambda may not be unique when the columns of BB are dependent.
  2. The inverse formula is a teaching formula. Numerically, explicit inverses can be fragile; solvers based on least squares, QR, SVD, pseudoinverses, or regularization are usually better.

Now let XRn×dX\in\mathbb R^{n\times d} be a design matrix and yRny\in\mathbb R^n a target vector. Linear regression predicts

y^=Xw.\hat y = Xw.

Least squares minimizes

12yXw22.\frac12\|y-Xw\|_2^2.

The zero-gradient condition first gives

XT(Xw^y)=0.X^{\mathsf T}(X\hat w-y)=0.

Multiplying by 1-1, the same condition can be written in the residual direction:

XT(yXw^)=0.X^{\mathsf T}(y-X\hat w)=0.

or, using the residual r=yXw^r=y-X\hat w,

XTr=0.X^{\mathsf T}r=0.

That is the same projection condition: the fitted prediction Xw^X\hat w is the closest point to yy in the column space of XX, and the residual is orthogonal to every feature column.

03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt
import numpy as np

# Project x onto the subspace spanned by the columns of B.
# Shapes: B is (3, 2), x is (3,), coeffs is (2,).
x = np.array([3.0, 1.0, 2.0])
B = np.array([[1.0, 0.0],
              [1.0, 1.0],
              [0.0, 1.0]])

coeffs, *_ = np.linalg.lstsq(B, x, rcond=None)
# lstsq solves the same normal equations, but avoids explicit inverse code.
p = B @ coeffs
r = x - p

print("projection:", np.round(p, 3))
print("residual:", np.round(r, 3))
print("B^T residual:", np.round(B.T @ r, 10))

# Dependent columns: same subspace, non-unique coefficients.
B_dep = np.column_stack([B[:, 0], 2 * B[:, 0]])
coeffs_dep, *_ = np.linalg.lstsq(B_dep, x, rcond=None)
p_dep = B_dep @ coeffs_dep

print("dependent projection:", np.round(p_dep, 3))
print("dependent coefficients:", np.round(coeffs_dep, 3))
print("same subspace point:", np.allclose(p_dep, (x @ B[:, 0]) / (B[:, 0] @ B[:, 0]) * B[:, 0]))

The important line is B.T @ r. At the projection, every entry is approximately zero, meaning the residual has no dot-product component along any column direction in the subspace.

04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

Drag the vector xx or rotate the subspace line. Before revealing, predict which normal side the residual will point toward after xx is projected onto the line.

The reveal draws the closest point πU(x)\pi_U(x) and the residual xπU(x)x-\pi_U(x). The diagnostic uTru^{\mathsf T}r should be zero, where uu is the line's unit direction. Switch to the dependent-columns mode to see the same projected point with non-unique coefficients: the geometry stays fixed, but the coordinate story becomes less simple.

Live Concept Demo

Explore Orthogonality, Projections, and Least-Squares Geometry

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 3/5undergraduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what Orthogonality, Projections, and Least-Squares Geometry should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.

Prediction open01 / Intuition
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change Orthogonality, Projections, and Least-Squares Geometry should make visible.

Visual Inquiry

Make the image answer a mathematical question

Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make Orthogonality, Projections, and Least-Squares Geometry easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

book · 2020Mathematics for Machine LearningDeisenroth, Faisal, and Ong

Primary source for inner products, orthogonality, orthogonal projections, projection matrices, and the projection view of linear regression.

Open source
course-notes · 2015CS229 Linear Algebra Review and ReferenceKolter and Do

Primary source for projection onto a span/range and the least-squares normal equations under a full-rank simplification.

Open source
course-notes · 2023CS229 Lecture Notes: Linear RegressionStanford CS229

Curriculum source for least-squares cost, matrix notation, gradients, and normal equations.

Open source

Claim Review

Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources3 references

deisenroth-2020-mml-projections, cs229-linear-algebra-review-least-squares, cs229-main-linear-regression-normal-equations

Witnesses4 local objects

Use equation, code, and demo objects to check whether the source support is operational.

Substantively reviewedThe orthogonal projection of x onto a finite-dimensional subspace U is the closest point p in U, and the residual r=x-p is orthogonal to every direction in U; when U is the column space of B, this gives B^T(x-B lambda)=0, and least squares applies the same condition as X^T(y-Xw)=0.Claim metadata: source checked

The page's central invariant is a direct teaching synthesis of MML projection geometry and CS229 least-squares normal equations.

Sources: Mathematics for Machine Learning, CS229 Linear Algebra Review and Reference, CS229 Lecture Notes: Linear RegressionThe inverse formula requires independent columns/full rank and is pedagogical. Rank-deficient systems can have non-unique coefficients even though the projected point is unique. Practical code should prefer solvers, QR/SVD, pseudoinverse, or regularization.A bounded review summary is present; still check caveats and exact source scope.

MML sections 3.6-3.8 support orthogonal complements, closest-point projection, residual orthogonality to each basis vector, B^T(x-B lambda)=0, and P=B(B^T B)^-1B^T under independent columns. CS229 linalg/main notes support projection onto span/range and least-squares normal equations under full-rank assumptions.

Reviewer: codex-source-excerpt-audit; reviewed 2026-06-28

Practice Loop

Try the idea before it explains itself

Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in Orthogonality, Projections, and Least-Squares Geometry.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Object research drawerClose
ConceptOrthogonality, Projections, and Least-Squares GeometryLinear Algebra

Research Room

Attach the question to an exact object

Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptLinear Algebra

Orthogonality, Projections, and Least-Squares Geometry

Anchored question

What is the smallest example that makes Orthogonality, Projections, and Least-Squares Geometry click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays locally in this browser for concept:linear-algebra/orthogonality-projections-least-squares-geometry.

No local draft saved.
Evidence to inspect
  • Source ids to inspect: deisenroth-2020-mml-projections, cs229-linear-algebra-review-least-squares, cs229-main-linear-regression-normal-equations
  • Definition, prerequisite, and contrast concept links
  • The equation or code witness that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - Orthogonality, Projections, and Least-Squares Geometry Object key: concept:linear-algebra/orthogonality-projections-least-squares-geometry Context: Linear Algebra Anchor id: concept/concept-notebook/linear-algebra/orthogonality-projections-least-squares-geometry Open question: What is the smallest example that makes Orthogonality, Projections, and Least-Squares Geometry click without losing the math? Evidence to inspect: - Source ids to inspect: deisenroth-2020-mml-projections, cs229-linear-algebra-review-least-squares, cs229-main-linear-regression-normal-equations - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

Open source object
concept/concept-notebook/linear-algebra/orthogonality-projections-least-squares-geometry concept:linear-algebra/orthogonality-projections-least-squares-geometry