Module 2: Python Implementation

From the Mastering Random Forests & Ensemble Learning curriculum ยท Updated Jan 11, 2026

Scikit-Learn Code Example

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Initialize the model
# n_estimators = number of trees
clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0)

# Fit to training data
clf.fit(X_train, y_train)

# Predict class labels
y_pred = clf.predict(X_test)

Exam Tip: Always check your feature_importances_ attribute to understand which variables are driving your model's decisions. This is crucial for model explainability.


Get the full Mastering Random Forests & Ensemble Learning curriculum

Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.

Create Free Account