GAMs: a glass box that bends
One smooth curve per feature, added together. Every prediction can be read off its curves. See when that's enough, and what the additive assumption can't catch.
What runs in CodeSplash
- Generalized additive model Verified in Python and R
You will be able to
- Describe an additive model of penalized smooth curves
- Read effective degrees of freedom and the smoothing parameter
- Tell from a comparison when interactions are what's missing
Read first
Curves that add
ReadA linear model gives each feature a slope. A generalized additive model gives each one a curve:
Each is a P-splineP-splineA smooth curve built from many small bumps, with a penalty on wiggliness that decides how flexible it really is.: many local bumps, with a penalty on how wiggly they get. To explain a prediction, read each curve at that row's value and add up.
That's why the card rates Generalized additive model at the top for interpretability.
Read one curve
ExploreEach panel shows one feature's effect on the prediction scale, with a band. Where is it close to a straight line? Where does it bend?
View data
| Feature | Value | Effect | Lower | Upper |
|---|---|---|---|---|
| Temperature | 56 | −1.200 | −1.400 | −1 |
| Temperature | 57.370 | −0.9950 | −1.188 | −0.8020 |
| Temperature | 58.730 | −0.8160 | −1.003 | −0.6290 |
| Temperature | 60.100 | −0.6610 | −0.8410 | −0.4810 |
| Temperature | 61.470 | −0.5290 | −0.7020 | −0.3560 |
| Temperature | 62.830 | −0.4170 | −0.5840 | −0.2500 |
| Temperature | 64.200 | −0.3230 | −0.4830 | −0.1630 |
| Temperature | 65.570 | −0.2460 | −0.4000 | −0.0930 |
| Temperature | 66.930 | −0.1840 | −0.3300 | −0.0370 |
| Temperature | 68.300 | −0.1340 | −0.2740 | 0.0060 |
| Temperature | 69.670 | −0.0950 | −0.2280 | 0.0380 |
| Temperature | 71.030 | −0.0650 | −0.1920 | 0.0620 |
| Temperature | 72.400 | −0.0420 | −0.1620 | 0.0780 |
| Temperature | 73.770 | −0.0240 | −0.1370 | 0.0900 |
| Temperature | 75.130 | −0.0090 | −0.1150 | 0.0980 |
| Temperature | 76.500 | 0.0050 | −0.0950 | 0.1050 |
| Temperature | 77.870 | 0.0190 | −0.0870 | 0.1260 |
| Temperature | 79.230 | 0.0370 | −0.0770 | 0.1500 |
| Temperature | 80.600 | 0.0580 | −0.0620 | 0.1780 |
| Temperature | 81.970 | 0.0860 | −0.0400 | 0.2130 |
| Temperature | 83.330 | 0.1230 | −0.0110 | 0.2560 |
| Temperature | 84.700 | 0.1690 | 0.0290 | 0.3090 |
| Temperature | 86.070 | 0.2280 | 0.0820 | 0.3750 |
| Temperature | 87.430 | 0.3010 | 0.1480 | 0.4540 |
| Temperature | 88.800 | 0.3900 | 0.2300 | 0.5500 |
| Temperature | 90.170 | 0.4970 | 0.3300 | 0.6640 |
| Temperature | 91.530 | 0.6240 | 0.4500 | 0.7970 |
| Temperature | 92.900 | 0.7720 | 0.5920 | 0.9520 |
| Temperature | 94.270 | 0.9450 | 0.7580 | 1.131 |
| Temperature | 95.630 | 1.142 | 0.9490 | 1.336 |
| Temperature | 97 | 1.368 | 1.168 | 1.568 |
| Wind | 2 | 0.5500 | 0.3500 | 0.7500 |
| Wind | 2.630 | 0.4500 | 0.2560 | 0.6430 |
| Wind | 3.270 | 0.3600 | 0.1730 | 0.5460 |
| Wind | 3.900 | 0.2790 | 0.0990 | 0.4590 |
| Wind | 4.530 | 0.2060 | 0.0320 | 0.3790 |
| Wind | 5.170 | 0.1400 | −0.0270 | 0.3070 |
| Wind | 5.800 | 0.0810 | −0.0790 | 0.2410 |
| Wind | 6.430 | 0.0280 | −0.1260 | 0.1810 |
| Wind | 7.070 | −0.0200 | −0.1670 | 0.1260 |
| Wind | 7.700 | −0.0630 | −0.2030 | 0.0770 |
| Wind | 8.330 | −0.1020 | −0.2350 | 0.0310 |
| Wind | 8.970 | −0.1370 | −0.2640 | −0.0100 |
| Wind | 9.600 | −0.1680 | −0.2880 | −0.0480 |
| Wind | 10.230 | −0.1960 | −0.3100 | −0.0830 |
| Wind | 10.870 | −0.2220 | −0.3290 | −0.1150 |
| Wind | 11.500 | −0.2450 | −0.3450 | −0.1450 |
| Wind | 12.130 | −0.2650 | −0.3720 | −0.1590 |
| Wind | 12.770 | −0.2840 | −0.3970 | −0.1700 |
| Wind | 13.400 | −0.3000 | −0.4200 | −0.1800 |
| Wind | 14.030 | −0.3150 | −0.4420 | −0.1890 |
| Wind | 14.670 | −0.3290 | −0.4620 | −0.1960 |
| Wind | 15.300 | −0.3410 | −0.4810 | −0.2010 |
| Wind | 15.930 | −0.3520 | −0.4990 | −0.2050 |
| Wind | 16.570 | −0.3620 | −0.5150 | −0.2080 |
| Wind | 17.200 | −0.3710 | −0.5310 | −0.2110 |
| Wind | 17.830 | −0.3790 | −0.5450 | −0.2120 |
| Wind | 18.470 | −0.3860 | −0.5590 | −0.2120 |
| Wind | 19.100 | −0.3920 | −0.5720 | −0.2120 |
| Wind | 19.730 | −0.3980 | −0.5850 | −0.2110 |
First 60 of 93 rows.
Read the band before you read the ends of a curve: few rows there means the shape is mostly the penalty talking.
Bending versus interacting
PredictPredict first
Ozone RMSE: a straight-line model has 22.9, and a random forest has 19.0. A GAM lets every feature bend, but doesn't let them interact. Where will it land?
Reveal
It barely beats the line: 22.3. Curves alone don't close the gap. Ozone's big values need hot and still weather together, and that's an interaction, which an additive model can't represent. A forest finds it by itself.
How wiggly?
ReadThe penalty decides, through Smoothing. The default chooses it by generalized cross-validation.
Effective degrees of freedom count how much wiggle was spent: about 1 for a straight line, and more as a curve bends. For ozone, the fit used 10.8 in total at a penalty of 1.
Fix Penalty (λ) yourself only when you have a reason, for example to compare with another study.
When it's the right model
ReadOn Pima, with four features and no interactions: AUC 0.853, against 0.857 for logistic on seven and 0.843 for boosting on seven. When effects are additive, a GAM is as good as anything, and you can show the curves to a regulator.
Use it when you need to explain each input's effect, and can accept no interactions unless you add them yourself.
Check your understanding
CheckpointCheck yourself
Which pattern can't a plain GAM represent?
Show the answer
Answer: c
Each curve involves one feature, so effects add. A dependency between two features is an interaction, which needs a joint term.
Check yourself
A smooth has effective degrees of freedom close to 1. What does it look like?
Show the answer
Answer: a
One degree of freedom is one slope. More means more bends. A strong penalty pushes a curve towards the line.
Check yourself
A GAM matches the linear model, yet a forest beats both clearly. What's the best next thing to try?
Show the answer
Answer: b
If bending didn't help and a model with interactions did, the missing structure is probably joint. A product term may bring the glass box back into contention.
Fit one
In the appIn the Learners step, select Generalized additive model. Basis functions per curve sets the budget and Smoothing picks the penalty. It handles numbers and two classes, and in Setup the Objective can be Poisson, Gamma or Tweedie for counts and amounts.
import numpy as np
import pandas as pd
from sklearn.linear_model import RidgeCV
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import SplineTransformer
df = pd.read_csv("airquality.csv").dropna()
X, y = df[["Solar.R", "Wind", "Temp"]], df["Ozone"]
# One spline basis per column, added together, under one penalty.
gam = make_pipeline(SplineTransformer(n_knots=6), RidgeCV(alphas=np.logspace(-3, 3, 13)))
print(-cross_val_score(gam, X, y, cv=5, scoring="neg_root_mean_squared_error").mean())library(mgcv)
df <- na.omit(airquality)
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
rmse <- sapply(1:5, function(k) {
fit <- gam(Ozone ~ s(Solar.R, k = 8) + s(Wind, k = 8) + s(Temp, k = 8),
data = df[fold != k, ], method = "GCV.Cp")
sqrt(mean((df$Ozone[fold == k] - predict(fit, df[fold == k, ]))^2))
})
mean(rmse)Each library has its own basis and penalty. The app runs one fit in both engines, and that's why its predictions agree between them.
Recap
Recap- A GAM adds one penalized smooth curve per feature, so every prediction can be read.
- Effective degrees of freedom say how much each curve bends.
- If a GAM ties a line and a forest wins, look for interactions.
Concepts in this lesson