Visual walkthrough — Feature importance from trees
We follow a tiny box of marbles being sorted by questions. Every arrow, every colour, every number below is drawn in the figures — read the picture and the words together.
Step 1 — A node is just a bag of items, and "impurity" is how mixed it is
WHAT. A node is a group of data points sitting together. Picture a bag holding 10 marbles: 5 magenta (call them "positive") and 5 violet ("negative"). We want a single number that says "how mixed is this bag?"
WHY a number at all? A tree's whole job is to sort mixed things into clean piles. To reward good sorting, we first need a ruler that measures messiness. That ruler is called impurity, written , where names the node (think " for the spot in the tree").
PICTURE. The bag on the left is a 50/50 mix — maximally confusing. The bag on the right is all one colour — perfectly clean, impurity zero.

Step 2 — Gini impurity: the chance two blind draws disagree
WHAT. We turn "mixedness" into a formula. Reach into the bag twice at random. Gini impurity is the probability the two marbles you pull are different colours.
WHY this definition and not, say, just "count colours"? Because probability-of-disagreement is smooth and honest: a bag that is 90/10 is less mixed than a 50/50 bag, and this measure captures that ordering automatically. A pure bag can never produce a disagreement, so it scores — exactly what we want.
Building the formula from the picture. Pick class first (probability ), then pick something that is not (probability ). That pair-disagreement for class is . Sum over all classes:
WHY does this simplify to ? Distribute the product inside the sum: . Now use the fact from Step 1 that the proportions add to one, . So the first piece is just , leaving
PICTURE. The curve of for one class: a hill peaking at (most confusing) and touching zero at and (pure).

For our root bag: . Highest possible for two classes — makes sense, it's a perfect 50/50 mess.
Step 3 — A split cuts the bag; each child gets its own impurity
WHAT. A split asks one yes/no question about a feature (e.g. " some value?") and pours the marbles into a left child and a right child .
WHY split? To make the piles cleaner than the parent. Using the example from the parent note, splitting on gives Left = 4 marbles (4 magenta, 0 violet) and Right = 6 marbles (1 magenta, 5 violet).
PICTURE. The 10-marble parent pours down two pipes into two smaller, cleaner bags.

Compute each child's Gini (using Step 2):
- : the left bag is pure — all magenta. A tree loves this.
- : still a little mixed (one stray magenta), but far cleaner than the parent's .
Step 4 — Combine the children by weighting: big bags matter more
WHAT. We need one "impurity after splitting" number to compare against the parent's . We average the two children — but a weighted average, weighting each child by the fraction of marbles it holds.
WHY weight, not a plain average? A plain average would treat a bag of 6 marbles the same as a bag of 4. But cleaning 6 marbles is more valuable than cleaning 4. The fraction is exactly "what share of the parent's marbles landed here."
Here (parent count), , :
PICTURE. Two bars whose widths equal the sample fractions ( and ) and whose heights are the child impurities; the weighted average is the area-balanced level line.

Step 5 — Impurity decrease : how much mess this split removed
WHAT. Subtract the after-impurity from the before-impurity. This drop, , is the split's actual cleaning work.
WHY subtract? "Before minus after" is the universal way to measure improvement. Big positive number = a lot of mess removed = a great question to ask.
Notice the two child terms are added (they are the weighted average from Step 4), then that whole sum is subtracted from the parent — that is why the big bracket carries a plus sign inside.
Why is never negative? (the picture behind it). Look back at the hill in Step 2: impurity is a concave curve — it bulges upward. Concave means the curve always sits above any straight line connecting two of its points. The "after" impurity is exactly a weighted average of two points on that curve (the two children), i.e. a point on the straight chord between them. The "before" impurity sits at the mixed proportion in between, which lies on the curve, hence above the chord. Curve-point (before) chord-point (after) means . This "curve above its own chord" fact is Jensen's inequality, and it is the entire reason a binary split can never raise weighted impurity. Worst case the two children have the same proportion as the parent — chord meets curve — and .
PICTURE. A vertical thermometer: the tall column, the shorter column, and the green gap between them labelled .

