Random Forest
Random Forest builds many Decision Trees and combines their answers. For classification, the trees vote. For regression, their numeric predictions are usually averaged. The group often gives a more reliable result than one tree alone.
What does it do?
It combines predictions from a collection of trees.
Imagine asking 25 different decision trees whether a student will pass. If 19 trees say Pass and 6 say Fail, the forest predicts Pass.
Quick facts
Features such as study hours and attendance.
A class or a number.
Supervised: trees learn from labelled examples.
Randomness makes trees different; combining them reduces the effect of one tree's mistakes.
1. How does Random Forest work?
Each tree gets a slightly different view of the training data.
A forest votes together
This diagram shows five sample trees. Each one gives a prediction. The forest uses the majority vote.
Four steps in training
Each tree trains on a bootstrap sample: rows are drawn at random, with replacement.
At each split, the tree considers a random subset of features.
Build many trees so they do not all make exactly the same decisions.
Use majority vote for classification or an average for regression.
2. Live Random Forest Playground
Change the number of trees and see how their votes combine.
Tree voting board
Each tree casts one vote for Pass or Fail.
Experiment controls
Choose the number of trees and change how mixed the sample is.
3. The mathematics behind Random Forest
The final answer is made by combining the predictions of the individual trees.
Classification: majority vote
Mode means the most common answer. If 7 out of 10 trees predict Pass, Pass wins.
Regression: average
If trees predict 20, 22, and 24 lakh, the forest prediction is 22 lakh.
Bootstrap sampling
Each tree receives a slightly different training sample. Some original rows are left out for that tree.
Random feature selection
This makes trees less alike. Their combined predictions can be more stable than one large tree.
4. Numerical worked example
Let's combine the predictions of seven trees.
Example: Will a student pass?
A forest has seven trees. Their predictions are: Pass, Pass, Fail, Pass, Pass, Fail, Pass.
Pass = 5 trees
Fail = 2 trees
Total = 7 trees
Pass share = 5/7 ≈ 71.4%
Fail share = 2/7 ≈ 28.6%
5. How do we evaluate Random Forest?
Use the same task-specific measures used for other classification or regression models.
For classification
Share of predictions that are correct.
Useful when classes are uneven or mistakes have different costs.
Shows which classes the forest mixes up.
For regression
Average size of prediction errors.
Penalise larger prediction errors more strongly.
Compares predictions with a simple average-based baseline.
6. Python implementation
Train a Random Forest classifier with scikit-learn.
Example code
Study hours and attendance are used to predict pass/fail.
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.metrics import classification_report
# Each row is one student: [study hours, attendance %]
X = np.array([
[1, 40], [2, 50], [3, 60], [4, 65], [5, 70],
[6, 75], [7, 80], [8, 85], [9, 90], [10, 95],
[2, 45], [4, 68], [6, 72], [8, 88], [9, 96]
])
# 0 = Fail, 1 = Pass
y = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1])
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
model = RandomForestClassifier(
n_estimators=100,
max_depth=4,
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
print("Confusion matrix:\\n", confusion_matrix(y_test, predictions))
print(classification_report(y_test, predictions))
# Predict for a new student
new_student = np.array([[6, 80]])
print("Predicted class:", model.predict(new_student)[0])
print("Class probabilities:", model.predict_proba(new_student)[0])
# See which features were useful to the fitted forest
print("Feature importances:", model.feature_importances_)7. Real-life applications
Random Forest is useful when many features may work together to predict an outcome.
Fraud detection
Flag transactions with patterns similar to known suspicious activity.
Customer churn
Estimate whether customers may stop using a service.
Student performance
Explore how attendance, study time and other factors relate to results.
Price prediction
Estimate numeric values such as house prices using many property features.
8. Knowledge check
Answer each question, then submit to see your score and explanations.