Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Machine Learning›Random Forests

Supervised Learning

Random Forests

Random Forests are an ensemble learning method that constructs a multitude of decision trees during training to output the mode or mean prediction of individual trees. By aggregating these predictions, the algorithm significantly reduces the variance inherent in single trees, leading to more robust and generalized models. You should reach for Random Forests when you need a highly accurate, out-of-the-box solution that handles non-linear data well without extensive hyperparameter tuning.

Understanding Decision Tree Instability

To understand why Random Forests are effective, we must first analyze the primary weakness of individual decision trees: high variance. A single decision tree is notorious for being overly sensitive to the specific training data it receives. If you change a small subset of the training samples, the resulting tree structure can change drastically, leading to wildly different predictions. This phenomenon is known as overfitting, where the model captures noise rather than the underlying signal. Because a single tree creates rigid decision boundaries based on hierarchical splits, it tends to create complex, jagged regions that fail to generalize to new, unseen data. Understanding this instability is the key to appreciating why ensemble methods are necessary. By stacking these fragile learners, we aim to transform an unreliable predictor into a stable consensus, moving away from high-variance individual models toward a low-variance aggregate model that captures more reliable patterns.

from sklearn.tree import DecisionTreeClassifier
# A single tree often overfits to the training data
model = DecisionTreeClassifier(max_depth=None)
# Even small changes in data lead to completely different splits

Bootstrap Aggregating (Bagging)

The foundational mechanism behind a Random Forest is Bagging, or bootstrap aggregating. The process begins by creating multiple subsets of the original training data using sampling with replacement, a technique known as bootstrapping. Each subset is used to train an independent decision tree. Because each tree is trained on a slightly different version of the dataset, each tree will learn slightly different decision boundaries and capture different noise patterns. When it is time to make a prediction, the ensemble aggregates the results from all individual trees—typically through a majority vote for classification or averaging for regression. The magic happens because, while the individual trees are high-variance and prone to error, the average of these diverse, decorrelated errors tends toward zero. This effectively smooths out the volatile decision boundaries, resulting in a model that is significantly more stable and robust to input variations across the entire feature space.

import numpy as np
# Simulating bootstrap sampling
n_samples = 100
indices = np.random.choice(n_samples, size=n_samples, replace=True)
# Each tree trains on a different set of indices

Feature Randomness and Decorrelation

While Bagging creates diversity through row sampling, Random Forests introduce a second layer of randomness: feature sampling. At each split point within every decision tree, the algorithm considers only a random subset of the available features rather than evaluating all of them. This is crucial because, in many datasets, a few strong features might dominate the splitting process. If we didn't restrict the feature set, every tree in the forest would likely choose the same dominant features at the top nodes, leading to highly correlated trees that behave similarly. By forcing each tree to look at different combinations of features, the forest ensures that individual trees are decorrelated. This process increases the 'wisdom of the crowd' effect because the ensemble is no longer reliant on any single feature or a specific tree's bias. When these diverse, decorrelated trees are combined, the resulting forest becomes remarkably resistant to overfitting even on complex, high-dimensional datasets.

from sklearn.ensemble import RandomForestClassifier
# max_features controls the randomness per split
# 'sqrt' is a common default for classification tasks
rf = RandomForestClassifier(n_estimators=100, max_features='sqrt')

Evaluating Model Performance and Importance

One of the major advantages of the Random Forest architecture is the inherent ability to measure feature importance. Since the model builds many trees that evaluate features at various nodes, we can track how much each feature contributes to decreasing the impurity (such as Gini impurity or entropy) across the entire forest. This provides a clear, quantitative ranking of which variables are actually driving the model's predictions. Furthermore, we can evaluate performance using 'Out-of-Bag' (OOB) error. Because each tree is only trained on a bootstrap sample, the samples left out (the 'out-of-bag' samples) act as a built-in validation set. This allows us to estimate the generalization error of the forest during the training process itself, eliminating the immediate need for a separate validation split during initial prototyping. This internal feedback loop makes the model easier to tune and verify without wasting precious data resources or requiring complex external cross-validation setups for every minor iteration.

# Accessing feature importance after training
rf.fit(X_train, y_train)
importances = rf.feature_importances_
# Higher values indicate more influential features

Practical Constraints and Scalability

