XGBoost objectives and tree regularization
Quick answer
XGBoost adds trees to minimize a training objective that combines prediction loss and tree complexity. A second-order approximation uses both the first derivative (gradient) and second derivative (Hessian) of each training loss to choose leaf updates. Regularization can shrink those updates or make additional splits less attractive. Static examples tested locally with Python 3.13.5, XGBoost 3.1.3, LightGBM 4.6.0 and CatBoost 1.2.8 where imported. ByteMind package execution is unverified.
Solve a leaf quadratic
G, H, lam = -6, 3, 1w = -G/(H+lam)print(w, G*w + (H+lam)*w*w/2)Observed local output:
1.5 -4.5The quadratic is minimized at a positive correction; the loss approximation improves by 4.5 before shrinkage.
How it works
For a leaf containing training rows I, let G = sum(g_i) and H = sum(h_i), where g_i and h_i are derivatives with respect to the current raw prediction. With L2 strength lambda and no L1 penalty, the part of the approximate objective depending on leaf weight w is Gw + (H+lambda)w²/2. Differentiating gives w = -G/(H+lambda), provided H+lambda is positive. For half squared error, g = prediction - target and h = 1 (unweighted). For G=-6, H=3 and lambda=1, the optimum is 1.5. Substituting it into the quadratic gives -4.5, compared with zero for w=0. A split gain in the same simplified setting is 0.5(G_left²/(H_left+lambda) + G_right²/(H_right+lambda) - G_parent²/(H_parent+lambda)) - gamma. Positive gain is necessary for this unconstrained criterion but other constraints may still rule out a split. The learning rate scales the new tree contribution. reg_alpha adds L1 leaf-weight regularization; min_child_weight limits child Hessian mass, not necessarily row count. objective determines the training derivatives; eval_metric determines what you monitor. For classification choose a classification objective, and do not treat margins as calibrated probabilities. This derivation excludes L1, sample weights and constrained leaf updates.
Fit one regularized tree
import numpy as npfrom xgboost import XGBRegressorX=np.array([[0],[0],[1],[1]])y=np.array([0,0,4,4])m=XGBRegressor(n_estimators=1,max_depth=1,learning_rate=1,base_score=2,reg_lambda=2,reg_alpha=0,gamma=0,tree_method="hist",device="cpu",n_jobs=1,random_state=7).fit(X,y)print(m.predict([[0],[1]]).tolist())Observed local output:
[1.0, 3.0]Each leaf contains two residuals of the same sign. The L2 penalty halves the unregularized update from magnitude 2 to 1.
Common mistakes
Do not divide by zero curvature, interpret gamma as a learning rate, or assume min_child_weight always counts rows. Do not copy a GPU flag from older tutorials: this local example explicitly sets device="cpu" and tree_method="hist".
Practice and next steps
Recompute the weight and objective reduction with lambda=3. Explain why the correction shrinks. Then change reg_lambda in the local example and compare the predictions; never tune on the final test set. Complete the knowledge check and staged exercise. Next: LightGBM histogram and growth.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
XGBoost objectives and tree regularization — knowledge check
Apply xgboost objectives and tree regularization to new scenarios.
G=-8, H=3, lambda=1. Optimal leaf weight?
What does gamma control?
What does min_child_weight bound?
What does objective specify?
What does a larger L2 term do for fixed G and H?