XGBoost fundamentals and tuning
2.3.13· AI-ML › Tree-Based & Instance Methods
XGBoost exist kyun karta hai?
Random Forest se compare karo (jo independent trees ko parallel mein average karta hai), boosting trees ko sequentially aur dependently banata hai — har ek is baat par depend karta hai ki pehle kya aaya tha.
MODEL kya hai? (Additive form)
Hum trees greedily add karte hain: round par prediction hai Hume (naya tree) chunna hai jo objective ko sabse zyada reduce kare.
Objective — regularization andar kyun built-in hai
Kyun: ordinary gradient boosting sirf loss minimize karta hai; XGBoost ko optimization ke andar add karta hai taaki trees prune hon aur leaf weights shrink hon — automatically, afterthought ki tarah nahi.
KAISE: Leaf weights ko first principles se derive karna
Additive step ko round par objective mein daalo (pehle ke trees ke constants drop karo):
Step 1 — Loss ko ke around 2nd order tak Taylor-expand karo. Yeh step kyun? Hum ek arbitrary loss ko seedha tree structures par optimize nahi kar sakte, isliye hum ise ek quadratic se approximate karte hain — jise hum closed form mein solve kar sakte hain.
Har example ke liye gradient aur Hessian define karo: Tab
Step 2 — Examples ko leaf ke anusaar group karo. Ek tree har ko exactly ek leaf mein bhejta hai. Maano aur wo leaf constant output karta hai. Kyunki : Maano aur .
Yeh step kyun? Ab objective har mein independent quadratics ka sum hai — minimize karna trivial hai.
Step 3 — ke upar minimize karo. Derivative zero set karo:
Step 4 — Wapas substitute karo aur ek fixed tree structure ke liye best achievable objective nikalo:
Split kaise chuna jaata hai — Gain formula
Hum har tree try nahi kar sakte. Isliye greedy tarike se grow karte hain: ek node par, uske data ko Left () aur Right () mein split karne ki koshish karo. Improvement hai (score pehle) (score baad mein):
kyun? Split karne se ek leaf add hoti hai, jiska cost hai. Agar Gain ho, split mat karo — yahi built-in pruning hai.

Worked Example 1 — Leaf weight (regression, squared error)
Squared error ke liye: (negative residual), .
Maano ek leaf mein 3 examples hain jinka residual hai, toh . lo.
- , .
- .
Yeh step kyun? Leaf output karta hai — mean residual ke paas hai lekin ki wajah se 0 ki taraf shrunk hai. Bada ⇒ zyada shrinkage ⇒ kam overfit.
Worked Example 2 — Kya split karna chahiye?
Node ka hai. Proposed split: Left , Right . . Gain ⇒ split reject karo. Kyun: loss mein reduction complexity cost ko beat nahi karti.
Worked Example 3 — Log-loss gradients
Binary classification, , loss . Tab aur . Agar aur current hai: (bada push upar), . Kyun: confident-wrong points ko bade gradients milte hain ⇒ agla tree unpar focus karta hai.
Key Hyperparameters (jo 80/20 matter karta hai)
| Param | Controls | Direction |
|---|---|---|
eta / learning_rate |
har tree ka contribution shrink karta hai: | ↓ = zyada robust, zyada trees chahiye |
n_estimators |
trees ki sankhya | eta ke saath pair karo; early stopping use karo |
max_depth |
tree complexity | ↓ overfit se ladne ke liye |
min_child_weight |
leaf mein min | ↑ overfit se ladne ke liye |
gamma |
split ke liye min gain () | ↑ = zyada pruning |
lambda, alpha |
leaf weights par L2 / L1 | ↑ = zyada regularization |
subsample |
tree ke liye row sampling | <1 randomness add karta hai |
colsample_bytree |
feature sampling | <1 trees ko decorrelate karta hai |
Recall Feynman: ek 12-saal ke bachche ko samjhao
Socho tum ek tasveer bana rahe ho aur ek dost tumhari galtiyan dikhata hai. Tum thoda theek karte ho. Phir ek aur dost batata hai ki kya abhi bhi galat hai, aur tum woh thoda theek karte ho. Har dost sirf ek chhoti si correction karta hai (yahi learning rate hai), aur tum ek dost ki nahi sunते agar unka "fix" matter karne ke liye bahut chhota ho (yahi gamma pruning hai). Bahut saare doston ke baad, tumhari tasveer shandar hoti hai — lekin agar tum hamesha ke liye doston ko bulate raho, toh woh un chezon ko "fix" karne lagte hain jo pehle se theek theen aur kharab kar dete hain. Isliye tum sahi waqt par ruk jaate ho (early stopping). XGBoost aaise vinammra doston ki team hai jo gradient (which way fix karna hai) aur Hessian (how carefully fix karna hai) dono use karke jawab ko paas la dete hain.
Recall — khud ko test karo
Flashcards
XGBoost ka additive model form kya hai?
XGBoost loss ka 2nd-order Taylor expansion kyun use karta hai?
Optimal leaf weight ka formula?
Split Gain ka formula?
kya karta hai?
kya karta hai?
Squared error ke liye aur kya hain?
Log-loss ke liye aur kya hain?
XGBoost mein Random Forest ki tarah unlimited trees kyun nahi add kar sakte?
Learning rate (eta) ka kya role hai?
Sabse high-leverage tuning pair kaunse do hyperparameters hain?
XGBoost structurally Random Forest se kaise alag hai?
Connections
- Gradient Boosting — XGBoost ek regularized, 2nd-order implementation hai.
- Decision Trees — base learner ().
- Random Forests — contrast: bagging/parallel vs boosting/sequential.
- Regularization (L1 L2) — leaf weights par kaam karte hain.
- Bias-Variance Tradeoff — boosting bias reduce karta hai; regularization variance control karta hai.
- Taylor Series — gradient+Hessian approximation ka basis.
- Newton's Method — ek damped Newton step hai.