Validation-based early stopping
Quick answer
Early stopping limits boosting stages using a validation metric. Monitor a fixed validation set while training on separate rows, choose the iteration by a predefined rule, and reserve an untouched test set for final evaluation. Examples are static and locally tested with Python 3.13.5, scikit-learn 1.8.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Select a validation stage
losses=[5,4,4.2,3.8,3.9]best=min(range(len(losses)),key=losses.__getitem__)print(best+1,losses[best])Observed local output:
4 3.8Stage numbers start at one; the minimum is not necessarily the final stage.
How it works
A validation curve plots metric versus stage count for fixed other hyperparameters. For a loss such as MAE, smaller is better. A simple rule chooses the earliest stage with the smallest validation loss within a fixed budget. A patience-based rule stops after a specified number of consecutive non-improving steps; define minimum improvement and whether to restore the best stage. For example losses [5,4,4.2,3.8,3.9] have minimum at stage 4, whereas patience 1 would stop after stage 3 and choose stage 2. These rules differ. With scikit-learn classic GradientBoostingRegressor, staged_predict can inspect predictions after each fitted stage. Internal early stopping through n_iter_no_change uses validation_fraction of the fit data; do not confuse it with externally held-out evaluation. Histogram boosting has its own early_stopping and n_iter_no_change parameters and can accept explicit validation arrays in scikit-learn 1.8. For correlated or time-ordered observations, use appropriate group or chronological splits rather than assuming a random internal split is valid. The chapter project trains a bounded classic model with internal early stopping disabled, computes all validation stages, refits a fresh model on the original training rows with the selected count, and then evaluates that model once on test data. That is retrospective stage selection, not compute-saving online stopping. A random forest and a training-mean baseline supply comparisons. A validation curve is not a test curve; repeated tuning can itself overfit validation. Refit on train+validation only under a predeclared protocol; this project intentionally does not do so.
Check an explicit held-out stage curve
from sklearn.ensemble import GradientBoostingRegressorfrom sklearn.metrics import mean_absolute_errorX=[[0],[1],[2],[3]]; y=[0,0,4,4]model=GradientBoostingRegressor(n_estimators=3,max_depth=1,learning_rate=0.5,random_state=7).fit(X,y)valid_X=[[0.25],[2.75]]; valid_y=[0,4]print([mean_absolute_error(valid_y,p) for p in model.staged_predict(valid_X)])Observed local output:
[1.0, 0.5, 0.25]The held-out rows are distinct from training rows; this toy curve improves at every stage, unlike some noisy datasets.
Common mistakes
Do not use the test set for stage count or patience. Do not report the best stage if you actually deploy the last stage. Do not assume validation_fraction produces a suitable temporal or grouped split.
Practice and next steps
Use the chapter milestone to choose a stage count on validation MAE, compare the predeclared forest baseline on the same split, and report test results only after freezing the choice. Complete the knowledge check and staged exercise. Next: XGBoost objectives and tree regularization.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Validation-based early stopping — knowledge check
Apply validation-based early stopping to new examples.
Validation losses [7,4,4,6], earliest minimum rule. Which stage?
Which data may choose the number of boosting stages?
Why distinguish best stage from last trained stage?
Does retrospective staged_predict selection save the training cost of later stages?
For time-ordered data, which split is appropriate for future prediction?