Core ML Models Guide 3 of 6 · tap to switch
Random Forests
Average hundreds of decorrelated decision trees trained on random bootstrap bags to crush variance and win on tabular datasets out of the box.
In 1906, statistician Francis Galton visited a country fair where 787 villagers tried to guess the weight of an ox. Almost every individual guess missed wildly, some hundreds of pounds too high, others too low. Yet when Galton averaged all 787 guesses together, the crowd landed within one single pound of the true weight.
A Random Forest (Leo Breiman, 2001) puts that exact Wisdom of the Crowd to work on Decision Trees. One deep tree is jumpy and overfits stray outliers. Grow 100 slightly different trees in parallel and let them vote: individual mistakes cancel out, and blocky rectangular boxes melt into a smooth diagonal decision surface!
Click from 1 Tree up to 100 Trees below and watch the chaotic boundaries calm down while accuracy on unseen Test customers climbs!
Notice a reassuring property: adding more trees to a Random Forest does not cause overfitting. Past roughly 100 to 300 trees, error on unseen data simply settles smoothly onto a flat plateau.
How Random Forest Works: Bootstrap Bagging & Out-of-Bag Error
If you feed the exact same table to 100 identical Decision Trees, you get 100 identical clones. So how does a Random Forest make every tree genuinely different?
First, it uses Bootstrap Aggregating (Bagging): to build each tree, it draws a full-sized bag of rows from your training table with replacement (like pulling marbles from a jar and tossing each marble right back in before the next pull). Some rows get picked twice or three times, while roughly 36.8% never get picked for that tree at all!
Click Draw Tree #2 below to inspect how each tree sees a fresh slice of the table, and watch the untouched amber Out-of-Bag (OOB) rows appear automatically.
Because Tree #1 never saw its amber Out-of-Bag rows during training, we can grade every row using ONLY the trees that skipped it. That gives you a trustworthy built-in validation benchmark practically for free.
Trick #2: Random Feature Subsampling (what separates Forests from plain Bagging)
Row bagging alone has a hidden flaw. Suppose one powerhouse column (like Debt Ratio) is much stronger than every other clue. Even on bootstrapped bags, nearly every tree will still pick Debt Ratio as its very first top question! Asking 100 near-identical clone trees to vote barely helps because they all share the same blind spots and make the same mistakes.
Random Forests add a brilliant twist: at every single split, each tree is only allowed to look at a small random subset of columns. If your spreadsheet has 9 columns, each split only peeks at 3 random columns (the square root of the total, often written as √p for classification). Temporarily hiding the dominant clue forces other trees to uncover real patterns in Credit Score, Savings, and Income. When genuinely diverse trees vote together, one tree’s mistake gets canceled out by another tree’s strength.
Toggle between Plain Bagging and Random 2-of-6 Columns below to see how hiding dominant clues temporarily unlocks genuine team diversity!
Which clues actually helped? Catching the ID-column trap
Once a Random Forest finishes training, teams naturally want to know which columns mattered most. By default, most tools report a quick Default Split Score, officially called Mean Decrease in Impurity (MDI). It adds up how much each column cleaned up the training groups every time a tree split on it.
There is one famous catch: the Default Split Score is easily fooled by columns with lots of unique values, even a completely random Customer Account ID! Because every row has its own unique ID number, deep trees find endless lucky cutoffs on that ID column to memorize training rows, making pure noise look like a star predictor.
The honest check is a Shuffle Test, officially called Permutation Importance. After the forest is trained, you take an unseen validation table and shuffle ONE column like a deck of cards, scrambling its values while keeping all other columns untouched. If shuffling Support Tickets causes validation accuracy to plunge, that clue truly helps. If shuffling Account ID changes accuracy by 0%, you have caught a fake clue red-handed!
Toggle between Default Split Score (MDI) and Shuffle Test (Permutation Importance) below to watch a fake noise ID get exposed instantly.
See Breiman's Ensemble Variance Equation & the 1/e OOB DerivationThesis / Reference
1. Why Decorrelation Drives Random Forests (Breiman, 2001): Suppose an ensemble averages identically distributed trees, each having individual prediction variance and pairwise correlation between any two trees. The variance of their average is exact:
As , the second term vanishes to zero, leaving an irreducible variance floor of . Plain Bagging reduces but leaves high; random feature subsampling (max_features = sqrt(p) for classification, p/3 for regression) directly shrinks so the ensemble variance floor plummets!
2. Exact Out-of-Bag (OOB) Probability: When drawing rows with replacement from a table of size , the probability that any particular training row is skipped on every single draw approaches :
3. Unbiased Feature Attribution: Strobl et al. (2007) demonstrated that Gini/MDI importance inflates continuous and high-cardinality noise variables because more candidate cutpoints inflate training split gains. Permutation Importance (computed on OOB or held-out validation data) measures the actual drop in predictive performance when column is randomly permuted, eliminating training overfitting bias (while keeping correlated twin columns in mind, where SHAP or grouped permutation shines).
See Random Forest + OOB Score + Permutation Importance in PythonOptional Code
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
rf = RandomForestClassifier(
n_estimators=300,
max_features="sqrt", # decorrelates trees at every split
oob_score=True, # free Out-of-Bag validation estimate
n_jobs=-1, # train trees in parallel across CPU cores
random_state=42,
)
rf.fit(X_train, y_train)
print("Built-in OOB accuracy:", round(rf.oob_score_, 3))
# Honest held-out Permutation Importance (immune to high-cardinality MDI bias)
perm = permutation_importance(rf, X_val, y_val, n_repeats=10, random_state=42)
for name, mean in sorted(zip(feature_names, perm.importances_mean), key=lambda x: -x[1]):
print(f"{name:24s} -> {mean:.4f}")Parallel voting vs. learning from your mistakes
Random Forests train hundreds of independent trees in parallel to average away error wobble, without ever checking what previous trees got wrong.
What if instead we built a relay team of small trees in sequence, where every brand-new tree focuses obsessively on fixing the exact leftover mistakes of the team so far?
That brings us to the reigning champion of tabular ML: Gradient Boosting (XGBoost & LightGBM).
Quick Intuition Check
Four quick real-world scenarios. No scores, just test your intuition.
What crucial ingredient separates a Random Forest from plain Bagging of Decision Trees?
Roughly what share of training rows are left untouched (Out-of-Bag) by any single bootstrapped tree in a large dataset?
Increasing the number of trees in a Random Forest from 200 to 1,000 will cause severe overfitting on unseen data.
Your Random Forest's Default Split Score (MDI) ranks a randomly generated 6-digit Customer Reference ID as the #2 most important clue. What should you do?
Common Questions
Is Random Forest supervised or unsupervised, and why is it good for classification?
What is the difference between Bagging and a Random Forest?
Does adding more trees (n_estimators) cause a Random Forest to overfit?
What is Out-of-Bag (OOB) error in Random Forests?
Why should you be cautious with built-in feature_importances_ (MDI)?
When should you pick Random Forests vs. Gradient Boosting (XGBoost / LightGBM)?
Related Visual Guides
Decision Trees & Split Impurity
Turn messy tables into crystal-clear flowcharts of yes/no questions, understand Gini Impurity vs. Entropy, and see why pruning stops overfitted boxes.
Gradient Boosting (XGBoost & LightGBM)
Build a relay team of small trees where every new tree fixes the leftover mistakes of the previous ones, dominating real-world tabular ML.
Overfitting vs Underfitting & Bias-Variance
How to tell real learning apart from blind memorization, understand the bias-variance tradeoff, and cure overfit models.
Written & illustrated by Muhammad Aashir Irshad · Updated December 2026 · Free open-access reference
Cite this guide in a paper, thesis, or course (BibTeX & APA)
@misc{irshad2026randomforests,
author = {Irshad, Muhammad Aashir},
title = {Random Forests: Interactive Visual Guide},
howpublished = {SeeingML},
year = {2026},
month = {dec},
url = {https://seeingml.com/learn/random-forests/},
note = {Interactive visual guide}
} Irshad, M. A. (2026). Random Forests: Interactive Visual Guide. SeeingML. https://seeingml.com/learn/random-forests/