Classify structured feature data
General tabular classification
Scikit-Learn · Class
A classifier that combines many decision trees trained on dataset subsamples. Averaging their outputs improves predictive accuracy and helps control overfitting.
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.
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)| Name | Type | Description |
|---|---|---|
n_estimators | int | Number of trees in the forest; default 100. |
criterion | {"gini", "entropy", "log_loss"} | Split-quality function: gini, entropy, or log_loss; default gini. |
max_depth | int or None | Maximum depth of each tree; default None. |
min_samples_split | int or float | Minimum number of samples required to split an internal node; default 2. |
min_samples_leaf | int or float | Minimum number of samples required at a leaf; default 1. |
max_features | {"sqrt", "log2", int, float} or None | Maximum number of features considered for each split; default sqrt. |
bootstrap | bool | Whether bootstrap samples are used to build trees; default True. |
oob_score | bool | Whether to use out-of-bag samples to estimate generalization; default False. |
random_state | int, RandomState instance or None | Controls randomness; default None. |
max_samples | int or float or None | Number of samples drawn for each tree when bootstrap sampling is enabled; default None. |
An instance of RandomForestClassifier; fitted methods return the estimator, while prediction methods return class labels or class probabilities as documented.
n_estimators specifies how many decision trees the forest contains.criterion chooses the function used to evaluate candidate tree splits.fit(X, y) trains the forest from feature matrix X and target vector y.max_samples controls the per-tree sample count only when bootstrap sampling is enabled; disabling bootstrap makes every tree use the full dataset.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.
General tabular classification
Model robustness
Class-imbalanced learning
Parallel model fitting
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.
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.
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.
RandomForestRegressorExtraTreesClassifierDecisionTreeClassifierBaggingClassifierHistGradientBoostingClassifier