Learning Guides
Random Forest Algorithm Explained with Examples
Quick answer: Learn how the Random Forest algorithm works, why it reduces overfitting compared to a single decision tree, feature importance, and its strengths and weaknesses.
What is Random Forest?
Random Forest is an ensemble Machine Learning algorithm that builds many decision trees, each trained on a random subset of the data and a random subset of features, then combines their predictions through majority vote (classification) or averaging (regression).
Why Not Just Use One Decision Tree?
A single decision tree tends to overfit, memorising the specific noise and quirks of its training data rather than the genuine underlying pattern. Random Forest reduces this problem by averaging across many trees, each of which sees slightly different data, so their individual errors tend to cancel out.
How Random Forest Works
Create multiple random samples of the training data, drawn with replacement, a technique called bootstrapping
Train a separate decision tree on each sample
At each split within each tree, consider only a random subset of features, not all of them
Combine all trees' predictions through majority vote for classification, or averaging for regression
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=200, max_depth=10, random_state=42)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Key Hyperparameters
| Parameter | What it controls |
|---|---|
| n_estimators | Number of trees in the forest |
| max_depth | Maximum depth of each individual tree |
| max_features | Number of features considered at each split |
| min_samples_split | Minimum samples required to split a node further |
Feature Importance
Random Forest can rank which features contributed most to its predictions, based on how much each feature reduced impurity across all the trees' splits, a useful tool for understanding a model's behaviour even though the forest as a whole is less interpretable than a single tree.
import pandas as pd
importances = pd.Series(model.feature_importances_, index=X_train.columns)
print(importances.sort_values(ascending=False))
Strengths and Weaknesses
| Strengths | Weaknesses |
|---|---|
| Handles both classification and regression well | Less interpretable than a single decision tree |
| Resistant to overfitting compared to one tree | Slower to train and predict than simpler models |
| Handles missing data and mixed feature types reasonably well | Can still overfit with very noisy data if untuned |
Random Forest vs a Single Decision Tree
A single decision tree is fast, fully interpretable, but prone to overfitting. Random Forest sacrifices some interpretability and speed in exchange for meaningfully better generalisation to new, unseen data.
Common Interview Questions
Why does Random Forest generalise better than a single decision tree?
Each tree sees a different random subset of data and features, so any single tree's tendency to overfit its specific subset gets averaged out across the many trees in the forest.
What is the difference between bagging in general and Random Forest specifically?
Random Forest is a specific implementation of bagging applied to decision trees, with the additional step of randomly selecting a subset of features to consider at each split, which further decorrelates the individual trees.
FAQ
Frequently Asked Questions
What is the Random Forest algorithm?
An ensemble Machine Learning algorithm that builds many decision trees on random subsets of data and features, then combines their predictions through majority vote or averaging.
Why is Random Forest less prone to overfitting than a single decision tree?
Each tree in the forest sees a different random subset of data and features, so averaging across many such trees smooths out any single tree's tendency to overfit its own subset.
What does feature importance mean in a Random Forest model?
It ranks which features contributed most to the model's predictions, based on how much each feature reduced impurity across all the trees' splits.
Is Random Forest used for both classification and regression?
Yes. For classification it combines trees through majority vote, and for regression it averages the trees' predicted values.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin