Tune Random Forest Hyperparameters by OOB Score

Quick Overview

Implement a grid search over max_features and min_samples_leaf for a scikit-learn random forest classifier, scoring each setting by out-of-bag accuracy with no cross-validation. It checks how OOB estimates work and how to return the first best setting in row-major order when scores tie.

Tune Random Forest Hyperparameters by OOB Score

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `select_forest_params_by_oob(X, y, max_features_options, min_samples_leaf_options, n_estimators=50, random_state=0)`. For every pair in the grid, fit a `RandomForestClassifier` and score it by out-of-bag accuracy (no cross-validation). Return the best setting and the full score table. The function must meet these requirements: - Fit each forest with `n_estimators`, `bootstrap=True`, `oob_score=True` and the given `random_state`, and score it with `oob_score_`. - Return a dict with keys `"max_features"`, `"min_samples_leaf"`, `"oob_score"` (float) and `"scores"`. - `scores` is a float array of shape `(len(max_features_options), len(min_samples_leaf_options))`. - Ties go to the first setting in row-major order (`max_features` is the outer loop). - Return option values exactly as given (for example `"sqrt"`, `None`, ints). Example: ```python # X, y: at least 2 features, with the two classes far apart on every feature. # Assume every forest in this grid gets an OOB score of 1.0 on this data. res = select_forest_params_by_oob(X, y, [2, 1], [2, 1]) res["scores"] # array([[1., 1.], [1., 1.]]) res["max_features"] # 2 (first in row-major order) res["min_samples_leaf"] # 2 res["oob_score"] # 1.0 ``` ```hint Breaking ties Fill the table in the same order the tie rule uses, then check how your selection function treats repeated maxima in a flattened array. ``` ```hint Keeping option values intact Watch what happens to a list such as `[2, "sqrt"]` if you convert it to a NumPy array before reading the winning value back. ``` ### What a Strong Answer Covers - Fitting each forest with bootstrap sampling and OOB scoring switched on, using the given `n_estimators` and `random_state`, and reading `oob_score_`. - A score table whose shape and orientation match the loop order. - A selection step whose tie behavior matches the row-major rule. - Returning the original option objects and a Python float for the best score. - Why OOB accuracy works as a validation estimate and what it saves compared with k-fold cross-validation. ### Follow-up Questions - Each row's OOB prediction comes from only part of the forest. How does that bias the estimate when `n_estimators` is small, and what happens as the forest grows? - After picking the best of many grid cells by OOB accuracy, is the winning `oob_score` a fair estimate of test accuracy? What would you do about it? - When is OOB accuracy a poor guide for model selection, for example with grouped or time-ordered rows? - How would you adapt the function for a regression target?

Overview: Implement a grid search over max_features and min_samples_leaf for a scikit-learn random forest classifier, scoring each setting by out-of-bag accuracy with no cross-validation. It checks how OOB estimates work and how to return the first best setting in row-major order when scores tie.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement select_forest_params_by_oob(X, y, max_features_options, min_samples_leaf_options, n_estimators=50, random_state=0). For every pair in the grid, fit a RandomForestClassifier and score it by out-of-bag accuracy (no cross-validation). Return the best setting and the full score table.

The function must meet these requirements:

  • Fit each forest with n_estimators , bootstrap=True , oob_score=True and the given random_state , and score it with oob_score_ .
  • Return a dict with keys "max_features" , "min_samples_leaf" , "oob_score" (float) and "scores" .
  • scores is a float array of shape (len(max_features_options), len(min_samples_leaf_options)) .
  • Ties go to the first setting in row-major order ( max_features is the outer loop).
  • Return option values exactly as given (for example "sqrt" , None , ints).

Example:

# X, y: at least 2 features, with the two classes far apart on every feature.
# Assume every forest in this grid gets an OOB score of 1.0 on this data.
res = select_forest_params_by_oob(X, y, [2, 1], [2, 1])
res["scores"]           # array([[1., 1.], [1., 1.]])
res["max_features"]     # 2  (first in row-major order)
res["min_samples_leaf"] # 2
res["oob_score"]        # 1.0

What a Strong Answer Covers Guidance

  • Fitting each forest with bootstrap sampling and OOB scoring switched on, using the given n_estimators and random_state , and reading oob_score_ .
  • A score table whose shape and orientation match the loop order.
  • A selection step whose tie behavior matches the row-major rule.
  • Returning the original option objects and a Python float for the best score.
  • Why OOB accuracy works as a validation estimate and what it saves compared with k-fold cross-validation.

Follow-up Questions Guidance

  • Each row's OOB prediction comes from only part of the forest. How does that bias the estimate when n_estimators is small, and what happens as the forest grows?
  • After picking the best of many grid cells by OOB accuracy, is the winning oob_score a fair estimate of test accuracy? What would you do about it?
  • When is OOB accuracy a poor guide for model selection, for example with grouped or time-ordered rows?
  • How would you adapt the function for a regression target?