Build Random Forest Classifiers

Learn to build random forest classifiers in Python for data science — hands-on steps, troubleshooting, and what to study next.

Focus: build random forest classifiers

Sponsored

If you've ever trained a single decision tree and watched it overfit your training data while flailing on new samples, you know the frustration. Random forests solve this by turning one noisy model into a committee of hundreds, each voting on the final answer. In this lesson, you'll build random forest classifiers in Python, understand the magic behind the ensemble, and learn exactly when to reach for this workhorse over other algorithms.

The problem this lesson solves

Single decision trees are the model equivalent of a brilliant but erratic colleague. They memorize patterns in your training data with perfect fidelity — and then choke on anything slightly different. This is overfitting: the tree learns noise alongside signal, so its impressive training accuracy collapses on real-world data. You've likely seen it: a tree hits 99% on training, then 70% on validation.

Random forests solve this by building many trees, each trained on a random subset of data and features, then combining their predictions. This practice of combining multiple models is called ensemble learning. The result? Variance drops dramatically, accuracy improves, and the model generalizes far better to unseen data.

But there's a catch: you need to understand how to build and tune them correctly. In this lesson, you'll move from a shaky single tree to a robust forest, complete with code, tuning choices, and edge cases that trip up beginners.

Core concept / mental model

Think of a random forest as a panel of experts in a hospital. One doctor might be brilliant but tired; another might misdiagnose a rare disease. But when you gather a diverse panel — different specialties, different training, different biases — and have them vote, the collective diagnosis is almost always more accurate than any single expert.

In machine-learning terms: - Bagging (Bootstrap Aggregating) creates many datasets by sampling with replacement from your original data. Each tree sees a slightly different view. - Random feature selection ensures that each split considers only a random subset of features, so trees don't all look identical. - Voting (for classification) or averaging (for regression) combines outputs.

This is the bias-variance tradeoff in action: a single tree has low bias but high variance. Averaging many trees keeps the bias low while averaging out the variance.

Random Forest = Many Decorrelated Decision Trees (bagging + random features) + Aggregate Vote

How it works step by step

Here's the exact algorithm behind building a random forest classifier:

  1. Bootstrap sampling: For each of the n_estimators trees, create a training set by randomly sampling your original data with replacement. Some rows appear multiple times; others are left out (these become the "out-of-bag" samples).
  2. Grow a decision tree on each bootstrap sample. At each node, instead of evaluating all features, select a random subset of max_features (often √p for classification).
  3. Split using the best feature in that subset (e.g., Gini impurity or entropy).
  4. Keep growing until a stopping rule (like max_depth or a minimum samples split) is met — no pruning needed.
  5. Predict: for a new sample, each tree votes for a class. The class with the most votes wins.

Why does this work? Randomizing both data and features decorrelates the trees. If all trees were identical, averaging wouldn't help. Diversity is key — that's why random forests often beat gradient boosting when data is noisy.

Hands-on walkthrough

Let's build a random forest classifier with scikit-learn. If you don't have it, install it:

pip install scikit-learn

Step 1: Load and prepare data

We'll use the classic iris dataset — perfect for classification practice.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
X, y = iris.data, iris.target

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)
print(X_train.shape, X_test.shape)

Expected output:

(105, 4) (45, 4)

Step 2: Train the forest

from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(
    n_estimators=100,
    max_depth=3,
    random_state=42
)
clf.fit(X_train, y_train)

print(f"Train accuracy: {clf.score(X_train, y_train):.3f}")
print(f"Test  accuracy: {clf.score(X_test, y_test):.3f}")

Expected output (varies slightly):

Train accuracy: 0.981
Test  accuracy: 0.978

Notice test accuracy is close to training — that's the forest fighting overfitting.

Step 3: Inspect feature importance

One huge advantage: random forests give feature importance for free.

for name, importance in zip(iris.feature_names, clf.feature_importances_):
    print(f"{name}: {importance:.3f}")

Expected output:

sepal length (cm): 0.042
sepal width (cm): 0.010
petal length (cm): 0.820
petal width (cm): 0.128

Petal length dominates — consistent with iris classification knowledge.

Step 4: Make predictions

# Predict class for a new sample: setosa-like
sample = [[5.1, 3.5, 1.4, 0.2]]
print(f"Predicted class: {iris.target_names[clf.predict(sample)[0]]}")
print(f"Probabilities:   {clf.predict_proba(sample)[0]}")

Expected output:

Predicted class: setosa
Probabilities:   [1. 0. 0.]

The forest is very confident — all trees agree.

Compare options / when to choose what

Random forests aren't the only ensemble game. Here's how they stack up:

