Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 32

Random Forest Classifier

Let many trees vote on the class so the label does not depend on one unlucky split. This lesson includes one worked example.

Intermediate12 min
Random Forest Classifier MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

Each tree in a random forest is trained on a resampled table and a random subset of inputs. Every tree casts a class vote. The forest predicts the class with the most votes. One overfit tree is outnumbered.

Example

python
import numpy as np
from sklearn.ensemble import RandomForestClassifier

X = np.array([[1], [2], [3], [7], [8], [9]], dtype=float)
y = np.array([0, 0, 0, 1, 1, 1])
label = RandomForestClassifier(n_estimators=20, max_depth=2, random_state=0).fit(X, y).predict([[6]])[0]
print(f"label: {int(label)}")

Output:

text
label: 1

When to use it

  • Use it as a default classifier on tables with mixed numeric inputs.
  • The vote share is a rough confidence, not a calibrated probability.
  • Feature importances are a hint, not a proof of cause.

The theory lesson Ensemble Methods explains why the vote helps.

Loading playground…