04.10 Lesson 10 of 16
1 hBagging & Random Forests
Explain how bootstrap aggregation and random feature subsets produce a more stable ensemble of decision trees.
By the end of this lesson
You will be able to
- Explain bootstrap sampling and bagging.
- Describe feature subsampling at each split.
- Explain why aggregation can reduce variance.
- Identify the main random forest hyperparameters.
Bagging trains on bootstrap samples
Bagging means bootstrap aggregating. Each tree is trained on a sample drawn with replacement from the training data. A random forest adds feature subsampling so each split considers only a subset of candidate features. Classification combines tree votes; regression averages tree predictions. The trees are deliberately made different, and aggregation often lowers variance compared with a single deep tree.
from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(
n_estimators=300,
max_features="sqrt",
min_samples_leaf=2,
class_weight="balanced",
random_state=42,
n_jobs=-1,
) Tune diversity and capacity, not only the tree count
The number of trees controls ensemble size, but useful tuning also considers max_features, max_depth, min_samples_leaf and class weights. More trees reduce sampling noise until additional trees have diminishing returns. A forest is often robust on tabular problems, but probabilities can still require calibration and impurity-based feature importance can mislead when features differ in cardinality. Use held-out permutation importance for a performance-based view.
Apply what you learned
ExerciseCompare a tree with a forest
Train a decision tree and random forest on the same training partition and compare their generalization.
- Reuse the same preprocessing and split for both models.
- Fit a decision tree and a random forest with a fixed random seed.
- Compare training and validation F1 or average precision.
- Change one forest parameter, such as min_samples_leaf, and record the effect.
- Explain why the forest can be more stable but less transparent than one tree.
The models are compared on identical held-out rows and the report explains bootstrap sampling, feature subsampling and the variance trade-off.
Free references
Study resources
- 01 Random Forests Part 1: Building, Using and Evaluating Video
Watch this individual Random Forest Part 1 video. Focus on bootstrap samples, random feature subsets and aggregation.
- 02 Ensemble Methods Article
Read the Random Forest section and compare its description with the bagging procedure.
Check your understanding
Self-test
-
Question 1
What is the purpose of bootstrap sampling in bagging?
It gives each learner a different resampled training set so their errors are less perfectly correlated. -
Question 2
Why does a random forest sample features at each split?
Feature subsampling encourages tree diversity and prevents a small set of strong features from dominating every tree.
Completion check
Move on when
You can explain bagging and feature subsampling and compare a forest with one tree using validation metrics.