Despite their power, Random Forests are not without trade-offs. Because the ensemble consists of many deep decision trees, the model can become quite large in terms of memory usage, and making predictions can be slower than using a simple linear model. If the dataset has a massive number of features, the individual trees may still struggle if the 'signal' is sparse. Additionally, while Random Forests are excellent at preventing overfitting, they do not extrapolate well outside the range of the training data. For example, if you are performing regression and the input values in the test set fall outside the range of values seen during training, the forest will simply predict the average value of the nearest training instances, as trees cannot perform linear extrapolation. Understanding these limitations is critical for deploying robust machine learning systems where input ranges might drift or where inference latency is a hard constraint on the production environment.

# Check model size and prediction speed
import sys
print(f"Model size in memory: {sys.getsizeof(rf)} bytes")
# Inference on new data points
predictions = rf.predict(X_new)

Key points

  • Random Forests reduce model variance by aggregating predictions from multiple independent decision trees.
  • Bagging involves training individual trees on bootstrap samples to introduce diversity in the data seen by each learner.
  • Feature randomness forces trees to consider different attributes at each split, preventing individual features from dominating the model.
  • Decorrelation of trees ensures that errors made by one tree are likely to be offset by the correct predictions of others.
  • Out-of-bag error provides a built-in way to estimate generalization performance without needing an external validation set.
  • Feature importance scores are derived naturally from the impurity reduction across all nodes in the forest.
  • Random Forests excel at capturing complex, non-linear patterns but struggle with extrapolation beyond the training data range.
  • The ensemble approach effectively balances the trade-off between bias and variance, leading to robust results with minimal hyperparameter tuning.

Common mistakes

  • Mistake: Thinking Random Forests improve performance by training on the entire dataset for every tree. Why it's wrong: This would result in highly correlated trees that overfit. Fix: Use bootstrap aggregating (bagging) to train each tree on a random subset of the data.
  • Mistake: Assuming more trees always leads to better performance. Why it's wrong: While Random Forests do not overfit by adding more trees, they do reach a point of diminishing returns where computational cost outweighs predictive gains. Fix: Monitor out-of-bag error and stop adding trees once the error plateaus.
  • Mistake: Neglecting to tune the 'max_features' hyperparameter. Why it's wrong: If all features are considered at every split, trees become highly correlated, reducing the benefit of the ensemble. Fix: Test different values for the number of features considered at each split to ensure tree diversity.
  • Mistake: Believing that Random Forests provide the same interpretability as a single decision tree. Why it's wrong: A single tree is a white-box model, but an ensemble of hundreds of trees is opaque. Fix: Use feature importance metrics or SHAP values to gain insight into the model's logic.
  • Mistake: Using Random Forests on data with extremely high dimensionality and many irrelevant features. Why it's wrong: Random Forests can struggle to pick up signal in sparse, high-dimensional spaces compared to algorithms that apply regularization. Fix: Perform feature selection or dimensionality reduction prior to training.

Interview questions

What is a Random Forest and how does it function at a high level?

A Random Forest is an ensemble learning method that constructs a multitude of decision trees during the training phase. It functions through the concept of bagging, or bootstrap aggregating, where each individual tree is trained on a random subset of the training data. For classification tasks, the output is the class selected by the majority of trees, and for regression, it is the average prediction of the individual trees. This technique reduces the variance of the model significantly without increasing the bias, which helps prevent overfitting, a common issue when using a single deep decision tree.

What are the two main sources of randomness used in Random Forests, and why are they important?

The two primary sources of randomness are bootstrap sampling of the data and feature randomness. First, bootstrap sampling involves drawing samples from the training set with replacement, ensuring each tree sees a slightly different version of the data. Second, when splitting a node, the algorithm considers only a random subset of features rather than all of them. This is crucial because it decorrelates the trees; if one feature is a very strong predictor, it would dominate every tree without this restriction. Decorrelation ensures that the ensemble is robust and generalizes better to unseen data.

How does a Random Forest handle the bias-variance tradeoff compared to a single deep decision tree?

A single deep decision tree has low bias but very high variance, meaning it is highly sensitive to the specific noise in the training set, leading to poor generalization. A Random Forest addresses this by averaging multiple high-variance, low-bias trees. While the bias of the forest is roughly the same as a single tree, the variance is reduced because the average of uncorrelated variables has lower variance than a single variable. Mathematically, if we have N trees with variance sigma squared, the variance of the ensemble is reduced roughly by a factor of N, assuming the trees are not perfectly correlated.

Compare Random Forests with Gradient Boosted Trees. When would you choose one over the other?

Random Forests and Gradient Boosted Trees are both ensemble methods, but they work differently. Random Forests build trees independently in parallel using bagging to reduce variance. Gradient Boosting builds trees sequentially, where each new tree attempts to correct the errors made by the previous ones, primarily reducing bias. You would choose a Random Forest when you need a model that is easy to tune and unlikely to overfit. You would choose Gradient Boosting when you have a well-defined dataset and require maximum predictive accuracy, as it can typically achieve better performance if hyperparameter tuning is handled carefully.

