AI Hints
Home›Machine Learning Tutorial›Gradient Boosting
Machine Learning · Lesson 38 of 85

Tuning Boosting

Early stopping and the key settings.

⏱ 2 min read▶ Video at 1:23:01🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

Boosting has a lot of settings, and they can look intimidating. The good news is that only a few really matter. And one of them, the number of trees, can be chosen for you automatically, with a beautiful trick called early stopping.

Tuning Boosting
From the video: Tuning Boosting
  • Many settings, few that matter
  • Trees can be chosen for you
  • Early stopping

Early stopping

We allow up to three thousand trees, which is far more than we need. Early stopping sets aside a slice of the training data, and watches the score there after each new tree. If it fails to improve for twenty five rounds in a row, training stops. Run it. It used far fewer trees than we allowed. We saved time, and we avoided overfitting, and we never had to guess the right number.

early_stopping.pypython
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import train_test_split

X, y = fetch_california_housing(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=0)

model = HistGradientBoostingRegressor(
    max_iter=3000, learning_rate=0.05, early_stopping=True,
    validation_fraction=0.15, n_iter_no_change=25, random_state=0)
model.fit(X_tr, y_tr)

print("Trees allowed:", 3000)
print("Trees used:   ", model.n_iter_)
print("Test R2:      ", round(model.score(X_te, y_te), 3))
Output
Trees allowed: 3000
Trees used:    508
Test R2:       0.845

The settings that matter

Here are the settings that matter. The learning rate. Lower is usually better, but needs more trees. A value around point zero five is a great start. Tree size. Shallow trees, with a depth of three to eight, are usually best. Subsampling. Giving each tree a random part of the rows and features adds diversity. And regularisation, such as the minimum size of a leaf. Set the learning rate, use early stopping for the tree count, then tune the rest.

SettingGood startEffect
Learning rate0.05Lower needs more trees
Number of treesEarly stoppingStops by itself
Max depth3 to 8Bigger overfits more
Subsample0.8Adds diversity
Min leaf size20Smooths the model

Quick check

A question for you. Your boosting model has a training score of point nine nine, and a test score of point seven. What should you do first? A, add more trees. B, raise the learning rate. C, make the trees shallower, and use early stopping. D, remove your test set. The answer is C. The large gap means overfitting, so we simplify the model.

Quick check
Train is 0.99, test is 0.70. What should you try first?

Summary

Set a low learning rate, keep the trees small, and let early stopping pick the rest. Next, you will meet the two famous libraries that made boosting a superstar, and that often win data science competitions.

Key takeaways
  • Early stopping picks the tree count
  • Learning rate and depth matter most
  • A big gap means simplify
Next up: XGBoost and LightGBM — the competition winners