2.3.8 · D4Tree-Based & Instance Methods

Exercises — Feature importance from trees

2,288 words10 min readBack to topic
Figure — Feature importance from trees

The figure above is the object every problem below lives inside: a parent bucket splitting into a left and right child. Keep it in your mind's eye.


Level 1 — Recognition

L1.1

For a binary classification node with class proportions , what is its Gini impurity, and what does that number mean in words?

Recall Solution

WHAT we do: plug into . WHAT it means: if you draw two points at random (with replacement) from this bucket, the chance they belong to different classes is . A 50/50 bucket is the most mixed a binary node can be — that is why is the maximum Gini for two classes.

L1.2

Which of these correctly names the quantity ? (a) impurity, (b) arrival weight, (c) impurity decrease, (d) Gini index.

Recall Solution

(b) arrival weight. It is the fraction of all data that reaches node . It has nothing to do with how mixed the node is (that's impurity) — it is purely a "how many people showed up" head-count.

L1.3

A leaf holds only class-A points. State its Gini impurity and its variance impurity (if we treated the labels as numbers all equal to ).

Recall Solution

Pure bucket → . Gini . For variance, every so and . Both impurities are — a pure node has nothing left to clean.


Level 2 — Application

L2.1

Root of a tree: 20 samples, 12 class-P and 8 class-N. Compute the root Gini impurity.

Recall Solution

.

L2.2

That root (20 samples) splits on feature into:

  • Left: 8 samples (6 P, 2 N)
  • Right: 12 samples (6 P, 6 N)

Compute for this split, then (the split is at the root, so its arrival weight is ).

Recall Solution

Step 1 — child impurities (why: measure how clean each side got). Left: . Right: . Step 2 — weighted child impurity (why: bigger child counts more). Step 3 — decrease. . Step 4 — arrival weight , so . The split barely helped — the right child is still 50/50, so this feature does little impurity work.

L2.3 (regression)

A regression node holds . Compute its variance impurity .

Recall Solution

. Deviations squared: , sum .


Level 3 — Analysis

L3.1

Feature splits the root (all samples) with local . Feature splits a deeper node reached by only samples, with a larger local . Which feature gets more importance from these two splits, and why?

Recall Solution

WHAT we compute: apply the arrival weight to each. Answer: (0.10) beats (0.075) even though 's local cleanup was three times bigger. WHY: 's split only touched a quarter of the data, so its cleanup is scaled down by . Importance rewards splits that clean a lot of people, not just clean a lot. This is the entire reason the weight exists — see Bias-Variance Tradeoff for why we distrust deep, low-sample splits.

L3.2

Normalise the two importances from L3.1 so they sum to .

Recall Solution

Total . Check: ✓. Normalising lets you read each number as "fraction of total impurity work done."

L3.3

Two features and are perfectly correlated (identical columns). The tree happens to use at every relevant split and never uses . Predict the MDI of and of , and explain the danger.

Recall Solution

is large; because never appears in a split, and MDI only sums over nodes that split on that feature. The danger: a naïve reader concludes " is useless" — but is just as predictive as ; it lost a coin-flip for who-gets-used. Fix: cluster correlated features, or use grouped Permutation Importance / SHAP Values.


Level 4 — Synthesis

L4.1 — Build a whole tree's importances

A dataset has samples, 8 class-0 and 8 class-1. The fitted tree is:

  • Root (16 samples, 8/8): splits on feature into
    • Node L (8 samples, 7 class-0, 1 class-1): splits on feature into
      • Leaf LL (4 samples, 4 class-0, 0 class-1)
      • Leaf LR (4 samples, 3 class-0, 1 class-1)
    • Leaf R (8 samples, 1 class-0, 7 class-1): leaf.

Compute , , then the normalised importances.

Recall Solution

Root split (on ), arrival weight . . Child L: . Child R: . Weighted children: . . .

Node-L split (on ), arrival weight . (from above). Leaf LL: pure → . Leaf LR: . Weighted children: . . .

Normalise. Total . does of the impurity work — it separated the two classes at the root; only mopped up a nearly-pure node reached by half the data. This is the same Decision Trees machinery, just totalled per feature.

L4.2 — regression synthesis

A regression tree: root holds (), splits on into and . Compute (root split, weight ).

Recall Solution

Root: , deviations, sum , . Each child is constant → variance . Weighted children . . Arrival weight . A perfect split (each child constant) removes all variance — the maximum possible cleanup.


Level 5 — Mastery

L5.1

You fit a Random Forest on customer data. A customer_id column (a unique integer per row, thousands of distinct values) shows the highest MDI. Is customer_id your most predictive feature? Diagnose and prescribe.

Recall Solution

No. customer_id has enormous cardinality, so it offers a split point between almost every pair of rows. With so many candidate splits, it can carve the training data into near-pure leaves by memorising, driving up impurity decrease on the training set even though it carries no generalisable signal — a classic MDI cardinality bias compounded by being computed on training data (overfitting; see Bias-Variance Tradeoff). Prescribe: (1) drop identifier columns before training; (2) rank features with Permutation Importance on a held-out set (shuffle the column, measure the score drop — an id column shuffled changes nothing, so its permutation importance collapses to ~0); (3) cross-check with SHAP Values.

L5.2

Your MDI ranking says feature is worthless (importance ), but a domain expert insists is highly predictive. Under what single condition can both be right, and how do you confirm it?

Recall Solution

Both are right if is strongly correlated with another feature that the trees happened to use instead. Once is split on, becomes redundant and never gets selected, so its MDI is ~0 — yet alone would predict well. Confirm: (i) compute the correlation / clustering of with used features; (ii) run grouped permutation importance (permute the cluster together) — if the cluster's importance is high, the signal lives in jointly; (iii) re-fit with removed and watch 's MDI jump. This is exactly the correlated-feature failure mode from the parent note's Mistake 2.

L5.3

Choose the right tool. For each goal below, pick MDI, permutation importance, or SHAP, and justify in one line. (a) A fast, free ranking during model development. (b) An honest ranking for a report to stakeholders. (c) Per-prediction, signed attribution ("this loan was denied because income was low").

Recall Solution

(a) MDI — it's computed for free while the tree grows; fine for quick internal iteration, just distrust high-cardinality winners. (b) Permutation importance — model-agnostic, computed on held-out data, so it doesn't reward training overfit; the fair choice for a stakeholder ranking. (c) SHAP — the only one giving signed, per-sample contributions with additive game-theoretic guarantees, so it can say why this individual prediction went the way it did.



Connections

  • Feature importance from trees — the parent note these exercises drill.
  • Decision Trees — every here is a split the tree already made.
  • Gini vs Entropy — swap and the numbers change, the method doesn't.
  • Random Forests — L5.1 lives here (average over trees).
  • Gradient Boosting — gain-based importance shares MDI's biases.
  • Permutation Importance — the fix in L5.
  • SHAP Values — per-prediction attribution in L5.3.
  • Bias-Variance Tradeoff — why training-set MDI overestimates.