Least-Squares Linear Regression: Derive the Objective and Solution
Call LinearRegression().fit(X, y) and coefficients appear in milliseconds. The library never shows you the equation it solved, why that equation has a…

Key topics
Call LinearRegression().fit(X, y) and coefficients appear in milliseconds. The library never shows you the equation it solved, why that equation has a closed-form answer, or when the answer stops being trustworthy. This article reads the machine's source code: the objective, the derivative, the normal equations. By the end, those coefficients stop being magic numbers and become consequences of a specific minimization problem.
If you have already seen the intuition — a line through points, residuals as vertical gaps — you are ready. We are going to name the quantity being minimized, then solve for it exactly.
What the Solver Is Actually Minimizing
For a single data point, the residual is the difference between the observed value and the model's prediction:
A positive residual means the model predicted too low; a negative one means too high. If we summed raw residuals, a large positive miss and a large negative miss would cancel, and a line that is badly wrong in both directions could look perfect. So we square each residual before summing. The result is the residual sum of squares, also called the least-squares objective function:
Squaring does three useful things. It removes sign cancellation. It penalizes large misses far more than small ones — a residual of 4 contributes 16, while four residuals of 1 contribute only 4. And it produces a smooth, differentiable curve, which is what makes the closed-form solution possible.
The prediction itself is a weighted sum of features plus an intercept:
The weights are the unknowns. Everything else — the values and the values — is data we already have.
Note: Minimizing squared error is a choice, not a law of nature. It encodes a preference for small errors and a strong sensitivity to outliers. A single point far from the pack can drag the whole line toward it, because its squared residual dominates the sum.
Knowledge check
Check your understanding
Answer this question before you continue.
Notation: Design Matrix, Parameter Vector, Residual Vector
Writing the sum for every point gets unwieldy fast. Matrix notation compresses it into one expression and makes the derivative tractable.
Stack the data into a design matrix with one row per example and one column per feature. Add a leading column of ones to carry the intercept:
That column of ones is not decoration. It lets the intercept be treated as just another coefficient, so the algebra stays uniform across all parameters.
Collect the coefficients into a parameter vector and the targets into a target vector :
Now every prediction at once is a single matrix-vector product , and the residual vector is . The RSS sum collapses into the squared Euclidean norm of that vector:
One compact expression for the whole objective. This is the form we will differentiate.
Note: Two assumptions are already baked in here. The model is linear in the parameters — predictions are weighted sums of the features. And the features are treated as fixed, known quantities, not random variables. Both matter later.
Deriving the Normal Equations
We want the that minimizes . Expand the squared norm:
The objective is a convex quadratic in : the term is a bowl opening upward. That convexity is the reason any stationary point we find is the global minimum, not a local trap.
To find the stationary point, take the gradient with respect to . Two matrix-calculus rules do all the work:
- for a constant vector
- when is symmetric
Applying them term by term:
Set the gradient to zero:
Divide by two and rearrange:
These are the normal equations. They are a system of linear equations in , one equation per coefficient. Solving for the estimate:
The matrix is called the Gram matrix. The closed-form solution exists and is unique exactly when is invertible — which requires the columns of to be linearly independent. No feature can be a perfect linear combination of the others. If one is, the inverse does not exist and the solution is not unique.
Common mistake: Treating the closed form as the only way to minimize the objective. Gradient descent reaches the same minimum iteratively, and it becomes the practical choice when forming or inverting the Gram matrix is too expensive. The normal equations are the destination; gradient descent is one road to it.
Knowledge check
Check your understanding
Answer this question before you continue.
The Geometric Reading: Why the Residual Is Orthogonal
The algebra works, but it hides a picture worth seeing. Think of the columns of as vectors. Every linear combination of them — every possible prediction — lives in a subspace spanned by those columns. The observed target vector generally sits outside that subspace.
Least squares is then a geometry problem: find the point in the subspace closest to . The closest point is the orthogonal projection of onto the subspace, and the residual is the perpendicular from down to it.
If the residual is perpendicular to the subspace, it must be perpendicular to every column of . Write that as equations:
Expand:
The normal equations, from a different door. Same destination, no calculus required.
The geometry also tells you what the fit can and cannot do. The model can only produce predictions inside the subspace. The residual is the part of the data the model structurally cannot explain — not a failure of the solver, but a limit of the representation.
Knowledge check
Check your understanding
Answer this question before you continue.
A Small Worked Example by Hand
Let's push numbers through the formulas. Take four points with one feature:
| 1 | 2 |
| 2 | 4 |
| 3 | 5 |
| 4 | 9 |
The design matrix includes the intercept column:
Compute the pieces of the normal equations. First :
Then :
Solve . The determinant of the Gram matrix is , so:
Multiply:
The intercept is and the slope is : the fitted line is .
Now check the residuals:
| residual | |||
|---|---|---|---|
| 1 | 2 | 2 | 0 |
| 2 | 4 | 4 | 0 |
| 3 | 5 | 6 | -1 |
| 4 | 9 | 8 | 1 |
The residuals sum to zero, and they are uncorrelated with the feature — the orthogonality property, visible in the numbers. The line also passes through the mean of (2.5) and the mean of (5), which is a general property of least-squares fits with an intercept.
You can confirm the result with a few lines of scikit-learn:
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1], [2], [3], [4]])
y = np.array([2, 4, 5, 9])
model = LinearRegression().fit(X, y)
print(model.intercept_, model.coef_) # 0.0 [2.]
The code verifies the derivation. It does not replace it — you now know which equation produced those numbers.
Knowledge check
Check your understanding
Answer this question before you continue.
What the Coefficients and Residuals Are Telling You
Each coefficient is the expected change in the target for a one-unit change in that feature, holding the other features fixed. The slope of 2 above means each additional unit of adds 2 to the predicted .
The intercept deserves care. It is the prediction when every feature is zero, which may be meaningless if zero lies outside the observed range. Here it is 0, but that is a fact about this dataset, not a universal rule.
Residuals are the model's leftover error, and their pattern matters more than their size. Well-behaved residuals look structureless — no curve, no funnel, no drift. A curved pattern suggests the linear form is wrong. A fan shape that widens with the feature suggests the error variance is not constant.
The derivation leans on a few assumptions: linearity in the parameters, independent observations, and roughly constant error variance. When those fail, the residuals show it. But note the boundary: the closed-form solution is still the least-squares answer even when assumptions are violated. The assumptions govern whether that answer is a good estimate, not whether it exists.
When the Closed Form Breaks Down
Three symptoms are worth recognizing.
Perfect collinearity. If one feature is an exact linear combination of others, the Gram matrix is singular, the inverse does not exist, and the solution is not unique. The model cannot distinguish the contributions of the redundant features.
Too many features. Forming and inverting costs roughly cubic time in the number of features. When that becomes the bottleneck, iterative methods earn their place.
Outliers and heavy-tailed noise. Squaring amplifies large residuals, so a few extreme points can pull the fit hard. The objective is doing exactly what it was defined to do — which is the problem.
The standard response to unstable or non-unique solutions is regularization, which adds a penalty that keeps the coefficients bounded and the solution unique. That is the natural next topic once the unregularized solution starts to wobble.
Where to Go Next
The derivation is now yours, not the library's. Two concrete moves will lock it in. First, re-derive the two-variable case on paper with your own numbers, then check the residuals sum to zero. Second, fit a small dataset and plot the residuals against the feature — if you see structure, you have found a violated assumption before any metric told you.
Once you can read a residual plot, the next question is what to do when the solution is unstable. That is where regularization picks up.
Knowledge check
Final check
Finish the article by checking the ideas you just learned.
References
Build stronger machine learning foundations
Use structured resources to connect theory, scikit-learn workflows, and evaluation practice.


