Random Forest Cheat Sheet
A cheat sheet for Random Forest covering bagging, feature randomness, out-of-bag scoring, hyperparameter tuning, and feature importance in scikit-learn.
2 PagesIntermediateMar 2, 2026
Classifier with scikit-learn
Fit a forest and check its out-of-bag score.
python
from sklearn.ensemble import RandomForestClassifierrf = RandomForestClassifier( n_estimators=300, max_depth=None, max_features='sqrt', min_samples_leaf=2, oob_score=True, n_jobs=-1, random_state=42)rf.fit(X_train, y_train)print('OOB score:', rf.oob_score_) # validation-free accuracy estimateprint('Test accuracy:', rf.score(X_test, y_test))
Regressor & Feature Importance
Random Forest for regression tasks.
python
from sklearn.ensemble import RandomForestRegressorreg = RandomForestRegressor(n_estimators=200, n_jobs=-1, random_state=42)reg.fit(X_train, y_train)importances = reg.feature_importances_ # mean decrease in impurity per feature
Hyperparameter Search
Tune key forest hyperparameters.
python
from sklearn.model_selection import RandomizedSearchCVparam_dist = { 'n_estimators': [100, 300, 500], 'max_depth': [None, 10, 20, 30], 'min_samples_leaf': [1, 2, 4],}search = RandomizedSearchCV(RandomForestClassifier(), param_dist, cv=5, n_iter=20, n_jobs=-1)search.fit(X_train, y_train)print(search.best_params_)
Key Concepts
Core theory behind Random Forest.
- Bagging- Each tree trains on a bootstrap sample (random sample with replacement) of the training data
- Feature randomness- Each split considers only a random subset of features (max_features), decorrelating the trees
- Out-of-bag (OOB) score- Free validation estimate using the roughly 37% of samples excluded from each tree's bootstrap sample
- n_estimators- Number of trees in the forest; more trees reduce variance with diminishing returns on compute
- Feature importance- Mean decrease in impurity across all trees, or more robust permutation importance
Pro Tip
Prefer permutation_importance from sklearn.inspection over the default feature_importances_ when features vary in cardinality or scale — impurity-based importance is biased toward high-cardinality and continuous features, even when they're not truly predictive.
Was this cheat sheet helpful?
Explore Topics
#RandomForest#RandomForestCheatSheet#DataScience#Intermediate#ClassifierWithScikitLearn#RegressorFeatureImportance#HyperparameterSearch#KeyConcepts#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance