Sub topic 7
Early stopping and controlling overfitting
This chapter assumes you know what a validation split is, how to read RMSE, log loss and AUC, and the basic idea of gradient boosting: each new tree corrects the errors of the trees before it.
Why boosting overfits
Every round adds a tree that fits the remaining errors. Training loss keeps falling. Validation loss falls at first, then flattens and rises once the trees start fitting noise. The number of rounds is therefore a regularization knob.
Early stopping in practice
Set a large round limit, give the library a validation set, and let it stop. The value 50 below is the patience: how many rounds without improvement to tolerate.
import numpy as np
import lightgbm as lgb
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
X_rest, X_test, y_rest, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
X_train, X_valid, y_train, y_valid = train_test_split(
X_rest, y_rest, test_size=0.25, random_state=0
)
model = lgb.LGBMRegressor(n_estimators=2000, learning_rate=0.05, num_leaves=31)
model.fit(
X_train,
y_train,
eval_set=[(X_valid, y_valid)],
eval_metric="rmse",
callbacks=[lgb.early_stopping(stopping_rounds=50), lgb.log_evaluation(0)],
)
pred = model.predict(X_test)
print("best iteration:", model.best_iteration_)
print("test RMSE:", np.sqrt(mean_squared_error(y_test, pred)))After early stopping, predict uses the best iteration, not the last one. In CatBoost, pass early_stopping_rounds to fit along with an eval_set. It keeps the best model by default (use_best_model=True).
Other controls
Early stopping fixes the number of trees. These parameters limit how much each tree can fit.
| Control | LightGBM | CatBoost | Effect |
|---|---|---|---|
| Tree size | `num_leaves` | `depth` | Smaller means a simpler model |
| Leaf size | `min_data_in_leaf` | `min_data_in_leaf` | Larger blocks leaves built on few rows |
| Step size | `learning_rate` | `learning_rate` | Smaller needs more rounds and often generalizes better |
| Leaf weight penalty | `lambda_l2` | `l2_leaf_reg` | Shrinks leaf values |
| Subsampling | `bagging_fraction`, `feature_fraction` | `subsample`, `rsm` | Adds randomness between trees |
A reliable recipe is to fix a small learning rate such as 0.05, set a large round limit, and let early stopping choose the tree count. Then tune the complexity parameters.
Common errors
- Early stopping on the training set. The metric never worsens, so training runs to the round limit.
- Using a validation set that leaks, for example random splits on time-ordered or grouped data.
- Tuning the learning rate without raising the round limit. Training then ends at the limit and
best_iteration_equalsn_estimators. - Too small a patience on a noisy metric, which stops training too early.
Exercise
You train with n_estimators=300 and learning_rate=0.01, and best_iteration_ comes out as 300. What does this suggest, and what should you change?
Show answer
The validation metric was still improving when training hit the limit, so early stopping never triggered. Raise n_estimators (or the learning rate) so the model can reach its best round.