2.3.4 · HinglishTree-Based & Instance Methods

Tree pruning techniques

2,035 words9 min readRead in English

2.3.4 · AI-ML › Tree-Based & Instance Methods


WHAT hai pruning?


Cost-Complexity Pruning (important 20%)

Yeh algorithm CART / scikit-learn ke ccp_alpha ke peeche hai. Ise master kar lo aur tumhare paas exam/interview ki 80% value aa jaayegi.

Criterion ko scratch se derive karna

WHAT chahiye humein? Ek tree jo accurate bhi ho aur chhota bhi. Dono wishes ko ek objective mein daalen.

Maano ek tree hai jiske terminal nodes (leaves) hain. Define karo:

  • = tree ki total error (ya impurity) = , jahan leaf par error hai.
  • = leaves ki sankhya (hamara complexity measure).

Hum complexity ko ek knob ke saath penalize karte hain:

  • : koi penalty nahi → full tree rakho.
  • : penalty dominant ho jaati hai → root tak collapse ho jaao (single node).

Toh ko sweep karne se sabse bade se sabse chhote tak trees ki ek sequence milti hai.

Kya subtree kaatna worth hai? (key derivation)

Ek internal node aur usmein rooted subtree lo. Do options compare karo:

Option A — subtree rakho: cost . Option B — ise ek single leaf tak prune karo: cost .

Hum prune karein jab B, A se bura na ho:

ke liye solve karo: Kyunki hai, divide karne par kuch khatranaak nahi hota jab :

WHY numerator ? Yeh woh error hai jo collapse karne par tum gain karte ho (subtree zyada accurate tha, isliye , numerator ). WHY se divide karo? Tum us error-cost ko un leaves ki sankhya par spread kar rahe ho jo tum remove karte ho. Yeh "extra error per leaf saved" hai — ek fair per-unit price.

  1. Full tree grow karo.
  2. Har internal node ke liye compute karo.
  3. Sabse chhote wale node ko prune karo → ek chhota tree milo; woh record karo.
  4. Tab tak repeat karo jab tak sirf root na bache.
  5. Ab tumhare paas ek nested sequence hai increasing ke saath.
  6. Best cross-validation se chunno (sabse kam CV error wala tree).
Figure — Tree pruning techniques

Reduced-Error Pruning (simpler alternative)


Pre-pruning knobs (scikit-learn)

Knob Meaning Effect
max_depth max levels shallower tree
min_samples_split ek node split karne ke liye min samples kam splits
min_samples_leaf leaf ke liye min samples smoother leaves
min_impurity_decrease split karne ke liye required impurity drop weak splits block karta hai
ccp_alpha cost-complexity post-pruning

Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho tumne ek animal guess karne ke liye "haan/na" sawaalon ka ek family tree draw kiya. Agar tum questions poochte raho, eventually har animal ka apna chhota sa box ho jaata hai — lekin kuch questions bekar hote hain ("kya uske naam mein 7 letters hain?") aur sirf unhi animals ke liye kaam karte hain jo tumne pehle se dekhe hain. Pruning un bekar branches ko kaat-chhaantna hai taaki tree naaye animals ke baare mein bhi samajhdaar rahe jinhe usne kabhi nahi dekha. Hum har branch test karte hain: "agar main tumhe kaat doon, kya main unhi animals ko sahi guess kar paunga jo maine pehle chhupaye the?" Agar haan → snip! Simpler tree, phir bhi smart.


Connections


Flashcards

Tree pruning ki do families kya hain?
Pre-pruning (growing karte waqt early stopping) aur post-pruning (poora grow karo, phir kaato).
Cost-complexity objective likho.
, error plus ek penalty times number of leaves.
par konsa tree milta hai vs par?
→ full unpruned tree; → sirf root node.
Node ke effective alpha ka formula.
.
Weakest-link pruning mein konsa node pehle prune hota hai?
Woh internal node jiska sabse chhota ho (sabse sasta error-per-leaf-saved).
Post-pruning generally pre-pruning se better kyun hai?
Pre-pruning greedy/short-sighted hai (horizon effect); post-pruning kaatne se pehle poore subtree ki value dekhta hai (XOR-like splits handle karta hai).
Best kaise choose karte hain?
Cross-validation se (ya validation set se) — woh chuno jo CV error minimize kare, training error nahi.
Reduced-Error Pruning kya hai?
Bottom-up, har subtree ko majority-class leaf se replace karo; agar validation accuracy na gire toh replacement rakho.
Pruning decide karne ke liye training error kyun use nahi kar sakte?
Training error zyada splits ke saath monotonically decrease hoti hai, isliye yeh kabhi prune karne ka signal nahi deti — tumhe held-out data chahiye.
Teen pre-pruning hyperparameters batao.
max_depth, min_samples_leaf, min_impurity_decrease (aur bhi: min_samples_split, ccp_alpha).
badhane ka tree size par kya asar hota hai?
Bada → chhota tree (leaves par bhaari penalty).
Over-pruning ka khatraa kya hai?
Underfitting — bias badhta hai, training aur test error dono kharab ho sakte hain.

Concept Map

memorizes noise

motivates

reduces

improves

family 1

family 2

greedy, misses XOR

sees full subtree

objective

alpha=0

alpha to inf

cut when

formula

Fully grown tree

Overfitting

Pruning

Tree complexity

Generalization

Pre-pruning early stopping

Post-pruning

Short-sighted

Cost-complexity pruning

R_alpha = R T + alpha leaves

Root only

Effective alpha of node

alpha = R t - R Tt over leaves - 1