AI Compass
Compass

Decision trees and ensembles

From a single tree to random forests and gradient boosting: why these methods are still the first choice for tabular data.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

A decision tree is a questionnaire. "Amount above 5,000 euros? If yes: customer for more than two years? If no: review." Each question splits the cases in two, and at the end of every branch sits an answer.

A single tree is easy to read and usually too imprecise. So you build many and let them decide together.

The two ensemble ideas

MethodHow the trees come aboutWhat improves
Random forestIn parallel, each on a random sampleVariance falls, very robust
Gradient boostingIn sequence, each correcting the remainderBias falls, higher quality

In practice

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier, HistGradientBoostingClassifier

X, y = make_classification(n_samples=6000, n_features=30, n_informative=8,
                           weights=[0.9, 0.1], random_state=0)

for name, m in [
    ("Random forest", RandomForestClassifier(n_estimators=400, min_samples_leaf=5,
                                             class_weight="balanced", random_state=0)),
    ("Boosting     ", HistGradientBoostingClassifier(max_iter=400,
                                                     learning_rate=0.06,
                                                     early_stopping=True,
                                                     random_state=0)),
]:
    s = cross_val_score(m, X, y, cv=5, scoring="average_precision")
    print(name, s.mean().round(3), "±", s.std().round(3))

The settings that matter

MethodKey dials
Random forestn_estimators (more is never worse), min_samples_leaf, max_features
Boostinglearning_rate and n_estimators jointly, max_depth, min_samples_leaf

For boosting the rule is: small learning rate, many trees, early stopping. A rate of 0.05 with 2,000 trees and a stopping criterion nearly always beats 0.3 with 200.

The split criterion

Gini impurity and information gain

G = 1 − Σₖ pₖ² gain = G(parent) − (nₗ/n)·G(left) − (nᵣ/n)·G(right)

The split chosen is the one that lowers the weighted impurity of the two children most against the parent.

pₖ
share of class k in the node
G
Gini impurity, zero for a pure node
nₗ, nᵣ
case counts in the left and right child

Boosting formally

Gradient boosting

rᵢ = − ∂L(yᵢ, F(xᵢ)) / ∂F(xᵢ) h_m = argmin_h Σᵢ ( rᵢ − h(xᵢ) )² F_m = F_{m−1} + ν · h_m

Each new tree is fitted to the negative gradient of the current model and added with a small learning rate.

F_m
the model after m trees
h_m
the m-th tree
ν
the learning rate, usually 0.03 to 0.1
r
the residual, that is the negative gradient of the loss

Reading feature importance correctly

Built-in impurity-based importance systematically favours features with many possible thresholds. A continuous column therefore looks more important than a binary column of equal predictive value.

More defensible is permutation importance on a held-out part: a column is shuffled and the drop in quality measured. With strongly correlated columns the importance splits between them, so permuting correlated groups together gives the more honest answer.

For the AI Act the difference is not academic: an importance figure in the technical documentation has to be reproducible and methodologically named. See Preparing for audit.

Related courses and sources

ArticleFreeEN

scikit-learn user guide

Not a manual but a textbook with code. Every method comes with a note on when it does not fit, which textbooks rarely state so plainly.

For anyone using classical methods; each one comes with a note on when it does not fit.

scikit-learnGo to offer
BookFreeEN

The Elements of Statistical Learning

The statistical view of machine learning, the reference on bias, variance and model selection for twenty years. Demanding, free as a PDF.

For readers with a statistics background; without one the entry is hard going.

Was this page helpful?
Decision trees and ensembles