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

Scikit-Learn · Class

RandomForestRegressor

RandomForestRegressor is an ensemble of decision-tree regressors that averages predictions from trees trained on dataset subsamples. It improves predictive accuracy and helps control over-fitting.

How it works

RandomForestRegressor combines multiple decision-tree regressors trained on dataset subsamples. It predicts by averaging their outputs, using feature and sample randomness to improve accuracy and reduce over-fitting; bootstrap sampling controls the data subset used by each tree.

Syntax

RandomForestRegressor(n_estimators=100, *, criterion='squared_error', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=1.0, 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, ccp_alpha=0.0, max_samples=None, monotonic_cst=None)

Parameters

NameTypeDescription
n_estimatorsint, default=100Number of trees in the forest.
bootstrapbool, default=TrueWhether bootstrap samples are used when building trees.
max_samplesint or float, default=NoneNumber or fraction of samples used to build each tree when bootstrap sampling is enabled.
oob_scorebool or callable, default=FalseWhether to use out-of-bag samples to estimate the generalization score; may be a callable metric.
random_stateint, RandomState instance or None, default=NoneControls randomness in bootstrap sampling and feature sampling.

Returns

A fitted RandomForestRegressor from fit; predict returns predicted target values for the supplied samples.

Key points

  • The class builds an ensemble of decision-tree regressors from different dataset subsamples.
  • Predictions are aggregated by averaging, which targets better accuracy and less over-fitting.
  • Each underlying tree uses the best split strategy rather than a random split strategy.
  • The estimator can natively process missing numeric values represented as NaNs.
  • Several tree-wise operations can run in parallel across the forest.
  • oob_score provides an optional generalization estimate from out-of-bag samples.

Common mistakes

  • max_samples affects the training subset only when bootstrap sampling is enabled; otherwise every tree uses the whole dataset.
  • Enabling out-of-bag scoring uses R² unless a custom metric callable is supplied.
  • Set random_state when reproducible bootstrap and feature sampling are required.

Example: Train and predict with a random forest regressor

from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor

X, y = make_regression(n_samples=100, n_features=4, random_state=0)
model = RandomForestRegressor(n_estimators=100, random_state=0)
model.fit(X, y)
predictions = model.predict(X[:3])
print(predictions)

Creates synthetic regression data, trains a reproducible forest, and predicts three samples.

When to use it

General regression

Predicting continuous numeric targets from tabular features.

Out-of-bag evaluation

Estimating generalization while fitting with bootstrap sampling.

Parallel ensemble fitting

Training independent trees concurrently for a larger forest.

Interview questions

QHow does RandomForestRegressor produce predictions?

It is an ensemble meta-estimator made from decision-tree regressors. It fits trees on various dataset subsamples and averages their predictions to improve predictive accuracy and control over-fitting.

Follow-up: How does bootstrap sampling change training? With bootstrap=True, each tree uses a bootstrap sample, and max_samples can control its size; with bootstrap=False, each tree uses the entire dataset.

QWhere does randomness enter the model?

The estimator uses the best split strategy of the underlying DecisionTreeRegressor, equivalent to splitter="best". Feature sampling can still introduce randomness when max_features is smaller than the number of features.

Follow-up: What does random_state control? It controls bootstrap randomness and feature sampling randomness when max_features is below the total feature count.

QWhat is the purpose of oob_score?

oob_score enables an out-of-bag estimate of generalization using samples not selected for a tree's bootstrap sample. The default scoring function is r2_score, but a callable metric(y_true, y_pred) can be provided.

Follow-up: When is out-of-bag scoring unavailable as intended? If bootstrap is disabled, there are no out-of-bag samples produced by the tree-building process.

  • sklearn.ensemble.RandomForestClassifier
  • sklearn.ensemble.ExtraTreesRegressor
  • sklearn.tree.DecisionTreeRegressor
  • sklearn.ensemble.GradientBoostingRegressor

Source: RandomForestRegressor — scikit-learn 1.9.1 documentation. Checked 2026-10-15.