Sub topic 8
Controlling overfitting
This chapter assumes you can train an XGBClassifier, read a train/validation split, and know what log loss measures. Boosting keeps fitting residuals, so given enough rounds it can memorize noise. Training loss keeps falling while validation loss turns upward. The tools below limit how much each tree can fit.
Recognizing overfitting
Pass both the training and validation sets as eval_set, then compare their curves. A growing gap between them signals overfitting. Training loss near zero with a much higher validation loss is the typical case.
Three families of controls
| Family | Parameters | Effect |
|---|---|---|
| Tree complexity | `max_depth`, `min_child_weight`, `gamma` | Limits how finely a tree can split the data |
| Randomness | `subsample`, `colsample_bytree` | Each tree sees only part of the rows or columns |
| Penalties and shrinkage | `reg_lambda`, `reg_alpha`, `learning_rate` | Shrinks leaf values and each tree's contribution |
max_depthcaps tree depth. Values from 3 to 6 are a common starting range.min_child_weightis the minimum sum of hessians in a leaf. Larger values block splits on tiny groups of rows.gamma(min_split_loss) is the smallest loss reduction a split must achieve. Raising it prunes weak splits.reg_lambdais the L2 penalty on leaf values, andreg_alphais the L1 penalty.- A lower
learning_ratemakes each tree matter less. It needs more rounds, but it usually generalizes better.
Comparing a loose and a regularized model
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
)
configs = {
"loose": dict(max_depth=8, min_child_weight=1, subsample=1.0,
colsample_bytree=1.0, reg_lambda=0, gamma=0),
"regularized": dict(max_depth=3, min_child_weight=5, subsample=0.8,
colsample_bytree=0.8, reg_lambda=5, gamma=1),
}
for name, params in configs.items():
model = XGBClassifier(
n_estimators=300, learning_rate=0.1, eval_metric="logloss", **params
)
model.fit(
X_train, y_train,
eval_set=[(X_train, y_train), (X_val, y_val)],
verbose=False,
)
history = model.evals_result()
train_loss = history["validation_0"]["logloss"][-1]
val_loss = history["validation_1"]["logloss"][-1]
print(name, round(train_loss, 4), round(val_loss, 4))validation_0 is the first set in eval_set (training), and validation_1 is the second. The loose model usually reaches a lower training loss. Check whether its validation loss is better or worse than the regularized model's.
Order of adjustment
- Fix a moderate
learning_ratesuch as 0.05 to 0.1. - Reduce
max_depthand raisemin_child_weightif the gap is large. - Add
subsampleandcolsample_bytreebetween 0.6 and 0.9. - Increase
reg_lambdaorgammafor finer control. - Use early stopping to choose the number of rounds (next chapter).
Exercise
A model has a training log loss of 0.02 and a validation log loss of 0.35 after 500 rounds. Name two parameter changes that should narrow the gap, and say which direction each moves.
Show answer
Lower max_depth (for example from 8 to 4) and set subsample below 1.0 (for example 0.8). Raising min_child_weight, gamma or reg_lambda, or lowering learning_rate, would also help. Each change limits how closely the trees can fit the training data.