Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 49

Random Forests: Enhancing Robustness through Feature Randomness

Recommended resources

Hello! Welcome to your next lesson on Tree-Based Models.

In our last lesson, we explored how bagging (bootstrap aggregating) helps reduce the high variance of a single decision tree by training many trees on different bootstrapped datasets and averaging their predictions. We concluded by noting that while bagging is a big improvement, its effectiveness is limited because the trees it produces can still be quite similar to one another, or correlated.

Today, we will build directly on that idea to learn about one of the most popular and powerful algorithms in machine learning. Your learning outcome for this lesson is to apply Random Forests and explain how feature randomness improves model robustness. We will discover the clever trick Random Forests use to de-correlate the trees, leading to a model that is even more accurate and stable than a standard bagged ensemble.

1. From Bagging to Random Forests

A Random Forest is an extension of the bagging method we just learned. It uses the same core principles of bootstrapping and aggregation, but with one crucial addition: feature randomness.

Let's see a step-by-step guide to building a Random Forest. This process introduces a second layer of randomness on top of the bootstrapping we saw in the last lesson.

StatQuest: Random Forests Part 1 - Building, Using and Evaluating

The StatQuest video 'Random Forests Part 1' provides an excellent, clear walkthrough of how a Random Forest is constructed and used.

Watch from 00:52 to 05:53. As you watch, focus on the two distinct random steps: Creating a bootstrap dataset (same as bagging). Using a random subset of features at each split when building a tree (this is the new part!).

To recap the video's key steps:

  1. Bootstrap the Data: Just like in bagging, create a new dataset by sampling rows with replacement from the original training data.
  2. Build a Decision Tree with a Twist:
    • Start building a normal decision tree.
    • However, at each node, before you look for the best split, randomly select a subset of the available features.
    • Find the best split only among that random subset of features.
    • Continue this process down the tree.
  3. Repeat: Repeat steps 1 and 2 many times (hundreds is common) to create a "forest" of diverse trees.
  4. Aggregate Predictions: To make a prediction for a new data point, let all the trees in the forest vote (for classification) or average their predictions (for regression).

This second layer of randomness is the defining characteristic of a Random Forest.


2. Why Feature Randomness Creates Robust Models

In the previous lesson, we noted that bagged trees can be correlated. If one feature is a very strong predictor, most of the bootstrapped trees will probably use that feature for their first split. This makes the trees structurally similar and limits the amount of variance reduction we get from averaging them.

Random Forests directly combat this problem. By forcing each split to consider only a random subset of features, it prevents individual trees from all latching onto the same dominant predictor.

Comparison of Feature Selection in Bagged Trees vs. Random Forests
This image illustrates the key difference in how splits are selected. For Bagged Trees (top), all features are considered at each split. For Random Forests (bottom), only a random subset of features is considered, forcing the trees to be more diverse.

To understand why this diversity is so powerful, let's explore the concept of tree correlation.

Why random forests outperform decision trees: 'bagging' for variance reduction

The article 'Why random forests outperform decision trees' from Ameer Saleem explains the statistical intuition behind why de-correlating trees is so important.

Read the section 'Random forest: de-correlated trees'. You don't need to worry about the mathematical formula, but focus on the core concept: the variance of the ensemble depends on the correlation between the trees. By introducing feature randomness, we de-correlate the trees, which reduces the overall variance of the model.

The main takeaway is that by making the trees less correlated, we get a greater reduction in variance when we average their predictions.

Some trees will be built without seeing the strongest predictor at all, forcing them to discover other, more subtle patterns in the data. While these individual trees might be weaker, their errors are different from the errors of the other trees. When we average them all together, these diverse errors cancel each other out more effectively, leading to a more robust and accurate final model.

This video provides another clear explanation of this exact point.

Random Forest Algorithm Clearly Explained!

The 'Random Forest Algorithm Clearly Explained!' video by Normalized Nerd reinforces why both bootstrapping and feature selection are crucial.

