Compared to Random Forest (which averages independent trees built in parallel), boosting builds trees sequentially and dependently — each depends on what came before.
WHY: ordinary gradient boosting only minimizes loss; XGBoost adds Ωinside the optimization so trees are pruned and leaf weights shrunk automatically, not as an afterthought.
Plug the additive step into the objective at round t (dropping constants from earlier trees):
L(t)=∑i=1nl(yi,y^i(t−1)+ft(xi))+Ω(ft).
Step 1 — Taylor-expand the loss to 2nd order around y^(t−1).
Why this step? We can't optimize an arbitrary loss over tree structures directly, so we approximate it by a quadratic — which we can solve in closed form.
Define per-example gradient and Hessian:
gi=∂y^l(yi,y^)y^(t−1),hi=∂y^2l(yi,y^)y^(t−1).
Then
L(t)≈∑i[gift(xi)+21hift(xi)2]+γT+2λ∑jwj2+const.
Step 2 — Group examples by leaf. A tree sends each xi to exactly one leaf j. Let Ij={i:xi lands in leaf j} and let that leaf output the constant wj. Since ft(xi)=wq(i):
L(t)=∑j=1T[(∑i∈Ijgi)wj+21(∑i∈Ijhi+λ)wj2]+γT.
Let Gj=∑i∈Ijgi and Hj=∑i∈Ijhi.
Why this step? Now the objective is a sum of independent quadratics in each wj — trivial to minimize.
Step 3 — Minimize over wj. Set derivative to zero:
∂wj∂(Gjwj+21(Hj+λ)wj2)=Gj+(Hj+λ)wj=0.
Step 4 — Substitute back to get the best achievable objective for a fixed tree structure:
We can't try every tree. Instead we grow greedily: at a node, try splitting its data into Left (L) and Right (R). The improvement is (score before) − (score after):
Why the −γ? Splitting adds one leaf, costing γ. If Gain ≤0, don't split — this is built-in pruning.
Node has G=−10,H=3. Proposed split: Left (GL=−8,HL=2), Right (GR=−2,HR=1). λ=1,γ=2.
Gain=21[364+24−4100]−2=21[21.33+2−25]−2=21(−1.67)−2=−2.83.
Gain <0 ⇒ reject the split.Why: the reduction in loss doesn't beat the γ complexity cost.
Binary classification, p=σ(y^), loss =−[ylnp+(1−y)ln(1−p)].
Then gi=pi−yi and hi=pi(1−pi).
If y=1 and current p=0.2: g=−0.8 (big push up), h=0.16. Why: confident-wrong points get large gradients ⇒ next tree focuses on them.
shrinks each tree's contribution: y^(t)=y^(t−1)+ηft
↓ = more robust, need more trees
n_estimators
number of trees K
pair with eta; use early stopping
max_depth
tree complexity
↓ to fight overfit
min_child_weight
min ∑hi in a leaf
↑ to fight overfit
gamma
min gain to split (γ)
↑ = more pruning
lambda, alpha
L2 / L1 on leaf weights
↑ = more regularization
subsample
row sampling per tree
<1 adds randomness
colsample_bytree
feature sampling
<1 decorrelates trees
Recall Feynman: explain to a 12-year-old
Imagine drawing a picture and a friend points out your mistakes. You fix them a little. Then another friend points out what's still wrong, and you fix that a little. Each friend only makes a small correction (that's the learning rate), and you don't listen to a friend if their "fix" is too tiny to matter (that's gamma pruning). After many friends, your picture is great — but if you keep calling friends forever, they start "fixing" things that were already fine and ruin it. So you stop at the right time (early stopping). XGBoost is a team of these polite friends who each nudge the answer closer using both which way to fix (gradient) and how carefully to fix (Hessian).
y^i=∑k=1Kfk(xi), a sum of regression trees added sequentially, each fitting residual errors.
Why does XGBoost use a 2nd-order Taylor expansion of the loss?
To turn an arbitrary differentiable loss into a quadratic in the tree output, which can be minimized in closed form per leaf (Newton-style), using gradient gi and Hessian hi.
Formula for the optimal leaf weight?
wj∗=−Gj/(Hj+λ) where Gj=∑i∈Ijgi, Hj=∑i∈Ijhi.
Dekho, XGBoost ka core idea simple hai: ek hi bada tree banane ki jagah, hum bahut saare chote trees ek-ek karke banate hain. Pehla tree kuch predict karta hai, usme jo galtiyan (residuals) reh jaati hain, doosra tree unko theek karta hai, phir teesra baaki galtiyan theek karta hai — aise chain chalti rehti hai. Isko boosting bolte hain. Random Forest se difference yeh hai ki wahan saare trees independent hote hain aur average lete hain, yahan har tree pichhle ke mistakes pe depend karta hai.
Ab maths ki jaan: har round pe hum loss ko 2nd-order Taylor expansion se approximate karte hain, yaani gradient gi (kis direction fix karna hai) aur Hessian hi (kitna carefully fix karna hai) dono use karte hain. Isse ek leaf ka best value nikalta hai: w∗=−G/(H+λ). Yeh λ leaf value ko chhota (shrink) karta hai taaki overfit na ho. Aur split karna hai ya nahi, yeh Gain formula batata hai — agar Gain γ se kam hai to split reject, matlab automatic pruning ho jaata hai.
Tuning ka 80/20: sabse important hai learning_rate (eta) chhota rakho aur early stopping use karo validation set pe, taaki trees ki sahi number khud choose ho jaaye. Ek badi galti students karte hain — "zyada trees matlab zyada accuracy" — yeh Random Forest me sahi hai, par XGBoost me zyada trees noise ko fit karke overfit kar dete hain. Isi tarah max_depth bahut zyada mat rakho (usually 3 se 8), warna har deep tree ke saath overfit compound hota jaata hai. Regularization ke liye gamma, min_child_weight, lambda ka combination use karo. Bas yaad rakho: dheere seekho, samay pe ruk jaao.