Model Speed (train) Accuracy Interpretability Handles noisy data Hyperparameters to tune
Decision Tree ★★★★★ ★★ ★★★★★ 1–2
Random Forest ★★★ ★★★★ ★★★ ★★★★ 4–6
Gradient Boosting (XGBoost) ★★ ★★★★★ ★★ ★★★ 8+
Logistic Regression ★★★★★ ★★★ ★★★★ ★★★ 1–2

When to choose random forest: - You have tabular data (rows/columns) - You need good accuracy out of the box without heavy tuning - You want feature importance for insight - You're less concerned about model interpretability than a single tree

When to choose something else: - Need fast predictions on massive data → gradient boosting or logistic regression - Need a fully interpretable model → decision tree or logistic regression - Data is text/images → neural networks

Key hyperparameters to tweak

  • n_estimators — number of trees. More is better but slower (returns diminish past ~500)
  • max_depth — limits tree depth; prevents overfitting
  • max_features — size of the random feature subset; sqrt(p) for classification is a good start
  • min_samples_split — minimum samples to split a node

Pro tip: Use RandomizedSearchCV or GridSearchCV to tune these, but remember that random forests are already quite robust to default parameters.

Troubleshooting & edge cases

1. "It's too slow!"

  • Reduce n_estimators (try 100 instead of 1000)
  • Set n_jobs=-1 to parallelize across cores
  • Reduce max_depth

2. "Training accuracy is perfect but test is 60%"

Your forest is still overfitting. Try: - Deeper max_depth? No — decrease it. - Increase min_samples_split (e.g., 10 or 20) - Increase min_samples_leaf - More trees don't help overfitting; reduce complexity instead.

3. "All features have importance near zero"

  • With many correlated features, importance splits among them. This is fine.
  • Consider dropping constants or near-constants.
  • Use permutation_importance for more reliable estimates.

4. "Class imbalance is killing my predictions"

Set class_weight='balanced' to give minority classes more weight.

5. "Predictions on new data differ wildly each run"

Set random_state for reproducibility. If you're comparing models, always fix this.

What you learned & what's next

You now know how to build random forest classifiers — from the mental model of bagging and random feature selection to hands-on code with scikit-learn. You can: - Explain why forests beat single trees (bias–variance tradeoff) - Train a forest and evaluate its test accuracy - Interpret feature importance - Choose between random forests and other models - Troubleshoot common pitfalls like overfitting, slowness, and imbalance

Next in the track: Now that you can build a solid classifier, the next lesson will teach you how to tune hyperparameters systematically with cross-validation, so you can squeeze out even more performance from your forests — and every model you build afterward.

Takeaway: Random forests are powerful, but they're not magic. Understand what each knob does, and you'll build models that generalize, not just memorize.

Practice recap

Practice building a random forest classifier on the wine dataset (load_wine()). Train a forest with 100 trees, then experiment with max_depth from 2 to 10 and note how training vs. test accuracy changes. Finally, print feature importances and identify which chemical property drives the classification most.

Common mistakes

  • Forgetting to set random_state leads to irreproducible results — always fix it for experiments.
  • Keeping n_estimators low (like 2–3) misses the whole point of an ensemble; start with at least 100.
  • Assuming a random forest can't overfit — too deep or too greedy trees can still overfit noisy data.
  • Tuning only n_estimators and ignoring max_depth, min_samples_split, or max_features, which matter more for variance.
  • Not scaling data before using a random forest — it's actually a feature, but many beginners waste time scaling anyway.

Variations

  1. Try ExtraTreesClassifier for even more randomness — great for high-dimensional data.
  2. Use RandomForestRegressor for continuous targets — same API, but predicts an average instead of vote.
  3. Switch to HistGradientBoostingClassifier for large datasets (>100k rows) — faster and often more accurate.

Real-world use cases

  • Bank credit risk modeling: predict loan default risk from tabular applicant data with thousands of features.
  • Medical diagnosis: use feature importance to identify the most predictive biomarkers for disease screening.
  • Customer churn prediction: classify which subscribers will cancel, using demographics, usage logs, and support tickets.

Key takeaways

  • Random forests combine many decorrelated decision trees via bagging and random feature selection.
  • They drastically reduce variance compared to a single decision tree, improving generalization.
  • Use RandomForestClassifier from scikit-learn: fit, score, and predict in a few lines.
  • Feature importance gives you a built-in, human-readable ranking of which features matter.
  • Tune max_depth, min_samples_split, and max_features — not just n_estimators — to fight overfitting.
  • Set random_state for reproducibility and use class_weight='balanced' for imbalanced classes.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.