Introduction
In our last lesson, you learned how to train and visualize a single decision tree. We saw its great strength: interpretability. You can look at the tree and understand its decision-making process. However, we also touched on its key weakness: a single tree can be unstable and prone to overfitting, meaning it might learn the noise in your training data too well and fail to generalize to new data.
This lesson addresses that weakness head-on. Your goal is to explain the concept of ensemble methods and the advantage of using a random forest. We will explore the powerful idea that by combining the predictions of many simple models, we can create a single, highly accurate, and robust model. This principle, often called the "wisdom of crowds," is fundamental to many state-of-the-art machine learning algorithms and is a crucial concept for building powerful, real-world AI systems.
The Wisdom of Crowds: An Introduction to Ensemble Learning
The core idea behind ensemble learning is surprisingly simple: instead of relying on a single expert (one model), you consult a committee of them and aggregate their opinions. This collective decision is often better than any individual expert's. In machine learning, this means a group of "weak learners" can come together to form one "strong learner."
There are two primary strategies for creating these ensembles:
- Bagging: Building many independent models in parallel and averaging their predictions.
- Boosting: Building models sequentially, where each new model tries to fix the errors of the previous one.
To get a clear overview of these concepts, let's watch a short video.
Ensemble (Boosting, Bagging, and Stacking) in Machine Learning: Easy Explanation for Data Scientists
This video from Emma Ding provides an excellent and clear explanation of ensemble learning, covering the core idea and the distinction between bagging and boosting.
Watch the video from the beginning until 06:37. As you watch, focus on: The fundamental idea of combining weak learners into a strong learner (00:38 - 01:41). How Bagging works: creating bootstrap samples and training models in parallel (01:41 - 03:40). How Boosting works: training models sequentially to correct prior mistakes (03:40 - 05:44). The summary of key differences between bagging and boosting (05:44 - 06:37).
To summarize the key distinctions from the video:
| Feature | Bagging | Boosting |
|---|---|---|
| Model Training | Parallel (models are independent) | Sequential (each model depends on the last) |
| Main Goal | Reduce variance (improves stability, less overfitting) | Reduce bias (improves accuracy on the training data) |
| Example | Random Forest | Gradient Boosted Trees |
Since our primary problem with single decision trees is their high variance (instability), we will now focus on bagging and its most famous implementation: the Random Forest.
Deep Dive: How a Random Forest Works
A Random Forest is an ensemble of decision trees, generally trained via the bagging method. However, it adds one extra ingredient to the bagging process that makes it even more powerful.
Let's build a Random Forest step-by-step. It's essentially a two-step randomization process.
StatQuest: Random Forests Part 1 - Building, Using and Evaluating
This StatQuest video with Josh Starmer is one of the best resources for understanding Random Forests intuitively. It breaks down the process into simple, visual steps.
Watch the video from the beginning up to 05:53. This will walk you through building and using a random forest. Step 1: Bootstrap Dataset (01:21): Notice how a new dataset is created by sampling with replacement from the original. This is the 'Bootstrap' in 'Bootstrap Aggregation' (Bagging). Step 2: Random Feature Selection (02:24): This is the key addition. At each split in a tree, the algorithm only considers a random subset of features. This forces the trees to be different from one another. Making a Prediction (04:34): See how a new data point is run through all the individual trees, and the final prediction is decided by a majority vote.
Let's solidify these concepts.
-
Bootstrap Aggregating (Bagging): The forest builds hundreds of trees. Each tree is trained on a slightly different dataset, created by drawing random samples with replacement from your original training data. This means some data points might be used multiple times in a single tree's training set, while others might not be used at all.
-
Random Feature Selection: When a single decision tree decides on a split, it considers all available features. In a Random Forest, at each split point, each tree is only allowed to choose from a random subset of features. For example, if you have 20 features, each tree might only be allowed to consider a random 5 of them at each split.
This second step is crucial. It de-correlates the trees. Without it, if one feature is very predictive, most of the trees would use it for their first split, making them all very similar. By restricting the features available, we force the trees to explore different ways of splitting the data, creating a more diverse "committee" of experts.
The final architecture looks like this:

