2.2 Linear regression
1. Learn
Section titled “1. Learn”- Stanford CS229 main notes (Ng, Ma)chapter 1: LMS algorithm, the normal equations, probabilistic interpretation
- Understanding Deep Learning (Prince)§2.2 Linear regression example (pp. 18–22)
- Mathematics for Machine Learning (Deisenroth, Faisal, Ong)§9.1–9.2 Problem Formulation and Parameter Estimation (pp. 291–303)
2. Bridge
Section titled “2. Bridge”Linear regression predicts , with a constant 1 prepended to every input so that is the intercept. Stack the inputs as rows of and the loss lossThe scalar we minimise; lower is better. Often an average over training examples.Introduced in 1.6 Optimization. is (CS229 notes §1.2; MML §9.2).
Geometry first. ranges over the column space of . The closest such vector to is its orthogonal projection, so the residual must be perpendicular to every column. That is the projection condition from module 1.1, and it gives the normal equations (MML §9.2.1, eq. 9.12, and §9.4). The derivation below reaches them by calculus instead, and both routes agree.
Solve, don’t invert. Computing explicitly is slower and less accurate than solving the system. Forming squares the condition number, so when columns are nearly dependent the answer loses digits. Production code uses QR or the SVD, which is what np.linalg.lstsq does (NumPy, lstsq). If two features are exact copies, is singular and there is no unique answer at all.
The same problem by gradient descent. The loss is a quadratic bowl with Hessian . Module 1.6 then says exactly when gradient descent converges: , and elongated bowls (very different eigenvalues) converge slowly (UDL §6.1). In the regression fitter, watch the contours on the right and the path that follows them. Neural networks have no closed form, so this route is the one that scales.
Why squared error? If plus Gaussian noise, then maximising the likelihood of the data is the same as minimising squared error (CS229 notes §1.3). Module 2.4 makes that recipe general.
3. Explore
Section titled “3. Explore”Fit linear regression both ways: by the normal equations, and by gradient descent with your own hand-derived gradient. Then measure the fit with R². The tests compare you against numpy.linalg.lstsq and find the exact learning rate at which your gradient descent starts to diverge.
stubs/m2_2_linear_regression.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m2_2_linear_regression.py and run mlc check 2.2. The tests are identical; import the results on the progress page.
Locally: mlc check 2.2.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.