random forest
regression model
ntree
mtry
machine learning

setting values for ntree and mtry for random forest regression model

Master System Design with Codemia

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

Introduction

Tuning ntree and mtry is the most common way to improve a random forest regressor without changing model family. Good defaults can work surprisingly well, but production datasets usually benefit from targeted tuning with reproducible evaluation. The right values are not universal, so the process matters more than any single number.

What ntree and mtry Control

ntree is the number of trees. Increasing it usually reduces prediction variance and stabilizes results, but training cost rises linearly. After a point, validation error plateaus and more trees only add compute time.

mtry is the number of candidate features tested at each split. Lower values increase tree diversity, which can improve generalization, but values that are too low can increase bias. Higher values can make individual trees stronger while also making trees more similar to each other.

A practical mental model:

  • 'ntree mostly controls stability.'
  • 'mtry mostly controls bias and diversity balance.'

For regression, common starting points are around p / 3 features per split, where p is feature count, then tune around that baseline.

Build a Reproducible Baseline First

Before tuning, lock down split logic and metric choice. Without this, differences across runs can look like improvements when they are only randomness.

python
1import numpy as np
2from sklearn.ensemble import RandomForestRegressor
3from sklearn.metrics import mean_squared_error
4from sklearn.model_selection import train_test_split
5
6rng = np.random.default_rng(42)
7X = rng.normal(size=(3000, 24))
8y = 4 * X[:, 0] - 1.8 * X[:, 5] + rng.normal(0, 0.7, size=3000)
9
10X_train, X_val, y_train, y_val = train_test_split(
11    X, y, test_size=0.2, random_state=42
12)
13
14baseline = RandomForestRegressor(
15    n_estimators=400,
16    max_features=8,
17    random_state=42,
18    n_jobs=-1,
19)
20baseline.fit(X_train, y_train)
21
22pred = baseline.predict(X_val)
23rmse = mean_squared_error(y_val, pred, squared=False)
24print("Baseline RMSE:", round(rmse, 4))

Use one baseline script like this as a fixed reference during tuning.

Tune mtry First, Then ntree

In many regression datasets, tuning feature subsampling has larger impact than adding hundreds of trees. A useful workflow is:

  1. Keep tree count moderate, for example 300.
  2. Search mtry values across a narrow, sensible range.
  3. Lock best mtry.
  4. Increase tree count until error improvement becomes negligible.
python
1from sklearn.model_selection import GridSearchCV
2
3model = RandomForestRegressor(
4    n_estimators=300,
5    random_state=42,
6    n_jobs=-1,
7)
8
9param_grid = {
10    "max_features": [4, 6, 8, 10, 12],
11    "min_samples_leaf": [1, 2, 4],
12}
13
14grid = GridSearchCV(
15    estimator=model,
16    param_grid=param_grid,
17    scoring="neg_root_mean_squared_error",
18    cv=5,
19    n_jobs=-1,
20)
21grid.fit(X_train, y_train)
22
23print("Best params:", grid.best_params_)
24print("Best CV RMSE:", round(-grid.best_score_, 4))

Then perform a small tree-count sweep:

python
1best_mtry = grid.best_params_["max_features"]
2best_leaf = grid.best_params_["min_samples_leaf"]
3
4for trees in [200, 400, 800, 1200]:
5    rf = RandomForestRegressor(
6        n_estimators=trees,
7        max_features=best_mtry,
8        min_samples_leaf=best_leaf,
9        random_state=42,
10        n_jobs=-1,
11    )
12    rf.fit(X_train, y_train)
13    score = mean_squared_error(y_val, rf.predict(X_val), squared=False)
14    print(trees, round(score, 4))

Pick the smallest tree count that achieves near-best validation error.

R Example with randomForest

If you are using R, the same principles apply directly because parameters are explicitly named ntree and mtry.

r
1library(randomForest)
2set.seed(42)
3
4fit <- randomForest(
5  y ~ ., 
6  data = train_df,
7  ntree = 500,
8  mtry = floor((ncol(train_df) - 1) / 3)
9)
10
11print(fit)

Use out-of-bag error for quick directional tuning, then confirm with validation or cross-validation.

Stop Criteria and Retraining Policy

You should revisit tuning when:

  • Feature set changes significantly.
  • Data volume grows a lot.
  • Label noise profile changes.
  • Latency or compute budget changes.

A previously tuned forest can become suboptimal after schema changes, even when code remains unchanged.

Common Pitfalls

  • Tuning only tree count and ignoring feature subsampling.
  • Selecting hyperparameters using the test set.
  • Comparing runs without fixed seed and consistent split.
  • Picking highest accuracy regardless of compute budget.
  • Using too wide a grid and spending compute on unrealistic settings.

Summary

  • 'ntree mainly improves prediction stability, while mtry changes bias-diversity behavior.'
  • Build a fixed baseline before any tuning.
  • Tune mtry first with moderate tree count, then tune ntree for plateau point.
  • Validate with CV or stable holdout metrics, not test-set optimization.
  • Choose the smallest configuration that meets both accuracy and runtime goals.

Course illustration
Course illustration

All Rights Reserved.