Random Forests
Probability Estimates
scikit-learn
Machine Learning
Data Science

Random Forests - Probability Estimates scikit-learn specific

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In scikit-learn, a random forest classifier can return class probabilities through predict_proba, but those numbers are not magic confidence scores. They are built from the trees' class distributions and are often useful, but they can still be poorly calibrated depending on the data and the model settings.

How scikit-learn Computes Them

For classification, each decision tree routes a sample to one leaf. That leaf contains a class distribution based on the training samples that landed there. The tree returns those leaf-level class proportions, and the forest averages them across all trees.

So conceptually:

  1. every tree produces a per-class probability vector
  2. the forest averages those vectors
  3. the result becomes predict_proba

This is different from majority vote alone. predict uses the winning class, while predict_proba exposes the averaged class probabilities.

A Small Example

python
1from sklearn.datasets import load_iris
2from sklearn.ensemble import RandomForestClassifier
3from sklearn.model_selection import train_test_split
4
5x, y = load_iris(return_X_y=True)
6
7x_train, x_test, y_train, y_test = train_test_split(
8    x, y, test_size=0.25, random_state=42
9)
10
11model = RandomForestClassifier(
12    n_estimators=200,
13    random_state=42
14)
15model.fit(x_train, y_train)
16
17probabilities = model.predict_proba(x_test[:3])
18predictions = model.predict(x_test[:3])
19
20print(probabilities)
21print(predictions)

The rows in probabilities sum to 1.0, and each column corresponds to one class label from model.classes_.

Why the Probabilities Can Look Overconfident

Random forests often produce probabilities that look intuitive, but they are not always well calibrated. A model saying 0.95 does not automatically mean "this class is correct 95 percent of the time".

There are a few reasons:

  • tree leaves may contain very few samples
  • deep trees can become sharp and confident
  • class imbalance can distort probability interpretation

This is why probability estimates and calibrated probability estimates are not the same thing.

Inspect the Output Carefully

If you want to know which probability belongs to which class, use classes_:

python
print(model.classes_)
print(model.predict_proba(x_test[:1]))

That matters because the probability columns follow model.classes_, not an assumed class ordering in your head.

For binary classification, the second column usually corresponds to the positive class if your labels are ordered that way, but you should still check rather than assume.

Calibration Matters for Threshold Decisions

If you only care about top-1 class prediction, raw forest probabilities may be enough. If you are using the numbers for:

  • risk scoring
  • threshold tuning
  • downstream decision rules
  • cost-sensitive classification

then calibration becomes much more important.

In scikit-learn, a common next step is probability calibration:

python
1from sklearn.calibration import CalibratedClassifierCV
2
3calibrated = CalibratedClassifierCV(model, method="isotonic", cv=3)
4calibrated.fit(x_train, y_train)
5
6print(calibrated.predict_proba(x_test[:3]))

That does not guarantee perfection, but it often gives probabilities that are more meaningful for decision-making.

Out-of-Bag and Probability Thinking

Random forests can also provide out-of-bag scoring when bootstrap=True and oob_score=True, but that is about validation, not a replacement for predict_proba.

Do not confuse:

  • model evaluation
  • class prediction
  • calibrated probability estimation

They are related, but not interchangeable.

Common Pitfalls

  • Treating predict_proba as perfectly calibrated confidence without checking.
  • Forgetting that the probability columns follow model.classes_.
  • Using hard class predictions when the real application needs threshold-based decisions.
  • Assuming a high random forest accuracy automatically means the probabilities are trustworthy.
  • Ignoring class imbalance, which can distort how the probability estimates should be interpreted.

Summary

  • In scikit-learn, random forest probabilities come from averaging per-tree class distributions.
  • 'predict_proba is useful, but the numbers are not automatically well calibrated.'
  • Always check model.classes_ to interpret the probability columns correctly.
  • If decision thresholds matter, consider calibrating the model.
  • Random forest probability estimates are helpful, but they should be treated as model outputs to evaluate, not as unquestionable truth.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.