Handbooks / Machine Learning Basics / Chapter 4
Decision Trees and Ensembles
7 pages · ~5 min✓ Reviewed
Builds on Model Evaluation. Next up: Neural Network Basics.
Decision Trees: The Mental Model
1. What a Tree Is
- Recursive split: one feature, one threshold at a time
- Leaf predicts the majority class (classification)
- Leaf predicts the mean target (regression)
- Test x <= tbest split
- Recurseeach child
- Leafmajority / mean
2. How It Grows
| Feature | Split test |
|---|---|
| Numeric | x <= t |
| Categorical | subset or one-vs-rest |
- Greedy top-down: best local gain, no look-ahead
- So the tree is not globally optimal
- No limit: splits until leaves are pure
- Result: zero training error, high variance (overfit)
3. Strengths and Limits
| Strength | Limit |
|---|---|
| No feature scaling | Axis-aligned boundaries |
| Non-linear interactions | Unstable to small data changes |
An unconstrained tree memorises the training set: 0 train error, weak test score.
Gini vs Entropy Splits
1. Impurity and gain
| Gini | Entropy | |
|---|---|---|
| Formula | 1 - sum(p_i^2) | -sum(p_i * log2 p_i) |
| Pure node | 0 | 0 |
| Balanced binary | max 0.5 | max 1 bit |
- Information gain = parent impurity - weighted average of children's impurity
- The split with the highest gain wins
2. Worked example
- Node: 5 positives, 5 negatives → Gini 0.5, entropy 1.0
- Split into (4,1) and (1,4): each child Gini 0.32
Pruning and Controlling Tree Depth
1. Pre vs post pruning
- Pre-pruning stops growth early:
max_depth,min_samples_split,min_samples_leaf,min_impurity_decrease - Post-pruning grows the full tree, then cuts weak subtrees
- sklearn post-pruning = cost-complexity via
ccp_alpha
| Pre-pruning | Post-pruning | |
|---|---|---|
| Speed | faster | costlier |
| Sees full tree | no | yes |
| Risk | stops too early | none from stopping |
2. Cost-complexity
- Minimize error + alpha * leaves
Bagging and Random Forests
1. Bagging and OOB
- Bagging: train trees on bootstrap samples, average or vote
- Cuts variance, bias stays the same
- Each bootstrap skips ~36.8% of rows (1/e)
- Those OOB rows give a free score:
oob_score=True
2. Random Forest vs Bagging
| Bagging | Random forest | |
|---|---|---|
| Features per split | all p | random subset |
| Tree correlation | high if strong features dominate | lower |
| Ensemble variance | higher | lower |
- Rule of thumb
max_features: sqrt(p) classification, p/3 regression
Boosting: AdaBoost, Gradient Boosting, XGBoost
1. Core idea
- Weak learner = barely better than chance; stump = depth-1 tree
- Trained sequentially, each fixing the previous errors
- Mainly cuts bias; bagging cuts variance
2. Three methods
| Method | Each round | Rule |
|---|---|---|
| AdaBoost | Upweights misclassified samples | alpha = 0.5 * ln((1 - err) / err) |
| Gradient boosting | Tree fits negative gradient (residuals for squared error) | F = F + learning_rate * tree |
- XGBoost objective is regularized: L1/L2 on leaf weights
gammapenalty per split- Second-order gradients
Feature Importance and Tree Models Cheat Sheet
1. Feature Importance
| Method | How | Data | Catch |
|---|---|---|---|
feature_importances_ | Sums impurity decrease per feature | Train | Biased to high-cardinality |
| Permutation | Shuffle one feature, measure score drop | Held-out | Fails with correlated features |
| SHAP | Additive attribution per prediction | Any rows | Needs an explainer |
TreeExplaineris fast for tree ensembles- Impurity importance is also biased to continuous features
explainer = shap.TreeExplainer(model) sv = explainer.shap_values(X_test)
call-site fragment: needs import shap and a fitted tree model
Check yourself
A node has 5 positives and 5 negatives (Gini 0.5). A split gives (3,2) and (2,3). What is the gain?
- Each child: 1 - (0.36 + 0.16) = 0.48
- Weighted average is also 0.48
- Gain = 0.5 - 0.48 = 0.02, a weak split
A forest keeps improving up to 500 trees, but boosting at 2000 rounds gets worse on validation. Why?
- Forest trees are averaged, so more trees only cut variance
- Boosting keeps fitting residuals, so it ends up fitting noise
- Fix: lower learning_rate, use early_stopping_rounds
You bootstrap 1000 rows for one tree. About how many rows are left out, and what are they for?
- Each row is missed with chance (1 - 1/n)^n, about 36.8%
- So roughly 368 rows are out-of-bag
- They score the tree for free: oob_score=True