Sub topic 7
The hyperparameters that matter
This chapter assumes you can train an XGBClassifier with the scikit-learn API, as in the previous chapter. It also assumes you know what a train/validation split is and have an intuitive grasp of overfitting. XGBoost has dozens of parameters, but a handful do most of the work.
Two groups of parameters
Parameters fall into two groups. Some set how many trees you add and how much each one contributes. The others set how complex each tree can be. Tuning is mostly about balancing the two groups.
| Parameter | Group | Effect | Typical range |
|---|---|---|---|
| `n_estimators` | ensemble | Maximum number of trees (boosting rounds) | 100 to 2000, with early stopping |
| `learning_rate` (`eta`) | ensemble | Scales each tree's contribution | 0.01 to 0.3 |
| `max_depth` | tree | Maximum depth of each tree | 3 to 8 |
| `min_child_weight` | tree | Minimum hessian sum a leaf needs | 1 to 10 |
| `subsample` | sampling | Fraction of rows used per tree | 0.5 to 1.0 |
| `colsample_bytree` | sampling | Fraction of columns used per tree | 0.5 to 1.0 |
| `reg_lambda` | regularization | L2 penalty on leaf values | 1 to 10 |
| `gamma` | regularization | Minimum loss reduction required to split | 0 to 5 |
How the main parameters behave
The learning rate and the number of trees trade off against each other. A smaller learning_rate makes each tree correct less of the error, so you need more trees. Small rates such as 0.05 usually generalize better but train more slowly. Fix a small rate first, then let early stopping choose the tree count.
max_depth controls how many features can interact inside one tree. Depth 3 allows interactions among up to three features. Deeper trees fit more complex patterns and overfit more easily.
min_child_weight blocks splits that create leaves with too little evidence. For squared error, the hessian is 1 per row, so the value acts like a minimum row count. For log loss, the hessian is p(1 - p), so confident rows count for less than that.
subsample and colsample_bytree add randomness, which makes the trees less correlated with each other. Values around 0.8 are a common starting point.
reg_lambda and gamma come from the regularized objective. A larger reg_lambda shrinks leaf values. A larger gamma prunes splits whose gain is too small.
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
)
for depth in [1, 3, 6, 10]:
model = XGBClassifier(
n_estimators=500,
learning_rate=0.05,
max_depth=depth,
min_child_weight=2,
subsample=0.8,
colsample_bytree=0.8,
reg_lambda=1.0,
eval_metric="logloss",
early_stopping_rounds=30,
)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)
print(depth, model.best_iteration, round(model.score(X_val, y_val), 3))The loop changes only max_depth, so any difference in the output comes from tree complexity. Shallow trees usually need more rounds before early stopping triggers.
Common mistakes
- Raising
n_estimatorswith a high learning rate and no early stopping. The model overfits as trees accumulate. - Tuning
max_depthandlearning_rateon the same validation set that early stopping uses. The final score becomes optimistic, so keep a separate test set. - Changing many parameters at once. You can no longer tell which change helped.
Exercise
You lower learning_rate from 0.1 to 0.02 and keep n_estimators=200 with no early stopping. Validation performance gets worse. What is the likely cause, and how do you fix it?
Show answer
Each tree now contributes five times less, so 200 trees are not enough and the model underfits. Raise n_estimators (for example to 1000 or more) and use early stopping on a validation set so the tree count is chosen automatically.