The Advantages of Using a Random Forest
Now that you know how a Random Forest works, we can clearly state its advantages over a single decision tree. This directly addresses the second part of today's learning outcome.
1. High Accuracy and Reduced Overfitting
The primary advantage is performance. By averaging the predictions of many de-correlated trees, the Random Forest smooths out the errors and biases of individual trees. A single tree might overfit by creating a very specific rule based on noise, but it's unlikely that the majority of trees in the forest will make the same mistake. This leads to a model with lower variance, which is more robust and generalizes better to new data.
To understand the statistical reason for this, let's explore how bagging reduces variance.
Why random forests outperform decision trees: 'bagging' for variance reduction
This article, 'Why random forests outperform decision trees', provides a concise statistical explanation for why bagging works and how random forests improve upon it.
Read the sections 'Variance of an ensemble of trees' and 'Random forest: de-correlated trees'. Don't worry about deriving the variance formula yourself. The key insight is in what the formula tells us: the variance of the ensemble's prediction decreases as we add more trees (n), but it is limited by the correlation (ρ) between the trees. This is precisely why the random feature selection in a Random Forest is so important—it's designed to reduce ρ.
2. Other Key Benefits
Beyond reducing variance, Random Forests offer several practical advantages.

Let's briefly touch on two of these:
-
Feature Importance: The model can automatically calculate which features are most important for making accurate predictions. It does this by measuring how much each feature contributes to reducing impurity across all the trees in the forest. This is incredibly useful for understanding your data.
-
Built-in Validation (Out-of-Bag Error): Remember that each tree is trained on a bootstrap sample? This means for each tree, about one-third of the original data was left out. This "Out-of-Bag" (OOB) data can be used as a validation set to test the performance of that tree. By aggregating these OOB predictions across the forest, we can get a reliable estimate of the model's performance without needing a separate validation set.
StatQuest: Random Forests Part 1 - Building, Using and Evaluating
Let's return to the StatQuest video to see a clear explanation of Out-of-Bag error.
Watch the segment from 05:53 to 08:13. Focus on how the data points not included in a bootstrap sample are used to evaluate the model's accuracy, giving an unbiased performance estimate.
For a final textual summary, this article recaps the process and advantages well.
Random Forest: Ensemble Learning Technique
This blog post from Keylabs.ai provides a great summary of the concepts we've covered. It's a useful reference to consolidate your understanding.
Quickly read the sections 'Random Forest: How It Works', 'Advantages of Random Forest', and the 'FAQ' at the end. This will reinforce the key components and benefits we've discussed.
Conclusion
In this lesson, you've moved from single models to the powerful concept of ensembles. By understanding how to combine multiple "weak" models, you've unlocked a technique that underpins many high-performance AI systems.
Key Takeaways:
- Ensemble learning combines multiple models to create a more powerful and robust "strong learner." The two main types are bagging and boosting.
- A Random Forest is an ensemble of decision trees that uses two key randomization techniques:
- Bootstrap Sampling (Bagging): Each tree is trained on a different random sample of the data.
- Feature Subsetting: At each split, each tree only considers a random subset of features.
- The main advantage of a Random Forest over a single decision tree is its reduced variance, leading to higher accuracy and strong resistance to overfitting.
- Random Forests also provide useful byproducts like feature importance rankings and an unbiased performance estimate through Out-of-Bag error.
Preview of the Next Lesson:
So far in this module, you've learned to build and evaluate several classification algorithms: K-Nearest Neighbors, Logistic Regression, Decision Trees, and now Random Forests. But given a new problem, which one should you choose? In our next lesson, we will put these models to the test by applying them to the same dataset and learning how to compare their performance systematically to select the best one for the job.