Part 9

Tune the forest for the decision you actually need to make.

A larger forest is not automatically a better forest. Tune tree count, tree size, feature randomness, sample size, and class handling with an evaluation design that matches deployment.

Hyperparameters as levers

First tuning question

Which parameter mainly creates more independent tree opinions, and which parameters control how complex each opinion can be?

ParameterControlsTypical effect
n_estimatorsNumber of treesMore stable aggregation; more compute and memory.
max_featuresFeatures considered per splitLower often reduces correlation but may weaken trees.
max_depthMaximum path lengthShallower trees usually reduce variance and complexity.
min_samples_leafMinimum rows in a leafLarger leaves smooth predictions and resist tiny pockets.
max_samplesRows drawn per bootstrapChanges tree diversity and training cost.
class_weightRelative class costsCan give rare classes more influence during splits.

What should we tune first?

Order question

Would you start with 200 combinations, or establish a simple baseline and inspect learning curves first?

  1. Fit one reproducible baseline with a held-out validation design.
  2. Check whether adding trees still improves validation performance.
  3. Try a small grid for max_features, depth, and leaf size.
  4. Use cross-validation or an appropriate grouped/time split.
  5. Keep the test set untouched until the final choice.
validation scoretraining costnumber of trees
validation performance cost / size

Metrics must match the business

Metric question

For a rare-fraud detector, can accuracy alone reveal whether the model is useful?

For classification, consider precision, recall, F1, ROC-AUC, PR-AUC, calibration, and the cost of false positives versus false negatives. For regression, consider MAE, RMSE, \(R^2\), subgroup error, and tail behavior.

For imbalanced classification, class_weight="balanced_subsample" adjusts weights from each bootstrap sample. It changes the split objective; it does not replace threshold selection or careful evaluation.

Previous: VarianceNext: Interpretation