Linear Regression
Least squares, the normal equations, and the regression metrics.
The regression twin of W2L3. Same skeleton — model, loss, optimisation, metrics — with a continuous target.
What a regression problem needs
The deck builds the list one bullet at a time:
- Features (inputs)
- Training examples — pairs of inputs and outputs
- A model — a function approximating the relationship between $x$ and $y$
- A loss — how well the model predicts on the training examples
- Optimisation — finding the parameters where the loss is minimal

Any "explain the regression pipeline" question is answered by walking that list.
The model
$$\hat y = w_0 + w_1 x$$
or, in the multi-dimensional case,
$$\hat y(x) = w_0 + w_1x_1 + w_2x_2 + \dots + w_dx_d = w^T x \text{ with } x_0=1$$

Loss: squared error
Least squares is the default because it is differentiable, convex in $w$, and its optimum has a closed form.
$$L(h(x),y) = \big(h(x)-y\big)^2, \qquad J(w) = \frac{1}{2N}\sum_{n=1}^{N}\big(h(x_n)-y_n\big)^2$$

The $\tfrac12$ is cosmetic — it cancels the 2 that comes down from the derivative.
Optimisation, two routes
Iterative — gradient descent, exactly as in classification:
$$w \leftarrow w - \alpha \nabla_w J(w)$$

Closed form — write the problem as $Aw = b$ and solve:
$$A = \begin{bmatrix} 1 & x_{11} & \cdots & x_{1d} \\ \vdots & & & \vdots \\ 1 & x_{N1} & \cdots & x_{Nd}\end{bmatrix},\quad w = \begin{bmatrix}w_0\\ \vdots \\ w_d\end{bmatrix},\quad b=\begin{bmatrix}y_1\\ \vdots \\ y_N\end{bmatrix}$$
If $A$ is invertible the ERM solution is $w = (A^TA)^{-1}A^Tb$ — the normal equations.

$(A^TA)$ is invertible only when the columns are linearly independent. If features are collinear (or $d > N$) you need the pseudo-inverse, or — which is the connection to W6L3 — ridge regularisation, because adding $\lambda I$ makes the matrix invertible again.
Gradient descent variants — a guaranteed definitional question
| Variant | What one update uses | Cost per update |
|---|---|---|
| Batch | the error over all training samples | full pass, exact gradient |
| Stochastic (SGD) | the error over one training sample | cheapest, noisiest |
| Mini-batch | the error over a subset | the middle ground, and what every deep-learning notebook uses |
And the two words students mix up:
- Epoch — processing the entire training set once.
- Iteration — processing the current training samples once.

Regression metrics
$$MSE = \frac{1}{N}\sum_{i=1}^{N}(y_i-\hat y_i)^2 \qquad RMSE = \sqrt{MSE} \qquad MAE = \frac{1}{N}\sum_{i=1}^{N}|y_i-\hat y_i|$$
$$R^2 = 1 - \frac{RSS}{TSS}, \qquad RSS=\sum (y_i-\hat y_i)^2,\quad TSS=\sum(y_i-\bar y)^2$$

Reading of $R^2$: the fraction of the variance in $y$ explained by the model. $R^2=1$ perfect, $R^2=0$ no better than predicting the mean, negative = worse than the mean. It is unitless, which is why it is comparable across datasets while MSE is not.
"Name typical loss functions for classification and regression." Classification: 0-1 loss, cross-entropy / logistic (negative log-likelihood), hinge loss (SVM). Regression: squared error (L2 / least squares), absolute error (L1 / MAE), Huber. Say one sentence on why each: cross-entropy is the negative log-likelihood of a Bernoulli/Categorical model and is smooth where 0-1 is not; hinge is the tightest convex upper bound on 0-1 that also enforces a margin; squared error is differentiable and gives a closed form; absolute error is robust to outliers but non-differentiable at zero.











































