RandomForestRegressor
max_features
machine learning
regression analysis
scikit-learn

Understanding max_features parameter in RandomForestRegressor

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In RandomForestRegressor, max_features controls how many candidate features each split is allowed to consider when a tree is choosing its best split. It is one of the main parameters that governs the tradeoff between tree strength and tree diversity.

What It Affects

A random forest does not just build many trees. It also injects randomness so the trees do not all make the same decisions. max_features is one of the main sources of that randomness.

At each split in each tree:

  • the algorithm samples a subset of features
  • it searches only within that subset for the best split

So max_features is not “how many features the whole model uses.” It is “how many features are eligible for consideration at one split point.”

Why This Helps a Forest

If every tree examined all features at every split, many trees would become highly similar, especially when a few strong predictors dominate. Limiting the candidate feature set introduces diversity across trees.

That matters because a random forest gains strength from averaging many trees that are individually useful but not perfectly correlated.

So the tradeoff is:

  • larger max_features: stronger individual trees, but more correlation between trees
  • smaller max_features: weaker individual trees, but more diversity across trees

A good setting balances both.

Allowed Value Types

max_features can usually be specified as:

  • an integer: exact number of features per split
  • a float: fraction of total features per split
  • '"sqrt": square root of the total number of features'
  • '"log2": log base 2 of the total number of features'
  • 'None or all features: no feature subsampling at split time'

Example:

python
1from sklearn.ensemble import RandomForestRegressor
2
3model = RandomForestRegressor(
4    n_estimators=200,
5    max_features="sqrt",
6    random_state=42
7)

That setting means each split only considers roughly the square root of the available feature count.

Example with Numeric Values

Suppose your dataset has 100 input features.

  • 'max_features=10 means each split considers 10 random features'
  • 'max_features=0.2 means each split considers 20 features'
  • 'max_features="sqrt" means each split considers about 10 features'
  • 'max_features=None means all 100 features are considered at every split'

Those settings can produce noticeably different forest behavior even with the same training data and same number of trees.

Regression Intuition

For regression, using all features can make trees individually stronger, but also more similar. A smaller max_features often improves generalization because it reduces correlation across trees, though if it is too small, each tree may become too weak to learn good splits.

That is why there is no universal best value. It depends on:

  • the number of features
  • feature redundancy
  • noise level
  • dataset size
  • how dominant a few predictors are

A Small Experiment

python
1from sklearn.datasets import make_regression
2from sklearn.ensemble import RandomForestRegressor
3from sklearn.model_selection import cross_val_score
4
5X, y = make_regression(n_samples=1000, n_features=40, noise=10, random_state=42)
6
7for setting in [None, "sqrt", "log2", 0.3, 5]:
8    model = RandomForestRegressor(
9        n_estimators=200,
10        max_features=setting,
11        random_state=42,
12        n_jobs=-1
13    )
14    score = cross_val_score(model, X, y, cv=5, scoring="r2").mean()
15    print(setting, score)

This is the right way to tune max_features: try plausible values and measure validation performance instead of relying on folklore.

What It Does Not Mean

max_features does not mean:

  • the forest permanently discards the other features
  • each tree receives only that many columns for its whole lifetime
  • the model output uses only those features globally

The feature subset is sampled again at each split. Over the entire forest, many different features may still be used extensively.

Common Pitfalls

  • Thinking max_features controls how many features the full model uses instead of how many are considered at each split.
  • Setting it to all features by default and then wondering why the trees are highly correlated and the forest gains less from ensembling.
  • Setting it too small on a difficult regression problem and weakening every tree too much.
  • Tuning max_features in isolation without checking interactions with n_estimators, depth, and sample size.
  • Copying a classification heuristic blindly without validating whether it works well for the regression dataset at hand.

Summary

  • 'max_features controls the number of candidate features considered at each split in each tree.'
  • Smaller values increase randomness and diversity; larger values strengthen individual trees but increase correlation.
  • The parameter can be an integer, a fraction, or a named rule such as "sqrt".
  • It does not permanently limit the full forest to a fixed subset of columns.
  • The best value is dataset-dependent and should be tuned with validation rather than assumed.

Course illustration
Course illustration

All Rights Reserved.