Sub topic 14
Related methods: extremely randomized trees and boosting
This chapter assumes you can train a random forest in scikit-learn, know what bagging and feature subsampling do, and understand the bias-variance trade-off from earlier chapters. Two methods are close relatives of the random forest. Extremely randomized trees add more randomness to reduce variance. Boosting builds trees one after another to reduce bias.
Extremely randomized trees
Extremely randomized trees (often called Extra-Trees) change two things compared with a random forest.
- Each tree is trained on the whole training set by default, not on a bootstrap sample.
- At each split, the tree draws one random threshold for each candidate feature. It then keeps the best of those random thresholds. A random forest searches for the best threshold on each candidate feature.
Random thresholds make each tree a worse fit on its own, so bias goes up a little. The trees also become less correlated with each other, so averaging removes more variance. Training is faster because no sorting or threshold search is needed.
In scikit-learn, ExtraTreesClassifier has the same interface as RandomForestClassifier.
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.model_selection import cross_val_score
X, y = load_breast_cancer(return_X_y=True)
models = {
"random forest": RandomForestClassifier(n_estimators=300, random_state=0, n_jobs=-1),
"extra trees": ExtraTreesClassifier(n_estimators=300, random_state=0, n_jobs=-1),
}
for name, model in models.items():
scores = cross_val_score(model, X, y, cv=5)
print(f"{name}: mean={scores.mean():.3f}, std={scores.std():.3f}")On many datasets the two score about the same. Treat the choice as a hyperparameter and compare them with cross-validation.
Because ExtraTreesClassifier uses no bootstrap by default, there are no out-of-bag samples. To get an out-of-bag score, set bootstrap=True and oob_score=True.
Boosting
Boosting also combines many trees, but it works differently from bagging.
- Bagging trains trees independently and averages them. It mainly reduces variance, so it uses deep, low-bias trees.
- Boosting trains trees in sequence. Each new tree tries to correct the errors of the ensemble built so far. It mainly reduces bias, so it uses shallow trees, often depth 3 to 8.
In gradient boosting, each new tree is fit to the errors (more precisely, the gradient of the loss) of the current ensemble. The new tree's output is scaled by a learning rate and added to the ensemble. A small learning rate needs more trees but usually generalizes better.
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import cross_val_score
X, y = load_breast_cancer(return_X_y=True)
boost = HistGradientBoostingClassifier(
learning_rate=0.1, max_depth=3, max_iter=200, random_state=0
)
scores = cross_val_score(boost, X, y, cv=5)
print(f"gradient boosting: mean={scores.mean():.3f}, std={scores.std():.3f}")Choosing between them
| Method | Main effect | Trees | Tuning effort | Overfitting risk |
|---|---|---|---|---|
| Random forest | Lowers variance | Deep, independent | Low | Low |
| Extra-Trees | Lowers variance further | Deep, independent | Low | Low |
| Gradient boosting | Lowers bias | Shallow, sequential | Higher | Higher |
A random forest is a strong default because it works well with little tuning. Boosting often reaches higher accuracy on tabular data, but it needs care with the learning rate, number of iterations, and tree depth. Too many iterations will overfit, so use early stopping on a validation set.
Exercise
A random forest overfits less than a single deep tree, yet gradient boosting with 5,000 iterations can overfit badly. Why does adding more trees behave differently in the two methods?
Show answer
In a random forest the trees are independent, so adding more trees only averages more predictions and the variance does not increase. In boosting each tree fits the remaining errors of the current ensemble, so more trees keep fitting the training data more closely, including its noise. Boosting therefore needs a limit such as early stopping or a small learning rate.
Exercise
Name two differences between a random forest and Extra-Trees in how a single split is chosen and in how the training data is sampled.
Show answer
A random forest uses a bootstrap sample and searches for the best threshold on each candidate feature. Extra-Trees uses the full training set by default and picks one random threshold per candidate feature, then keeps the best of those.