Step 6 — Arrival weight : splits that touch everyone count more
WHAT. Multiply by — the fraction of all the dataset's marbles ( from Step 1) that ever reach this node . Our root split sees all 10 of 10, so .
WHY? Deep down in a tree, a split might only see a handful of marbles. Cleaning 3 marbles is locally nice but globally minor. The arrival weight discounts deep, small splits so they can't outrank a decisive question near the top.
The concrete node (all raw numbers shown). Take a deeper node, reached by only of the marbles: 2 magenta and 1 violet. Split on feature into Left (pure) and Right (pure).
- Parent impurity: .
- Children: both pure, .
- After: , so .
Now apply the arrival weight and compare against from Steps 1–5:
's split was locally sharper (it made both children perfectly pure!) yet ranks below , because 's question shaped every marble in the dataset. That is the entire reason exists. (The parent note rounds this to and ; we show the exact node here.)
PICTURE. The tree drawn top-down: the root split fat and near the top (), the split small and deep (), each labelled with its contribution.

Step 7 — Sum over every node a feature splits on → feature importance
WHAT. A single feature may be used at several nodes. Add up all its contributions:
Read term by term: for every node whose split uses feature , take its arrival weight times its cleanup , and sum. That single number is the feature's Mean Decrease in Impurity (MDI) importance.
WHY sum? Importance is the total work a feature did across the whole tree, not its work at one lucky node.
Normalizing (so importances are comparable and add to 1):
Here the index in the denominator runs over every feature in the dataset (including itself) — so the denominator is the grand total importance of all features, and dividing rescales every feature onto a common 0-to-1 scale that sums to .
For our two features (using the rounded parent-note figures , ): total , so
PICTURE. A normalized bar chart: at , at , summing to 1.

Step 8 — The degenerate & edge cases (never leave the reader stranded)
Each of these can happen; here is what the formula does in every one. The four panels below are labelled A, B, C, D to match the four examples that follow.

Step 9 — Regression version: same story, swap the ruler
WHAT. For regression the marbles carry numbers , not colours. Impurity becomes variance about the node mean :
Here is the node's best single guess; the leftover squared error is the "mess."
WHY the same machinery works. Steps 3–7 never assumed which ruler we used. Swap Gini for variance and every equation stands. Using the parent's example , :
- Parent variance:
- Split into (mean 3, var ) and (mean 7, var )
- Weighted after:
- — a big drop, so this feature separates the -values cleanly.
PICTURE. A number line: the spread-out parent values, then two tight clusters, with the shrinking variance shown as narrowing bands.

The one-picture summary
Everything above, compressed: a bag flows down a split into two (or more) weighted children, the impurity drop is measured, scaled by arrival weight , and summed across all nodes using each feature — for both Gini (colours) and variance (numbers).

Recall Feynman retelling of the whole walkthrough
You have a bag of mixed marbles. Impurity is a mess-meter: 50/50 is maximum mess, all-one-colour is zero mess. You ask a yes/no question (a feature split) and pour the marbles into two smaller bags. Measure the mess in each new bag, but weight bigger bags more (cleaning 6 marbles beats cleaning 4). "Old mess minus new (weighted) mess" is — how much this question cleaned up; it can never be negative because the mess-meter is a hump-shaped curve and the split's result lies on the straight line under the hump (Jensen's inequality). Then scale that by how many of all the marbles ever reach that question — a question near the top touches everyone and counts big, a deep question touches few and counts small. Add up a feature's score over every place it was asked → its importance (MDI). If a question has more than two answers, just add up more child bags — same rule. Watch out: questions with tons of possible answers (like exact weight) can look important by memorizing noise, and a feature's identical twin gets starved because whoever splits first grabs all the credit. For predicting numbers, swap the mess-meter from "colours disagree" to "variance about the average" — same game, same math.
Connections
- Decision Trees — every step here reuses the tree's own splitting impurity.
- Gini vs Entropy — Step 2's ruler could be entropy instead of Gini.
- Random Forests — average Step 7's number over many trees for stable importance.
- Gradient Boosting — same split-based gain, just from boosted trees.
- Permutation Importance — the unbiased fix for Step 8's traps.
- SHAP Values — fair credit-sharing that cures the correlated-twin problem.
- Bias-Variance Tradeoff — why training-set MDI overestimates.