iClicker Exercise 11.1
What could we randomize to build a diverse collection of useful trees? Select all that seem promising.
- Which training rows each tree sees
- Which features a split can use
- The thresholds used for splits
- The maximum depth of each tree
- The target labels assigned to training examples
Two sources of randomness
- Bootstrap samples: each tree draws its own training rows, with replacement
- Random feature subsets: each split considers a new random subset of features
Both encourage trees to make different predictions.
Building a training set for each tree
Original training rows: A B C D E F
Each ID represents a complete row: features and target.
- Draw one row: select B
- Put it back: all six rows remain available
- Repeat six times: B B A E B D
| Tree 1 |
B B A E B D |
B (3 times) |
C, F |
| Tree 2 |
F C C A F D |
C, F (twice each) |
B, E |
Each tree gets six rows; some repeat and others are absent.
Random features at each split
Image created by GPT-6.1 Sol
- Sample candidate features at each split
- Choose the best split among those candidates
- An excluded feature can be available at a later split
Why choose a new subset at each split instead of one subset per tree?
Suppose capital.gain is useful but excluded from the candidates at the first split.
- One subset per tree: when could that tree use the feature?
- A new subset per split: what changes?
How a forest predicts
- Classification in scikit-learn: average the trees’ class probabilities; choose the class with the largest average
- Regression: average the trees’ numerical predictions
Trees are trained independently: no tree corrects an earlier tree.
Random-forest hyperparameters
n_estimators |
Number of trees in the average |
max_depth |
Maximum depth of each tree |
min_samples_leaf |
Minimum samples supporting a leaf |
max_features |
Candidate features considered at each split |
Fewer candidate features encourage diversity but can weaken individual trees.
More trees versus deeper trees
- Deeper trees can fit finer details, including noise
- More trees stabilize the average from the same training procedure
- Gains from more trees often level off; computation and memory keep growing
Adding trees does not automatically fix a training–validation gap.
iClicker Exercise 11.2
Select the most accurate statement about a standard random forest.
- Each tree independently draws a bootstrap sample of the training rows
- Each tree is restricted to one randomly selected feature subset throughout training
- Choose
n_estimators using whichever value scores best on the reserved test set
- Each tree learns to correct the predictions of the previous tree
- Averaging cannot outperform the most accurate individual model
Demo
Go to the Demo
- Trace a prediction through the small forest
- Compare the tree and forest on the same validation folds
- Inspect accuracy and fit time as the forest grows
Does the gain justify the extra computation?
Random forests and boosting
| Train trees independently |
Train trees sequentially |
| Average predictions |
Add learned corrections |
| Randomness encourages different trees |
Each tree improves the current ensemble |
Both require validation; neither guarantees better generalization.
Boosting example: One starting prediction
Predict house price from floor area, using squared-error loss. Prices are in thousands of dollars; these are invented training data.
Start by predicting the mean price, 280, for every house.
| 50 |
200 |
280 |
−80 |
| 70 |
220 |
280 |
−60 |
| 90 |
260 |
280 |
−20 |
| 110 |
300 |
280 |
+20 |
| 130 |
340 |
280 |
+60 |
| 150 |
360 |
280 |
+80 |
Where should we lower predictions? Where should we raise them?
Boosting example: Learn a correction
Fit a one-split tree: input = floor area; target = residual. It splits at floor area 100 and predicts each group’s mean residual.
| Area ≤ 100 |
−80, −60, −20 |
−53.3 |
| Area > 100 |
+20, +60, +80 |
+53.3 |
With learning rate 0.5, apply half the correction:
| Area ≤ 100 |
280 − 26.7 = 253.3 |
| Area > 100 |
280 + 26.7 = 306.7 |
Repeat: compute the remaining errors and fit the next correction tree.
Controlling boosting complexity
- Rounds: how many corrections we add
- Learning rate: how much of each correction we apply
- Tree size: how complex each correction can be
Tune these together using validation data.
More rounds can overfit; smaller steps may need more rounds.
Gradient-boosted tree implementations
| scikit-learn |
Classic and histogram-based implementations |
| XGBoost |
Efficient tree building and explicit regularization |
| LightGBM |
Histogram splits and leaf-wise tree growth |
| CatBoost |
Built-in handling of categorical features |
Compare on your task; no library is a universal winner.
Demo: Compare boosting candidates
Use the same training data, validation folds, and metric.
- Compare against the single tree and random forest
- Read validation scores alongside fold-to-fold variation
- Compare fit and prediction costs
A small score difference may not justify a much slower model.
Voting, averaging, and stacking
Combining different model types
For example: logistic regression + random forest + boosted trees.
- Classification: combine labels or probabilities
- Regression: average numerical predictions
- Different model types can encourage complementary errors
Check the combination against its strongest individual model.
Hard and soft voting
Hard voting
- Combine predicted labels
- Choose the class with the most votes
Soft voting
- Average class probabilities
- Choose the class with the largest average
Can hard and soft voting disagree?
Three binary classifiers predict these positive-class probabilities:
- Which class wins the hard vote at a 0.5 threshold?
- Which class wins the soft vote?
- Is using probabilities always better?
Stacking: Learn the combination
Base predictions become features for a meta-model.
Illustrative training inputs: each model supplies \(P(\text{income} > 50K)\).
| 0 |
0.34 |
0.40 |
0.10 |
≤50K |
| 1 |
0.72 |
0.65 |
0.85 |
>50K |
| 2 |
0.48 |
0.55 |
0.30 |
≤50K |
| 3 |
0.20 |
0.65 |
0.80 |
>50K |
- Inputs: the three probability columns, using out-of-fold predictions
- Target: the original income label; index is only a row identifier
- For a new row, the meta-model predicts from the base probabilities
Evaluating the whole stack
- Evaluate the complete training procedure on held-out data
- Keep evaluation rows out of base-model and meta-model fitting
- Compare with voting and the strongest individual candidate
Stacking adds fitting cost; improvement is not guaranteed.
Demo: Does combining models help?
- Compare voting and stacking with their constituent models
- Inspect their probability calculations and validation results
- Weigh any gain against training, prediction, and maintenance costs
Keep the ensemble only when the evidence supports it.
Comparing ensemble methods
| Random forest |
Independent randomized trees |
Average probabilities or numbers |
| Gradient boosting |
Successive correction trees |
Add scaled corrections |
| Voting / averaging |
Separate base models |
Vote or average |
| Stacking |
Base models + meta-model trained on held-out predictions |
Pass base predictions to meta-model |
All have classification and regression versions.
Key takeaways
- Useful, complementary predictions make ensembles promising
- More forest trees stabilize an average; more boosting rounds add corrections
- Stacking needs held-out base predictions for meta-model training
- Choose using validation performance and practical cost