2.3.8Tree-Based & Instance Methods

Feature importance from trees

1,676 words8 min readdifficulty · medium1 backlinks

WHAT are we computing?

The three ingredients:

  • Impurity i(t)i(t): how mixed a node is (Gini or entropy for classification, variance/MSE for regression).
  • Impurity decrease Δi(t)\Delta i(t): how much the split cleans up the node.
  • Weighting Nt/NN_t/N: a split high in the tree affects many samples → counts more.

HOW: derive Δi(t)\Delta i(t) from first principles

WHY do we weight children? A parent node with NtN_t samples splits into a left child (NtLN_{t_L}) and right child (NtRN_{t_R}). We compare the parent's impurity to the average impurity of the children — but a child holding more samples should count more. So we weight children by their sample fractions:

Δi(t)=i(t)    NtLNti(tL)    NtRNti(tR)\Delta i(t)=i(t)\;-\;\frac{N_{t_L}}{N_t}\,i(t_L)\;-\;\frac{N_{t_R}}{N_t}\,i(t_R)

Why this is always 0\ge 0: greedy tree algorithms choose the split that maximizes this drop, and impurity is a concave function, so splitting can never increase (weighted) impurity. That's why importances are non-negative.

Now multiply by the arrival weight Nt/NN_t/N (fraction of all data that reaches node tt) and sum over every node splitting on ff → gives Imp(f)\text{Imp}(f) above.

Forests: average over trees

For a Random Forest / GBM with TT trees, importance is the mean over trees, usually normalized to sum to 1: Impforest(f)=1Tτ=1TImpτ(f),Imp^(f)=Imp(f)gImp(g)\text{Imp}_{\text{forest}}(f)=\frac{1}{T}\sum_{\tau=1}^{T}\text{Imp}_\tau(f),\qquad \widehat{\text{Imp}}(f)=\frac{\text{Imp}(f)}{\sum_{g}\text{Imp}(g)}

Figure — Feature importance from trees

Worked Example 1 — one split, Gini

Root has 10 samples: 5 positive, 5 negative. A split on feature X1X_1 gives:

  • Left: 4 samples (4 pos, 0 neg)
  • Right: 6 samples (1 pos, 5 neg)

Step 1 — parent impurity. Why? Baseline mixed-ness. i(root)=1(0.52+0.52)=0.5i(\text{root})=1-(0.5^2+0.5^2)=0.5

Step 2 — child impurities. Why? Measure how clean each side got. i(L)=1(12+02)=0i(L)=1-(1^2+0^2)=0 i(R)=1((1/6)2+(5/6)2)=1(0.0278+0.6944)=0.2778i(R)=1-((1/6)^2+(5/6)^2)=1-(0.0278+0.6944)=0.2778

Step 3 — weighted child impurity. Why? Big child counts more. 410(0)+610(0.2778)=0.1667\frac{4}{10}(0)+\frac{6}{10}(0.2778)=0.1667

Step 4 — impurity decrease. Why? Cleanup achieved. Δi=0.50.1667=0.3333\Delta i = 0.5-0.1667=0.3333

Step 5 — arrival weight. Root sees all data, Nt/N=10/10=1N_t/N=10/10=1. Imp(X1)=1×0.3333=0.3333\text{Imp}(X_1)=1\times 0.3333=\boxed{0.3333}


Worked Example 2 — deeper node weighting

Suppose X2X_2 splits at a node reached by only 3 of 10 samples, with Δi=0.4\Delta i=0.4.

Step — apply arrival weight. Why? A split affecting few samples shouldn't dominate. Imp(X2)=310×0.4=0.12\text{Imp}(X_2)=\frac{3}{10}\times 0.4=0.12

Even with a bigger local Δi\Delta i (0.4 > 0.3333), X2X_2 (0.12) ranks below X1X_1 (0.3333) because X1X_1's split touched everyone. This is the whole reason for Nt/NN_t/N.

Normalized: Imp^(X1)=0.33330.4533=0.735\widehat{\text{Imp}}(X_1)=\frac{0.3333}{0.4533}=0.735, Imp^(X2)=0.265\widehat{\text{Imp}}(X_2)=0.265.


Worked Example 3 — regression (variance drop)

Node with y={2,4,6,8}y=\{2,4,6,8\}, yˉ=5\bar y=5. Split into {2,4}\{2,4\} and {6,8}\{6,8\}.

Step 1 — parent variance. (9+1+1+9)4=5\frac{(9+1+1+9)}{4}=5 Step 2 — child variances. Each child: yˉ=3\bar y=3 or 77, var =1+12=1=\frac{1+1}{2}=1. Step 3 — weighted. 12(1)+12(1)=1\frac12(1)+\frac12(1)=1 Step 4 — decrease. Δi=51=4\Delta i=5-1=4. Big drop → this feature separates yy well.