Watch the segment from 05:54 to 07:01. The narrator does a great job of explaining how random feature selection helps reduce the correlation between trees and why this is beneficial.

In summary, the robustness of a Random Forest comes from this two-pronged approach to creating diversity:

  • Bagging ensures trees are trained on different data samples.
  • Feature randomness ensures trees are built using different sets of predictors.

The result is a model that is much less sensitive to the specific noise in the training data, as seen by the smoother decision boundary and higher accuracy in the image below.

Decision Tree vs. Random Forest: Variance and Accuracy Comparison
A single Decision Tree (left) can have a complex, jagged boundary (high variance). A Random Forest (right), by averaging many de-correlated trees, creates a smoother, more generalized boundary that performs better on new data.
Test your understanding!

Imagine you have a dataset with 10 features for predicting customer churn. One feature, last_month_activity, is extremely predictive, while the others are only moderately useful.

What problem would you likely encounter if you used a standard bagged decision tree ensemble? How does a Random Forest specifically address this issue?

Show answer

With a bagged decision tree ensemble, most of the trees would likely select last_month_activity as the root node or a very early split. Because this feature is so strong and is available to every tree at every split, the trees would end up being highly correlated. Their structures would be very similar, which limits the variance reduction you get from averaging them.

A Random Forest solves this by introducing feature randomness. At each split, a tree is only allowed to consider a small, random subset of the 10 features. For many of the splits, the last_month_activity feature won't even be in the candidate pool. This forces the trees to explore the predictive power of the other, moderately useful features. The result is a forest of much more diverse, de-correlated trees, leading to a more robust final model that is less reliant on a single dominant feature.


3. Applying Random Forests: Key Hyperparameters

Now that you understand the "why," let's touch on the "how." When you apply a Random Forest in practice (for example, using Python's scikit-learn library), there are two main hyperparameters that control the randomness we've discussed.

A Comprehensive Guide to Random Forests

This article, 'A Comprehensive Guide to Random Forests,' provides a great overview of the model's practical aspects, including its key hyperparameters.

First, quickly read section '2. Core Concepts' to reinforce your understanding of Feature Randomness. Then, read section '7. Hyperparameters...', focusing specifically on the discussion of n_estimators and max_features.

The two most important hyperparameters for controlling the model's structure are:

  1. n_estimators: This is the number of trees in the forest. It's the same idea as in bagging. More trees are generally better (up to a point of diminishing returns), but they increase the computational cost. A common starting point is 100 or more.

  2. max_features: This is the size of the random subset of features to consider at each split. This is the key hyperparameter that makes a Random Forest different from a standard bagged ensemble.

    • Effect: A smaller max_features increases randomness and de-correlates the trees, which reduces variance but can slightly increase the bias of individual trees (since they might not get to see the best predictors). A larger max_features makes the trees more similar and the model behaves more like standard bagging.
    • Common Settings: For a dataset with m total features, common defaults are max_features = sqrt(m) for classification and max_features = m/3 for regression. These are good starting points, but this hyperparameter is often a prime candidate for tuning to get the best performance.

Conclusion

You've now progressed from single decision trees to bagging, and finally to Random Forests. You can now explain not just how a Random Forest is built, but also articulate why its specific design—particularly the use of feature randomness—makes it one of the most robust and widely used algorithms in machine learning.

Key Takeaways:

  • A Random Forest is an ensemble of decision trees that improves on bagging by adding feature randomness.
  • At each split in a tree, only a random subset of features (max_features) is considered.
  • This additional randomness de-correlates the trees, preventing them from all relying on the same few strong predictors.
  • By averaging the predictions of these diverse, de-correlated trees, a Random Forest achieves a greater reduction in variance, leading to a more robust and accurate model.

Preview of the Next Lesson:
Random Forests and Bagging are known as parallel ensemble methods, where many independent models are built and their results are combined. In the next lesson, we will explore a fundamentally different approach to ensembles called Boosting. We will learn about Gradient Boosting, a sequential method where each new model is built to correct the errors of the previous one. This powerful technique focuses on reducing bias rather than variance.