XGBoost: A Deep Dive into Gradient Boosting Logic
XGBClassifier(), you're missing why it consistently dominates tabular data competitions.The core logic revolves around additive training. Instead of training one massive tree, XGBoost trains a sequence of weak learners. Each new tree attempts to predict the residuals (the errors) of the previous ensemble.
The Mathematical Intuition
The objective function is split into two parts: the loss function (which measures how well the model fits the data) and the regularization term (which penalizes model complexity to avoid overfitting).
1. Taylor Expansion: Unlike standard Gradient Boosting, XGBoost uses the second-order derivative (Hessian) of the loss function. This provides more information about the curvature of the loss surface, allowing the model to converge much faster.
2. Gain Calculation: When deciding where to split a node, XGBoost calculates a "Gain" score. It only splits if the reduction in loss outweighs the penalty imposed by the regularization parameter $\lambda$.
3. Shrinkage: After each tree is added, its contribution is scaled down by a learning rate ($\eta$). This forces the model to learn slowly and prevents it from overshooting the optimal solution.
For anyone implementing this from scratch, the most critical part is understanding the relationship between the gradient ($g_i$) and the hessian ($h_i$) when calculating the optimal weight of a leaf:
# Conceptual weight calculation for a leaf in XGBoost
# weight = - sum(g_i) / (sum(h_i) + lambda)This structure is exactly why XGBoost handles sparse data so well; it learns a default direction for missing values based on which side of the split reduces the loss more effectively.
