Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

RandomForestClassifier

A classifier that combines many decision trees trained on dataset subsamples. Averaging their outputs improves predictive accuracy and helps control overfitting.

How it works

The estimator builds multiple decision-tree classifiers from dataset subsamples. It combines their predictions through averaging, while bootstrap sampling and tree-size controls influence diversity, fitting cost, and overfitting.

Syntax

RandomForestClassifier(n_estimators=100, *, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='sqrt', max_leaf_nodes=None, min_impurity_decrease=0.0, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight=None, ccp_alpha=0.0, max_samples=None, monotonic_cst=None)

Parameters

NameTypeDescription
n_estimatorsintNumber of trees in the forest; default 100.
criterion{"gini", "entropy", "log_loss"}Split-quality function: gini, entropy, or log_loss; default gini.
max_depthint or NoneMaximum depth of each tree; default None.
min_samples_splitint or floatMinimum number of samples required to split an internal node; default 2.
min_samples_leafint or floatMinimum number of samples required at a leaf; default 1.
max_features{"sqrt", "log2", int, float} or NoneMaximum number of features considered for each split; default sqrt.
bootstrapboolWhether bootstrap samples are used to build trees; default True.
oob_scoreboolWhether to use out-of-bag samples to estimate generalization; default False.
random_stateint, RandomState instance or NoneControls randomness; default None.
max_samplesint or float or NoneNumber of samples drawn for each tree when bootstrap sampling is enabled; default None.

Returns

An instance of RandomForestClassifier; fitted methods return the estimator, while prediction methods return class labels or class probabilities as documented.

Key points

  • The class ensembles multiple decision-tree classifiers trained on different subsets of the training data.
  • Combining tree outputs by averaging is intended to improve prediction quality and reduce overfitting.
  • n_estimators specifies how many decision trees the forest contains.
  • criterion chooses the function used to evaluate candidate tree splits.
  • The documented split criteria are Gini impurity plus entropy or log-loss information gain.
  • Calling fit(X, y) trains the forest from feature matrix X and target vector y.

Common mistakes

  • max_samples controls the per-tree sample count only when bootstrap sampling is enabled; disabling bootstrap makes every tree use the full dataset.
  • The forest's trees use the underlying decision tree's best split strategy rather than a random split strategy.

Example: Train and predict with a random forest

from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier

X, y = make_classification(
    n_samples=1000, n_features=4, n_informative=2,
    n_redundant=0, random_state=0, shuffle=False
)
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)
print(clf.predict([[0, 0, 0, 0]]))

Creates a synthetic classification dataset, trains a bounded-depth forest, and predicts one sample.

When to use it

Classify structured feature data

General tabular classification

Reduce variance with an ensemble

Model robustness

Apply class weighting through `class_weight`

Class-imbalanced learning

Use multiple workers with `n_jobs`

Parallel model fitting

Interview questions

QWhat is the basic idea behind RandomForestClassifier?

It is a meta-estimator that fits many decision-tree classifiers on different dataset subsamples. Averaging their predictions improves predictive accuracy and helps control overfitting.

Follow-up: How does bootstrap affect training data? With bootstrap=True, each tree receives a bootstrap sample whose size can be set with max_samples; otherwise, each tree uses the whole dataset.

QWhich parameter controls how many trees are trained?

n_estimators sets the number of trees in the forest. Increasing it generally gives the ensemble more trees to average, while also increasing training and prediction work.

Follow-up: Which parameter limits individual tree complexity? max_depth limits tree depth; related controls include min_samples_split, min_samples_leaf, and max_leaf_nodes.

QWhich split criteria are available?

The constructor supports gini, entropy, and log_loss as split-quality criteria. gini is the default, while entropy and log_loss use Shannon information gain.

Follow-up: What does bootstrap=False change? Trees are built from the complete dataset rather than bootstrap subsamples, and max_samples no longer controls their sample size.

  • RandomForestRegressor
  • ExtraTreesClassifier
  • DecisionTreeClassifier
  • BaggingClassifier
  • HistGradientBoostingClassifier

Source: RandomForestClassifier. Checked 2026-10-15.