Boosting step by step
Boosting does not build one model: it builds a hundred small ones, one after another, and each corrects the errors of those that came before. On tabular data it has for years been the model to beat. Here we watch it grow on a single variable: the days since the last purchase of the loyalty card customers.
Residuals and gradient
We start from the simplest prediction: for everyone, the average churn rate. For each customer we compute the residual, what happened minus what was predicted. A small tree learns to predict the residuals, and the prediction moves by a fraction ν of its answer. The residuals are recomputed, and we repeat.
The trick is that the residuals are the gradient of the cross-entropy with its sign changed. Boosting is gradient descent, but instead of moving weights it moves predictions, and the direction of the step is drawn by a tree.
In the lab, on the left, the risk curve and the true rates every 15 days; on the right, the average residuals and the latest tree chasing them.
Three experiments
Starting from the average. Before the first tree the curve is flat. The residuals on the right are the distance to be covered: negative for those who bought recently, positive for those who have been silent for months.
One piece at a time. With ν = 0.1 it takes dozens of trees to reach the true rates. With ν = 1 the curve jumps at once, but each tree corrects too much and chases the noise.
How many trees. After a few dozen trees the residuals die out. If you go on, the trees chase the noise of these 3,000 customers. This is why in the chapter early stopping sets aside a share of customers and stops when the error on them no longer falls.
The same experiment in Python
HistGradientBoostingClassifier with only the days variable and early stopping. Smaller steps require more trees, and the result hardly changes. You need ordito.py (!wget -q https://raw.githubusercontent.com/mid079/machine-learning-teoria-pratica/main/ordito.py).
import numpy as np
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.metrics import roc_auc_score
from ordito import carica, tabella_clienti
clienti = carica("clienti")
scontrini = carica("scontrini", ["id_scontrino", "id_cliente", "data_ora", "canale", "totale"])
train, test = tabella_clienti("2024-07-01", clienti, scontrini), tabella_clienti("2025-07-01", clienti, scontrini)
X, Xt = train[["giorni_ultimo"]], test[["giorni_ultimo"]] # a single variable, as in the lab
for passo in [0.03, 0.1, 1.0]:
m = HistGradientBoostingClassifier(learning_rate=passo, max_iter=2000, early_stopping=True,
validation_fraction=0.1, n_iter_no_change=20, random_state=42)
m.fit(X, train["abbandona"])
print(passo, m.n_iter_, round(roc_auc_score(test["abbandona"], m.predict_proba(Xt)[:, 1]), 4))
In the book
This lab accompanies chapter 8, “Ensemble methods”: the decomposition of error into bias and variance, bagging and random forests, the out-of-bag error, boosting from scratch with the Newton step, HistGradientBoosting and the comparison with logistic regression, which stays ahead on returns and does not on churn.
The “Inside Ordito” series
- Chapter 1 The line of the five stores
- Chapter 2 A simulated store
- Chapter 3 Where the leak hides
- Chapter 4 The valley of the five stores
- Chapter 5 The threshold
- Chapter 6 Why was it returned?
- Chapter 7 Build a tree
- Chapter 8 Boosting step by step
- Chapter 9 The margin
- Workshop A Price is place
- Chapter 10 Shapley by hand
- Chapter 11 Rotate the cloud
- Chapter 12 Groups and dense areas
- Chapter 13 Isolating a point
- Chapter 14 Naive forecasts
- Workshop B A year in production
- Chapter 15 The ink stain
- Chapter 16 Backpropagation