Introduction
In our last lesson, we established a crucial fact for machine learning: when we formulate a linear regression problem with more data points than features, the resulting matrix equation is overdetermined and almost never has an exact solution. Geometrically, this means the target vector does not lie within the column space of the feature matrix .
This might seem like a roadblock, but it's actually the starting point for one of the most powerful ideas in applied mathematics. If we can't find a vector that perfectly transforms into , our goal shifts: we must find the best possible approximate solution. This lesson is dedicated to defining what "best" means geometrically. You will discover that the optimal solution corresponds to finding the vector within the column space of that is closest to our target . This "closest vector" is found through a fundamental geometric operation: orthogonal projection.
What Makes a Solution "Best"?
Since we cannot make the equation hold true, we have to accept some error. The difference between our model's output and the actual target is the error vector, or residual, defined as . A natural way to define the "best" solution is to find the one that makes this error vector as small as possible.
"Smallest" is measured by the vector's length, or norm. Specifically, we aim to minimize the squared Euclidean norm of the error vector. This is the least squares criterion:
This objective has a direct and intuitive meaning in the context of data fitting. If our model is a line and our data points are , then the components of the error vector are the vertical distances between each data point and the regression line. Minimizing is the same as minimizing the sum of the squares of these vertical distances, .
The following article introduces this idea from the perspective of fitting a line to data points.
Visualizing the Method of Least Squares
This article from the IISER Kolkata Maths Club sets up the problem of finding a "line of best fit" by defining the error as the sum of squared deviations.
Read the introduction, from the beginning up to the equations for m and c. Pay attention to how the error quantity, the sum of squares, is constructed from the vertical deviations shown in the first diagram.
The Geometry of the Closest Point
We can now rephrase the least squares problem in purely geometric terms. The set of all possible outputs of our model, for all possible , is precisely the column space of . Our task is to find the specific vector in the column space, C(), that is closest to our target vector . This optimal vector is denoted , where are the optimal parameters.
Imagine the column space C() as a flat plane in a higher-dimensional space, and the target vector as a point floating somewhere off that plane. What is the point on the plane closest to ? Intuition correctly tells us it's the point directly "underneath" . We find it by dropping a perpendicular line from to the plane. This point, , is called the orthogonal projection of onto the column space C().
The crucial insight is that the error vector, , which connects the projection to the original vector, must be orthogonal (perpendicular) to the plane C().

The following video by the legendary Gilbert Strang of MIT masterfully connects the two pictures we've been using: the data-fitting picture (points and a best-fit line) and the vector-space picture (vectors and a column space).
16. Projection Matrices and Least Squares
Professor Strang explains how the problem of fitting a line to data points is equivalent to projecting a vector onto a subspace.
Watch the segment from 13:31 to 19:50. He first illustrates the errors E_1, E_2, E_3 on the data plot as vertical distances to the best-fit line. Then, he switches to the vector space diagram and shows how the target vector \mathbf{b} (containing the data values) is projected onto the column space to get the vector \mathbf{p} (the values on the line), making the error vector \mathbf{e} = \mathbf{b} - \mathbf{p} as short as possible.
The next reading provides another excellent, visual explanation of this same concept, solidifying the connection between the algebraic problem and the geometric solution.
Visualizing the Method of Least Squares
Continuing with the IISER Kolkata article, this part visualizes why an exact solution doesn't exist and then demonstrates the projection solution.
First, read the section "Overdetermined systems". The diagram here clearly shows the target vector \mathbf{b} lying outside the plane spanned by the columns of A. Next, read "Finding the best solution". This is the heart of the lesson. Focus on the sentence: "it is intuitively clear that the closest point A\hat{\mathbf{x}} must be the perpendicular projection of \mathbf{b} onto the plane." The accompanying diagram makes this concept explicit.
The Orthogonality Condition
We've established that to find the least squares solution, we must find the projection such that the error vector is orthogonal to the entire column space of .
What does it mean for a vector to be orthogonal to a whole subspace? It must be orthogonal to every vector within that subspace. This seems like an infinite number of conditions to check. However, we only need to ensure that is orthogonal to the vectors that span the subspace. The columns of themselves are the natural choice for a spanning set.
Therefore, the orthogonality condition simplifies to: the error vector must be orthogonal to each column of .
If the columns of are , this means:
Recalling that the dot product can be written as , we can stack these equations into a single, elegant matrix equation:
This is simply .
This single equation, , perfectly captures the geometric condition that the error vector is orthogonal to the column space of A.
The following resources confirm this derivation and its importance.
3.3. Multiple Dimensions — Machine Learning
This online book provides a concise explanation of the geometric solution, using notation common in machine learning literature.
Please read the subsection "The Geometric Solution". The diagram is very similar to the one we've seen, but using y for the target vector and X for the feature matrix. Crucially, notice how it concludes that the orthogonality of the error, X^T(y-X\theta) = 0, leads directly to the final equations.
16. Projection Matrices and Least Squares
In this final clip, Professor Strang completes his example and verifies that the calculated error vector is indeed orthogonal to the column space, just as the theory predicts.
Watch the segment from 31:58 to 34:26. He takes the dot product of the error vector E with the projection P and gets zero. More importantly, he shows that E is also perpendicular to the individual columns of matrix A, confirming the condition A^T \mathbf{e} = \mathbf{0}.
Conclusion
This lesson provided the geometric foundation for solving overdetermined systems, which are at the heart of linear regression and many other machine learning models. Instead of giving up when an exact solution doesn't exist, we redefined the goal to finding the best approximation.
Key Takeaways:
- The "best" or "least squares" solution to is the one that minimizes the squared length of the error vector, . This is equivalent to minimizing the sum of squared errors in a data-fitting context.
- Geometrically, this problem is solved by finding the vector in the column space of that is closest to the target vector .
- This closest vector is the orthogonal projection of onto the column space of .
- The defining characteristic of this projection is that the error vector, , must be orthogonal to the entire column space of .
- This orthogonality condition is captured by the compact matrix equation .
We have established the why and the what of the least squares solution. We have our fundamental geometric condition. In the next lesson, we will use this condition to do the how: we will algebraically rearrange to derive the famous normal equations, which provide a direct formula for calculating the best-fit parameters .