Sub topic 9
Choosing the Number of Rounds with Early Stopping
This chapter assumes you can train an XGBClassifier, know what n_estimators and learning_rate do, and understand a train/validation split. Boosting keeps adding trees, and each tree lowers the training loss. Validation loss behaves differently. It falls, flattens, and then rises as the model starts to memorize noise. Early stopping finds that turning point automatically.
How it works
You set n_estimators high, as a ceiling and not as a target. After each round XGBoost scores the model on a validation set. If the metric has not improved for early_stopping_rounds consecutive rounds, training stops. The model then records best_iteration, the round with the best validation score.
Example with the scikit-learn API
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=0
)
model = XGBClassifier(
n_estimators=1000,
learning_rate=0.05,
max_depth=3,
subsample=0.8,
colsample_bytree=0.8,
eval_metric="logloss",
early_stopping_rounds=30,
)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)
print(model.best_iteration, model.score(X_val, y_val))In recent XGBoost versions, early_stopping_rounds is a constructor argument, not a fit argument. Older tutorials pass it to fit, and that code fails on current releases.
Choosing the settings
eval_metric: pick the metric you care about, such aslogloss,auc, oraucpr.early_stopping_rounds: this is the patience. Too small, and noise ends training early. Too large, and you waste time. With a low learning rate, 30 to 50 is a common choice.learning_rate: lower rates need more rounds but usually give a better optimum. Fix a modest rate such as 0.05, then let early stopping choose the round count.
Common errors
- Stopping on the test set. Early stopping uses the validation set to pick the round count, so that score is optimistically biased. Keep a separate test set for the final evaluation.
- Several metrics or eval sets. XGBoost checks only the last metric on the last entry in
eval_set. Order them on purpose. - Wrong direction. For metrics like
auc, higher is better. XGBoost knows this for its built-in metrics. Custom metrics needmaximizeset correctly. - Predicting with extra trees. The model keeps the trees built after the best round. Recent versions use the best iteration for prediction when early stopping was active, but check
best_iterationif you slice trees manually. - Reusing the number blindly. If you retrain on train plus validation data, scale the round count up a little, since there is more data.
Exercise
You set nestimators=500 and earlystopping_rounds=20. The best validation log loss occurs at round 140. At which round does training stop, and why is the validation score still biased?
Show answer
Training stops at round 160, after 20 rounds without improvement. The score is biased because the validation set chose the stopping round, so the model was tuned to it. Use a separate test set for an unbiased estimate.