Recall Feynman: explain to a 12-year-old

Imagine sorting a messy box of red and blue marbles into cleaner piles. Each "question" you ask (like is it bigger than a coin?) that makes the piles neater earns points. A question that splits ALL the marbles into perfect red-only and blue-only piles earns lots of points; a question that only helps 3 marbles earns few. Add up each question-type's points across the whole game — the questions with the most points are the most important features. But careful: a question with tons of possible answers (like exact weight in grams) can "cheat" and look important even when it's just memorizing.


Flashcards

What does MDI (Mean Decrease in Impurity) measure?
The total weighted impurity reduction contributed by all splits using a feature, summed over the tree(s).
Write the weighted impurity decrease at a node.
Δi(t)=i(t)NtLNti(tL)NtRNti(tR)\Delta i(t)=i(t)-\frac{N_{t_L}}{N_t}i(t_L)-\frac{N_{t_R}}{N_t}i(t_R)
Why multiply each node's Δi\Delta i by Nt/NN_t/N?
So splits reaching more samples (usually high in the tree) count more than deep splits affecting few samples.
Give the Gini impurity formula and why it works.
1kpk21-\sum_k p_k^2; it's the probability two random draws from the node disagree in class — a measure of mixedness.
Why is impurity decrease always non-negative?
The greedy algorithm chooses the split maximizing the (concave) impurity drop; splitting can't increase weighted impurity.
What impurity is used in regression trees?
Variance / MSE about the node mean: 1Nt(yyˉt)2\frac{1}{N_t}\sum(y-\bar y_t)^2.
Main bias of MDI feature importance?
Inflated for high-cardinality/continuous features (more split points → can fit noise); also biased if computed on training data.
Fix for MDI's biases?
Use permutation importance (shuffle feature on held-out data) or SHAP values.
What happens to importance among two correlated features?
Once one is used, the other becomes redundant and gets near-zero MDI, hiding a real predictor.
How is forest importance obtained from tree importances?
Average the per-tree importances across all trees, then normalize to sum to 1.

Connections

  • Decision Trees — MDI is a byproduct of the same impurity used to grow the tree.
  • Gini vs Entropy — choice of i(t)i(t) changes importance values.
  • Random Forests — importance averaged over many trees for stability.
  • Gradient Boosting — also exposes split-based importances (gain/cover/frequency).
  • Permutation Importance — unbiased, model-agnostic alternative.
  • SHAP Values — game-theoretic attribution fixing MDI biases.
  • Bias-Variance Tradeoff — training MDI overestimates due to overfitting.

Concept Map

classification form of

regression form of

parent minus weighted children

weighted by

summed over splits on f

scales contribution

maximizes

ensures

averaged over trees

divided by total

Impurity i of t

Impurity decrease delta i

Arrival weight Nt over N

Feature importance MDI

Gini impurity

Variance impurity MSE

Greedy split choice

Importance non-negative

Forest importance

Normalized to sum 1

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Feature importance ka core idea simple hai: decision tree har split pe impurity (matlab node kitna "mixed" hai) ko kam karta hai. Jo feature jitni zyada impurity kam karta hai across saare splits, wo utna hi important maana jaata hai. Isko MDI ya Gini importance kehte hain. Formula bas ek weighted sum hai: har node pe impurity drop Δi\Delta i nikaalo, usko Nt/NN_t/N (kitne samples us node tak pahunche) se multiply karo, aur us feature ke saare nodes ka total le lo.

Weighting kyun? Socho ek split root pe hai jo saare 10 samples ko affect karta hai, aur doosra split neeche sirf 3 samples ko. Root wala split zyada matter karega, isiliye Nt/NN_t/N multiply karte hain. Classification mein impurity Gini =1pk2=1-\sum p_k^2 hoti hai (do random draws alag class ke aane ki probability), aur regression mein variance/MSE hoti hai.

Ek badi warning: MDI biased hota hai. High-cardinality ya continuous features (jinke bahut saare split points hote hain) noise fit karke bhi important dikhne lagte hain. Aur agar do features correlated hain, to ek use hote hi doosra redundant ho jaata hai aur uski importance almost zero ho jaati hai — jabki wo actually predictive tha. Isliye serious kaam mein permutation importance ya SHAP use karo, jo held-out data pe honestly measure karte hain. MDI quick intuition ke liye best hai, final decision ke liye cross-check zaroori.

Go deeper — visual, from zero

Test yourself — Tree-Based & Instance Methods

Connections