OOB Error Curve with a Warm-Started Random Forest

Quick Overview

Grow a scikit-learn random forest with warm_start in fixed steps, record the out-of-bag error at each size, and find the smallest tree count after which the error stays within a tolerance of the final value. Tests OOB scoring, warm-start reproducibility, input validation and suffix checks with NumPy.

OOB Error Curve with a Warm-Started Random Forest

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `oob_error_curve(X, y, step, max_trees, tol, random_state=0, max_features="sqrt")`, which grows one `RandomForestClassifier` with `warm_start=True` to `step, 2*step, ..., max_trees` trees and records the OOB error (`1 - oob_score_`) at each size. Also implement `stable_n_estimators(n_grid, errors, tol)`. `oob_error_curve` returns `(n_grid, errors, stable_n, forest)`. Requirements: - The forest uses `oob_score=True`, `bootstrap=True`, and the given `max_features` and `random_state`. The errors must match those of freshly fitted forests of each size. - `stable_n_estimators` returns, as an `int`, the smallest grid value from which every error (that point included) satisfies `|e - errors[-1]| <= tol`. - The returned `forest` is the warm-started model holding `max_trees` estimators. - A non-positive `step` or `max_trees`, or a `max_trees` that is not a multiple of `step`, raises `ValueError`. - `stable_n_estimators` raises `ValueError` for empty, non-1-D or mismatched inputs. Example: ```python grid = [10, 20, 30, 40, 50] errors = [0.30, 0.22, 0.18, 0.21, 0.20] stable_n_estimators(grid, errors, 0.015) # 40 stable_n_estimators(grid, errors, 0.025) # 20 stable_n_estimators([5, 10, 15, 20], [0.2, 0.5, 0.2, 0.2], 0.05) # 15 ``` With `tol=0.015` only the errors at 40 and 50 trees are within 0.015 of the final 0.20. In the last call the 5-tree error equals the final error, but the 0.5 at 10 trees comes after it, so the curve is stable only from 15 trees. ```hint Look at the whole tail A point close to the final error is not enough on its own. Think about how to check, for every index at once, whether all the points from there to the end pass. ``` ### What a Strong Answer Covers - One warm-started forest grown to each grid size with the required parameters, and the reason its OOB errors match fresh fits that use the same integer `random_state`. - Input validation for `step` and `max_trees` and for the arrays passed to `stable_n_estimators`. - A stability test over the whole suffix against the final error that reproduces all three examples. - How the earliest OOB points of the curve behave. - The cost of training the trees plus recomputing the OOB score at every grid point. ### Follow-up Questions - Why does the warm-started forest give the same OOB errors as fresh forests of each size, and when would it not? - Why are the OOB errors at the smallest forest sizes unreliable, and how would you treat them? - How would you choose `tol`, given that the OOB error is itself a noisy estimate?

Overview: Grow a scikit-learn random forest with warm_start in fixed steps, record the out-of-bag error at each size, and find the smallest tree count after which the error stays within a tolerance of the final value. Tests OOB scoring, warm-start reproducibility, input validation and suffix checks with NumPy.

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

Implement oob_error_curve(X, y, step, max_trees, tol, random_state=0, max_features="sqrt"), which grows one RandomForestClassifier with warm_start=True to step, 2*step, ..., max_trees trees and records the OOB error (1 - oob_score_) at each size. Also implement stable_n_estimators(n_grid, errors, tol). oob_error_curve returns (n_grid, errors, stable_n, forest).

Requirements:

  • The forest uses oob_score=True , bootstrap=True , and the given max_features and random_state . The errors must match those of freshly fitted forests of each size.
  • stable_n_estimators returns, as an int , the smallest grid value from which every error (that point included) satisfies |e - errors[-1]| <= tol .
  • The returned forest is the warm-started model holding max_trees estimators.
  • A non-positive step or max_trees , or a max_trees that is not a multiple of step , raises ValueError .
  • stable_n_estimators raises ValueError for empty, non-1-D or mismatched inputs.

Example:

grid   = [10, 20, 30, 40, 50]
errors = [0.30, 0.22, 0.18, 0.21, 0.20]
stable_n_estimators(grid, errors, 0.015)  # 40
stable_n_estimators(grid, errors, 0.025)  # 20
stable_n_estimators([5, 10, 15, 20], [0.2, 0.5, 0.2, 0.2], 0.05)  # 15

With tol=0.015 only the errors at 40 and 50 trees are within 0.015 of the final 0.20. In the last call the 5-tree error equals the final error, but the 0.5 at 10 trees comes after it, so the curve is stable only from 15 trees.

What a Strong Answer Covers Guidance

  • One warm-started forest grown to each grid size with the required parameters, and the reason its OOB errors match fresh fits that use the same integer random_state .
  • Input validation for step and max_trees and for the arrays passed to stable_n_estimators .
  • A stability test over the whole suffix against the final error that reproduces all three examples.
  • How the earliest OOB points of the curve behave.
  • The cost of training the trees plus recomputing the OOB score at every grid point.

Follow-up Questions Guidance

  • Why does the warm-started forest give the same OOB errors as fresh forests of each size, and when would it not?
  • Why are the OOB errors at the smallest forest sizes unreliable, and how would you treat them?
  • How would you choose tol , given that the OOB error is itself a noisy estimate?