General regression
Predicting continuous numeric targets from tabular features.
Scikit-Learn · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
n_estimators | int, default=100 | Number of trees in the forest. |
bootstrap | bool, default=True | Whether bootstrap samples are used when building trees. |
max_samples | int or float, default=None | Number or fraction of samples used to build each tree when bootstrap sampling is enabled. |
oob_score | bool or callable, default=False | Whether to use out-of-bag samples to estimate the generalization score; may be a callable metric. |
random_state | int, RandomState instance or None, default=None | Controls randomness in bootstrap sampling and feature sampling. |
A fitted RandomForestRegressor from fit; predict returns predicted target values for the supplied samples.
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.
Predicting continuous numeric targets from tabular features.
Estimating generalization while fitting with bootstrap sampling.
Training independent trees concurrently for a larger forest.
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.
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.
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.RandomForestClassifiersklearn.ensemble.ExtraTreesRegressorsklearn.tree.DecisionTreeRegressorsklearn.ensemble.GradientBoostingRegressor