Sub topic 2
The linear model and least squares
This chapter explains what a linear regression model computes and how ordinary least squares (OLS) picks its coefficients. It assumes you know basic Python, NumPy array shapes, and the equation of a line. You will not need scikit-learn yet. The next chapters introduce its API.
The model
A linear model predicts a target as a weighted sum of features plus a constant:
ŷ = w₁x₁ + w₂x₂ + … + wₚxₚ + b
In matrix form this is ŷ = Xw + b. Here X has shape (n_samples, n_features), w is a vector of n_features weights (the coefficients), and b is the intercept. The hat on ŷ means "predicted", to separate it from the observed y.
With one feature, this is the familiar line y = wx + b. The weight w is the slope. With two features, the model is a plane. With more, it is a hyperplane.
What "linear" means
The model is linear in the coefficients, not necessarily in the raw inputs. A model with the terms x and x² is still linear, because each weight multiplies a fixed column. A later chapter uses this to fit curves.
How least squares chooses coefficients
For any candidate w and b, each training row has a residual, which is the observed value minus the prediction:
eᵢ = yᵢ − ŷᵢ
OLS picks the w and b that minimize the sum of squared residuals:
SSE = Σ (yᵢ − ŷᵢ)²
Squaring makes every error positive and punishes large errors more than small ones. It also makes the problem smooth, so a closed-form solution exists. For the design matrix with a column of ones added for the intercept, the solution is the normal equation:
w = (XᵀX)⁻¹ Xᵀy
In practice, libraries solve this with numerically stable methods such as the singular value decomposition (SVD), not by inverting XᵀX directly.
A worked check with NumPy
You can reproduce OLS without scikit-learn. np.linalg.lstsq solves the least-squares problem directly.
import numpy as np
rng = np.random.default_rng(0)
x = rng.uniform(0, 10, size=100)
y = 3.0 * x + 2.0 + rng.normal(0, 1.0, size=100)
# Add a column of ones so the intercept is estimated too.
A = np.column_stack([x, np.ones_like(x)])
(w, b), *_ = np.linalg.lstsq(A, y, rcond=None)
residuals = y - (w * x + b)
print("slope:", w, "intercept:", b)
print("sum of squared residuals:", (residuals ** 2).sum())
print("residuals sum to about zero:", residuals.sum())The slope and intercept land close to the true values 3 and 2. They do not match exactly, because the noise shifts the fit. When the model includes an intercept, the residuals sum to approximately zero.
Common errors
- Confusing the error being minimized with the metric you report. OLS minimizes squared error on the training data. Performance on new data must be measured separately.
- Forgetting the intercept. Without it, the line is forced through the origin, which biases the fit unless your data truly passes through zero.
- Assuming a perfect solution exists. If features are exactly duplicated or perfectly correlated, XᵀX cannot be inverted and the coefficients are not unique. The multicollinearity chapter covers this.
Exercise
Why does OLS square the residuals instead of just summing them?
Show answer
Positive and negative residuals would cancel in a plain sum, so a poor line could still total zero. Squaring makes all errors positive, penalizes large errors more, and gives a smooth function with a closed-form minimum.
Exercise
Is the model ŷ = w₁x + w₂x² + b a linear model? Explain.
Show answer
Yes. It is linear in the coefficients w₁, w₂ and b, because each multiplies a fixed column of features (x and x²). Only the relationship between x and ŷ is curved.