Random Forests
Explore Random Forests, an ensemble method for improved predictions.
60 min•By Priygop Team•Last updated: Feb 2026
What are Random Forests?
Random Forests are an ensemble learning method that operates by constructing multiple decision trees and outputting the class that is the mode of the classes predicted by individual trees.
Key Concepts
- Ensemble Method: Combines multiple models
- Bootstrap Sampling: Random sampling with replacement
- Feature Randomness: Random subset of features
- Voting/Averaging: Final prediction aggregation
Implementation
Example
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
# Generate sample data
np.random.seed(42)
X = np.random.randn(100, 4)
y = (X[:, 0] + X[:, 1] + X[:, 2] > 0).astype(int)
# Split the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create and train the model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.4f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# Feature importance
feature_importance = model.feature_importances_
print("\nFeature importance:")
for i, importance in enumerate(feature_importance):
print(f"Feature {i}: {importance:.4f}")Try It Yourself — Supervised Learning
Try It Yourself — Supervised LearningHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|32 lines|1574 chars|✓ Valid syntax
UTF-8