Bring the mental model from Dot Product; this page will reuse it instead of restarting from zero.
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.
Concept Structure
Orthogonality, Projections, and Least-Squares Geometry
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
Orthogonality, Projections, and Least-Squares GeometryConceptual Bridge
What should feel connected as you move through this page.
Orthogonal projection turns least squares into geometry: the fitted point is closest in a subspace, and the leftover residual is perpendicular to that subspace.
The next edge should feel earned: use the demo prediction here before following Linear Regression & Least Squares.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Suppose you have a vector , but you are only allowed to explain it using directions from a subspace . You cannot usually reach exactly. The honest question is:
Which point in gets as close to as possible?
That closest point is the projection of onto . The leftover vector,
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 cannot make the miss smaller. If one direction inside 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 must live inside the column space of the design matrix . Least squares finds the point in that column space closest to the target vector . The fitted residual is not random leftover dust; it is the perpendicular part of that the chosen feature columns cannot represent.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be a subspace of spanned by columns
Any point in can be written as , where is a coordinate vector. The projection of onto solves
Writing , the residual is
At the closest point, this residual is orthogonal to every spanning direction:
Stacking those dot-product checks gives the normal equations for projection:
Equivalently,
If the columns of are independent, then is invertible and
The projected point is
The matrix
is the projection matrix onto the column space of under the usual dot product.
Two caveats matter:
- The projected point is unique, but the coefficients may not be unique when the columns of are dependent.
- 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 be a design matrix and a target vector. Linear regression predicts
Least squares minimizes
The zero-gradient condition first gives
Multiplying by , the same condition can be written in the residual direction:
or, using the residual ,
That is the same projection condition: the fitted prediction is the closest point to in the column space of , and the residual is orthogonal to every feature column.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
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
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Drag the vector or rotate the subspace line. Before revealing, predict which normal side the residual will point toward after is projected onto the line.
The reveal draws the closest point and the residual . The diagnostic should be zero, where 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.
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.
Start with the picture, metaphor, or geometric mechanism.
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.
Which visible object should carry the first intuition?
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.
Primary source for inner products, orthogonality, orthogonal projections, projection matrices, and the projection view of linear regression.
Open sourcePrimary source for projection onto a span/range and the least-squares normal equations under a full-rank simplification.
Open sourceCurriculum source for least-squares cost, matrix notation, gradients, and normal equations.
Open sourceClaim 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.
Claims without a substantive review badge still need exact source-support review.
deisenroth-2020-mml-projections, cs229-linear-algebra-review-least-squares, cs229-main-linear-regression-normal-equations
Use equation, code, and demo objects to check whether the source support is operational.
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-28Source support candidates
book 2020Mathematics for Machine LearningPrimary source for inner products, orthogonality, orthogonal projections, projection matrices, and the projection view of linear regression.
course-notes 2015CS229 Linear Algebra Review and ReferencePrimary source for projection onto a span/range and the least-squares normal equations under a full-rank simplification.
course-notes 2023CS229 Lecture Notes: Linear RegressionCurriculum source for least-squares cost, matrix notation, gradients, and normal equations.
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.
Before touching the demo, predict one visible change that should happen in Orthogonality, Projections, and Least-Squares Geometry.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
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.Open the draft below to save one note and next action in this browser.
Orthogonality, Projections, and Least-Squares Geometry
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
This draft stays locally in this browser for concept:linear-algebra/orthogonality-projections-least-squares-geometry.
- 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
- 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
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.
concept/concept-notebook/linear-algebra/orthogonality-projections-least-squares-geometry
concept:linear-algebra/orthogonality-projections-least-squares-geometry