04.10 Lesson 10 of 16

1 h

Bagging & 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

Exercise

Compare a tree with a forest

Train a decision tree and random forest on the same training partition and compare their generalization.

  1. Reuse the same preprocessing and split for both models.
  2. Fit a decision tree and a random forest with a fixed random seed.
  3. Compare training and validation F1 or average precision.
  4. Change one forest parameter, such as min_samples_leaf, and record the effect.
  5. Explain why the forest can be more stable but less transparent than one tree.
Done when

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

2 links
  1. 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.

  2. 02
    Ensemble Methods Article

    Read the Random Forest section and compare its description with the bagging procedure.

Check your understanding

Self-test

2 questions
  1. Question 1

    What is the purpose of bootstrap sampling in bagging?

  2. Question 2

    Why does a random forest sample features at each split?

Completion check

Move on when

You can explain bagging and feature subsampling and compare a forest with one tree using validation metrics.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels