Project
Linear regression from scratch, no libraries
One notebook, NumPy only, predicting median house value from the eight California Housing features. You write the closed-form solution and the gradient-descent solution, and prove they land in the same place.
- Load and split.
fetch_california_housing(as_frame=True), an 80/20 split with a fixed random_state, then standardise every feature using the training mean and standard deviation only. - Closed form first. Solve the normal equations with
np.linalg.lstsqand record the test RMSE. That number is the target everything else has to reach. - Derive the gradient before you type it. Write it out in a markdown cell, then implement
(2/n) * X.T @ (X @ w - y)as one vectorised expression — no Python loop over rows. - Train. Batch gradient descent, learning rate 0.1, 1,000 iterations, loss recorded every step. Plot it on a log y-axis; it must decrease monotonically.
- Prove both agree. Assert your final weights are within 1e-3 of the closed-form weights, then gradient-check three random coordinates against a central difference at eps = 1e-5.
- Break it deliberately. Re-run at learning rate 1.0, and again on unstandardised features. Two sentences on what diverged in each case and why.
Scope guard: no scikit-learn models, no regularisation, no feature engineering. If you cannot say what every line of the backward pass does, it is not finished.