Explain the concept of Out-of-Bag (OOB) error and why it is a useful metric in Random Forests.

The Out-of-Bag error is an internal validation method unique to bagging techniques. Since each tree is trained on a bootstrap sample that excludes about 36.8% of the original data, those excluded samples are 'out-of-bag' for that specific tree. We can pass these OOB samples through their respective trees to calculate an error rate without needing a separate validation set. This is incredibly useful because it provides an unbiased estimate of the generalization error during the training process, saving computational resources and allowing for real-time model monitoring during the training phase.

If you were implementing a Random Forest from scratch using Python, how would you approach the feature selection at each node split?

To implement feature selection at each node split, you would restrict the feature space to a randomly sampled subset of size 'm', typically the square root of total features for classification. For each feature in this subset, you would calculate the impurity reduction, such as Gini impurity or entropy for classification, or mean squared error for regression. You then select the feature and the split threshold that maximizes this reduction. Code-wise, this looks like: 'subset = np.random.choice(features, size=m, replace=False); best_split = find_best_split(subset, data)'. This randomness is the mechanism that enforces diversity among the trees, preventing them from becoming clones of one another.

All Machine Learning interview questions →

Check yourself

1. Why does a Random Forest typically exhibit lower variance than a single decision tree?

  • A.It uses a boosting process to reduce bias iteratively.
  • B.It averages the predictions of multiple deep trees trained on different bootstrap samples.
  • C.It forces each tree to use a different set of input features exclusively.
  • D.It uses a global optimization function to find the best possible split across all trees.
Show answer

B. It averages the predictions of multiple deep trees trained on different bootstrap samples.
Averaging decorrelated trees reduces variance without significantly increasing bias. Option 0 describes Gradient Boosting, option 2 is incorrect because feature sets overlap, and option 3 describes a single global model, not an ensemble.

2. What is the primary effect of the 'max_features' hyperparameter in a Random Forest?

  • A.It determines the maximum number of trees allowed in the forest.
  • B.It controls the depth of each individual tree to prevent overfitting.
  • C.It influences the correlation between trees by limiting the feature set per split.
  • D.It defines the total number of input variables the model can process.
Show answer

C. It influences the correlation between trees by limiting the feature set per split.
By limiting the features at each split, the trees become less correlated, which is the core strength of the ensemble. Option 0 refers to 'n_estimators', option 1 refers to 'max_depth', and option 3 is not a standard hyperparameter.

3. What does the 'Out-of-Bag' (OOB) error represent in a Random Forest?

  • A.The error calculated on a hold-out test set provided by the user.
  • B.The error rate on samples that were not included in the bootstrap sample of a specific tree.
  • C.The error resulting from trees that were pruned too aggressively.
  • D.The bias introduced by having too few features to split on.
Show answer

B. The error rate on samples that were not included in the bootstrap sample of a specific tree.
OOB allows for model validation during training without a separate validation set. Option 0 is a standard test set, while 2 and 3 do not define OOB.

4. When comparing a Random Forest to a single Decision Tree, which statement is most accurate regarding bias and variance?

  • A.Both bias and variance are significantly reduced.
  • B.Bias is reduced, while variance remains the same.
  • C.Variance is reduced, while bias remains relatively similar.
  • D.Bias is increased, while variance is significantly reduced.
Show answer

C. Variance is reduced, while bias remains relatively similar.
Bagging (the foundation of Random Forest) primarily targets variance reduction. Bias usually remains similar to that of a single unpruned decision tree. Option 0 is wrong because bias isn't the primary target, and 3 is wrong because the bias does not increase.

5. How does Random Forest handle feature importance, and what is its main limitation?

  • A.It calculates importance based on the reduction in impurity, but can be biased toward high-cardinality features.
  • B.It calculates importance by testing every feature permutation, making it very computationally efficient.
  • C.It uses linear coefficients, but the interpretation is limited by multicollinearity.
  • D.It defines importance by the depth of the tree, which ignores feature influence at shallow nodes.
Show answer

A. It calculates importance based on the reduction in impurity, but can be biased toward high-cardinality features.
Impurity-based importance is standard but favors variables with many categories. Option 1 is incorrect because permutation importance is separate, 2 is incorrect because forests aren't linear models, and 3 is incorrect because depth does not determine importance.

Take the full Machine Learning quiz →

← PreviousDecision TreesNext →Gradient Boosting — XGBoost, LightGBM

Machine Learning

35 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app