Regression Models
Docs: Linear models · Trees · Ensembles · SVR · Regression metrics · XGBoost
Overview
Use regression when the target is a number: hourly demand, a price, a sensor reading. The question is which model fits, and the answer is almost always "start linear". A linear model that explains most of the variance beats a boosted model you can't debug. The failure people hit is the opposite: reaching for gradient boosting first, then having no baseline to say whether its RMSE is any good.
Key concepts
Cheatsheet
| Model | Reach for it when | Avoid when | Scaling? | Key knobs | #1 failure mode |
|---|---|---|---|---|---|
| Linear Regression | Relationship looks straight, need interpretability | Curved residuals, correlated features | Yes | — | Fitting a line through a curve |
| Ridge (L2) | Correlated features, keep them all | You need feature selection | Yes | alpha | Untuned alpha (use RidgeCV) |
| Lasso (L1) | Many features, most are noise | Features come in correlated groups | Yes | alpha | Randomly dropping one of a correlated pair |
| ElasticNet | Feature selection + correlated groups | Simple problems (overkill) | Yes | alpha, l1_ratio | Tuning only one of the two knobs |
| Polynomial + Ridge | One clear curve (peak or trough) | Degree above 3 needed | Yes | degree, alpha | Extrapolating beyond training range |
| Decision Tree | Humans must read the rules | Accuracy is the goal | No | max_depth | Unconstrained depth → memorisation |
| Random Forest | Strong baseline, no tuning budget | Need best accuracy or extrapolation | No | n_estimators | Trusting biased feature_importances_ |
| Gradient Boosting | Best tabular accuracy | Tiny data, no time to tune | No | learning_rate, max_depth, early stopping | No early stopping → overfit |
| SVR | Small data, high-dim, non-linear | More than ~50k rows | Yes (critical) | C, epsilon, kernel | Unscaled features |
Running example: bike-rental demand
Every model is applied to one problem: predict hourly bike rentals from temperature, humidity, wind, hour of day, day of week and season, plus ~100 engineered features (lags, rolling means, weather × hour interactions). Numbers show the typical pattern, not one specific run.
| Model | Test RMSE | Notes |
|---|---|---|
| Predict-the-mean baseline | 165 | The number to beat |
| Linear Regression | 102 | Residuals show a hump at commute hours: linearity is wrong |
| Polynomial (deg 2) + Ridge | 88 | Captures the temperature curve |
| Ridge on all features | 84 | Correlated lag features handled |
| Lasso on 120 features | 86 | Keeps 18 features. Nearly as good, far simpler |
| Decision Tree (depth 4) | 95 | Worst accuracy, but the rules fit on a whiteboard |
| Random Forest (300 trees) | 61 | Big jump: the interactions are non-linear |
| XGBoost (tuned) | 55 | Best, but took 10× the tuning time of the forest |
| SVR (RBF) | 70 | Slowest to train, pickiest about inputs |
Linear gets you 60% of the way in two minutes, trees buy accuracy with opacity, and boosting wins if you pay the tuning cost.
Evaluation metrics
| Metric | In plain words | Use when |
|---|---|---|
| MAE | Average size of a miss | Outliers are noise you shouldn't be punished for; easy to explain |
| RMSE | Square root of the average squared miss: big misses count extra | Default. Same units as the target |
| R² | Share of the target's variance the model explains (1 = perfect) | Comparing fit across targets |
| MAPE | Average miss as a percentage of the true value | Stakeholders think in percent. Never when the target can be near zero |
Five predictions with misses of 10, 10, 10, 10 and 100:
| Metric | Value | Why |
|---|---|---|
| MAE | 28 | Each miss counts by its size |
| RMSE | 45.6 | The single 100 is squared, so it dominates |
If that 100 is a data-entry error, report MAE. If it's a real blown forecast that cost money, RMSE is telling the truth.
Linear vs non-linear
| Linear models | Non-linear models | |
|---|---|---|
| Examples | Linear, Ridge, Lasso, ElasticNet, SVR (linear kernel) | Decision Tree, Random Forest, Gradient Boosting, SVR (RBF) |
| Shape learned | Straight hyperplane | Arbitrary |
| Interpretability | Coefficients readable | Black box (shallow trees are the exception) |
| Data needed | Works on small data | Benefits from more data |
| Feature scaling | Required | Not for trees |
| Extrapolation | Reasonable beyond the training range | Trees predict the last value they saw |
| Signal | Move |
|---|---|
| Roughly straight relationship, someone must audit coefficients, or under a few thousand rows | Stay linear |
| Residuals show a pattern (curve, clusters) | ❌ Linear assumption is wrong. Add a curve term or go non-linear |
| Interactions matter (temperature × weekend) | Go to trees |
| Linear is accurate enough | ✅ Stop. There's no prize for a fancier model |
Choosing a model
Figure M1-1: Regression model selection. Amber is the starting point for every regression problem; the residual plot picks the branch.
Linear regression
The coefficients are the model. Fit a straight line; if it explains the variance, stop.