Linear Regression and Regularization

Least squares, interpreting coefficients, and ridge, lasso and elastic net penalties for more stable models.

Beginner · about 8 hours · difficulty 2/5

Learn first: Framing ML Problems and the scikit-learn API, Optimization and Gradient Descent

Linear regression predicts a number as a weighted sum of features. It is fast, interpretable and a strong baseline, and its ideas (loss, gradients, regularization) carry into every other model.

Core ideas

  • Ordinary least squares minimizes mean squared error; it has a closed-form solution and a gradient descent solution.
  • Coefficients give the change in prediction per unit feature change, holding others fixed. Scale features before comparing magnitudes.
  • Assumptions (linearity, independent errors, constant variance) matter most for inference, less for pure prediction.
  • Polynomial and spline features let a linear model fit curves.

Regularization

  • Ridge (L2) shrinks all coefficients; helps with correlated features.
  • Lasso (L1) drives some coefficients to exactly zero, acting as feature selection.
  • Elastic net mixes both.

The penalty strength (alpha) is a hyperparameter chosen by cross-validation. Always scale features first, because the penalty treats all coefficients equally.

Pitfalls

Extrapolating far outside the training range, reading coefficients causally, and ignoring residual plots that reveal a missing nonlinearity.

Good enough to move on when you can fit OLS, ridge and lasso, choose alpha with RidgeCV or LassoCV, and interpret the resulting coefficients and residuals.

Key concepts

  • Ordinary least squares
  • Interpreting coefficients
  • Residual analysis
  • Polynomial features
  • Ridge, lasso and elastic net
  • Statsmodels for inference

Mini project

Predict median house value on the scikit-learn California Housing dataset with OLS, ridge and lasso, compare cross-validated RMSE, and plot which coefficients lasso sets to zero as alpha grows.

Check yourself

Why must you standardize features before applying ridge or lasso regularization?

Free resources