cat /en/boosting-step-by-step/
MDN-119:22
SERIES Inside Ordito · Chapter 8

Boosting step by step

· Artificial Intelligence · 5 min read

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.

DEMO Boosting step by step · about this demo · full screen ↗

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.

COVER COMING SOON
mltp · 2026

Machine Learning. Teoria, matematica e pratica degli algoritmi

Michele di Nuzzo · in Italian

Every algorithm in two steps: first derive it, with the mathematics it needs and an example worked by hand, then write it from scratch in NumPy. Only at the end use it with scikit-learn.

[the book’s page] [shop links coming soon]

The “Inside Ordito” series