Sub topic 2
Early Stopping and Overfitting in LightGBM and CatBoost
Assume Python, pandas, decision trees, train/validation splits, and metrics such as RMSE and AUC.
Early stopping controls model size
Gradient boosting adds trees one at a time. More trees can reduce training error but eventually make the model memorize noise. Early stopping monitors a validation metric and keeps the iteration with the best score.
Set a large maximum number of trees, then stop after the metric fails to improve for a patience period.
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(50)]
)
print(model.best_iteration_)Training LightGBM
LightGBM provides a scikit-learn API and a native API. The first fits naturally into scikit-learn pipelines. The second uses Dataset objects and a parameter dictionary.
train_set = lgb.Dataset(X_train, label=y_train)
valid_set = lgb.Dataset(X_valid, label=y_valid, reference=train_set)
booster = lgb.train(
{"objective": "regression", "metric": "rmse",
"learning_rate": 0.05, "num_leaves": 31},
train_set, num_boost_round=2000, valid_sets=[valid_set],
callbacks=[lgb.early_stopping(50)]
)LightGBM finds splits from feature histograms and grows the leaf with the greatest loss reduction. This is often fast, but leaf-wise growth can overfit.
Categorical columns in LightGBM
Convert categorical columns to pandas category values. LightGBM can then use categorical splitting without expanding every value into a separate one-hot column. Apply the same category definitions to training and validation data.
What CatBoost does differently
CatBoost is designed for categorical data. You pass categorical column names through a Pool, rather than manually encoding them.
train_pool = Pool(X_train, y_train, cat_features=["city", "plan"])
valid_pool = Pool(X_valid, y_valid, cat_features=["city", "plan"])
model = CatBoostClassifier(iterations=1000, eval_metric="AUC", verbose=100)
model.fit(train_pool, eval_set=valid_pool, early_stopping_rounds=50)Ordered methods prevent leakage
Naive target encoding uses the label while creating a feature, causing target leakage. CatBoost uses ordered target statistics: for each row, its category statistic is calculated from earlier rows in a random order, not from the row’s own label.
CatBoost also uses ordered boosting to reduce prediction shift, where training predictions differ from predictions made on unseen data. Its symmetric trees use the same split at each depth, which can improve speed and regularity.
Exercise
Why should a model use a large tree limit together with early stopping?
Show answer
The limit gives training room to find the best iteration, while early stopping prevents unnecessary trees from overfitting.