Module 11 · Machine Learning: How Models Learn · Lesson 36 of 51
Underfitting, overfitting and linear regression
The bias-variance trade-off with the curve-fitting lab, a linear regression solved by hand with NumPy, reading coefficients, and how regularisation and validation sets keep models honest.
In this lesson · about 45 minutes
- Recognise underfitting and overfitting from train and test errors
- Fit a linear regression with least squares and interpret its coefficients
- Explain regularisation and the role of a validation set
Try it · data lab
Underfit, good fit, overfit
Fit a curve to temperature and sales. Raise the complexity and compare the error on training points with the error on new points.
Train error (RMSE)
$158
Test error (RMSE)
$146
Best degree on test
3
Verdict
underfit
Bias and variance
| Training error | Test error | Fix | |
|---|---|---|---|
| Underfit (high bias) | High | High | More features, a more flexible model, less regularisation |
| Good fit | Low | Low, close to training | Ship it (after checking for leakage) |
| Overfit (high variance) | Very low | Much higher | More data, simpler model, regularisation, fewer features |
Linear regression
The workhorse model: prediction = b₀ + b₁x₁ + b₂x₂ + … It picks the coefficients that minimise the sum of squared errors (least squares). Each coefficient reads as: "holding the other features fixed, one more unit of x changes the prediction by b". With NumPy: put a column of ones in front of X for the intercept and call np.linalg.lstsq(X, y, rcond=None).
Regularisation
Ridge adds a penalty on large coefficients; Lasso can push weak ones to exactly zero (a built-in feature selector). Both trade a little training accuracy for better behaviour on new data. The penalty strength is a hyperparameter: a setting you choose, not one the model learns.
Validation
To choose hyperparameters without touching the test set, carve a validation set out of training (or use cross-validation, Module 12). Train on train, compare settings on validation, report the final score on test, once.
Linear regression with NumPy
Exercise
Fit and judge a regression
Fit sales on temp_c and promo (plus an intercept) using the training rows only. Set coef to the three coefficients rounded to 2 decimals (as a list), and test_rmse to the RMSE on the test rows rounded to 1 decimal.
Code that runs before yours
import pandas as pd
import numpy as np
daily = pd.read_csv("kc_daily.csv")
rng = np.random.default_rng(0)
is_test = rng.random(len(daily)) < 0.2
train, test = daily[~is_test], daily[is_test]Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 1
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.