2.3.3Tree-Based & Instance Methods

Gini impurity

1,891 words9 min readdifficulty · medium5 backlinks

A decision tree splits data to make each resulting group as "pure" as possible — ideally each leaf contains only one class. Gini impurity is a number that measures how impure (mixed-up) a node is. Lower = purer.

The Core Idea

WHY this formula? (Derivation from scratch)

WHAT we want: the probability that a randomly picked item is labelled incorrectly if we assign it a random class label drawn from the node's own distribution.

HOW we build it step by step:

  1. Pick a random item. Probability it truly belongs to class ii is ==pi====p_i==. Why? pip_i is defined as that class's share of the node.

  2. Independently pick a random label from the same distribution. Probability that label is class ii is also pip_i.

  3. We are wrong whenever the true class (ii) and the guessed label (jj) differ: iji \neq j. Probability of a match on class ii is pipi=pi2p_i \cdot p_i = p_i^2. Why square? Two independent draws both landing on ii.

  4. Total probability of a correct match (any class): ipi2\sum_i p_i^2.

  5. Therefore probability of a mistake: G=1i=1Kpi2G = 1 - \sum_{i=1}^{K} p_i^2

  6. Rewrite algebraically to see the "per-class error" view: 1ipi2=ipiipi2=ipi(1pi)1 - \sum_i p_i^2 = \sum_i p_i - \sum_i p_i^2 = \sum_i p_i(1-p_i) Why this matters? Each term pi(1pi)p_i(1-p_i) = (chance item is class ii) × (chance our guess is NOT class ii). It reads as "expected misclassification per class."

Figure — Gini impurity

Worked Examples

Common Mistakes

Recall Feynman: explain to a 12-year-old

You have a box of jelly beans, some red, some green. You play a game: pull one bean, hide its color, then guess its color by pulling another random bean and copying that one's color. If the box is all red, you always win. If it's half-half, you lose a lot. Gini impurity is just "how often you lose this game." A smart tree keeps splitting the box into smaller boxes so each box is nearly all one color — because then you almost never lose.

Active Recall

What is the Gini impurity formula for a node?
G=1i=1Kpi2=ipi(1pi)G = 1 - \sum_{i=1}^K p_i^2 = \sum_i p_i(1-p_i)
What does Gini impurity literally measure (probabilistic meaning)?
The probability of misclassifying a random item if you guess its class using a randomly drawn label from the node's own class distribution.
What is the Gini impurity of a perfectly pure node?
0
What is the maximum Gini impurity for K classes, and when?
11/K1 - 1/K, achieved when all classes are equally likely (uniform pi=1/Kp_i = 1/K).
Max Gini for a 2-class node?
0.5 (at 50/50 split).
How do we score a candidate split?
Weighted average impurity of children cNcNGc\sum_c \frac{N_c}{N}G_c; the tree maximizes Gini gain = GparentGsplitG_{parent} - G_{split}.
Why weight children by Nc/NN_c/N instead of a plain average?
Larger children contribute more total misclassifications; unweighted sums ignore child sizes and inflate with more branches.
Why is a uniform distribution the worst (highest Gini)?
Because pi2\sum p_i^2 is minimized when probability is spread evenly, so G=1pi2G=1-\sum p_i^2 is maximized.
Gini vs Entropy — practical difference?
Both 0 at purity, max at uniform; Gini avoids computing log\log so it's cheaper and usually gives near-identical trees.
Does Gini=0 on training data mean a good model?
No — it often signals overfitting/memorization; purity is the split criterion, not the generalization target.

Connections

  • Decision Trees — Gini is the default split criterion in CART/scikit-learn.
  • Entropy and Information Gain — the alternative impurity measure (plogp-\sum p\log p).
  • Overfitting and Pruning — why we don't chase Gini=0.
  • Random Forests — average many Gini-grown trees.
  • Classification vs Regression Trees — regression uses variance/MSE instead of Gini.
  • Gini Coefficient (Economics) — different concept, same name (don't confuse).

Concept Map

aims for

measured by

defines

computed as

rewritten as

averaged over children

weighted by Nc over N

G_parent minus G_split

chooses split for

G equals 0 when

Decision Tree

Node Purity

Gini Impurity G

Random guess mistake rate

G equals 1 minus sum pi squared

Sum pi times 1 minus pi

Weighted Gini G_split

Gini Gain

Greedy Best Split

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Gini impurity ka matlab bilkul simple hai: ek node (data ka group) kitna "mixed-up" hai, yeh batata hai. Socho ek dabbe mein red aur green balls hain. Tum ek ball uthate ho, uska color chhupa dete ho, aur guess karne ke liye ek aur random ball nikaal kar uska color copy karte ho. Agar saare balls red hain to tum kabhi galat nahi honge — impurity 0. Agar aadhe-aadhe hain to bahut baar galat honge — impurity maximum (0.5 for 2 classes). Bas yehi probability of mistake hi Gini hai: G=1pi2G = 1 - \sum p_i^2.

Formula yaad rakhne ka trick: "One minus sum of squares". Har class ka proportion pip_i nikaalo, square karo, sabko add karo, aur 1 se minus kardo. Jab ek hi class dominate karti hai to pi2\sum p_i^2 bada ho jaata hai, isliye GG chhota (pure) ho jaata hai. Jab sab equal hote hain, tab GG sabse zyada.

Decision tree yeh karti hai: har possible split try karti hai, aur dekhti hai konsa split children ko sabse zyada pure banata hai. Isko measure karne ke liye hum children ki Gini ka weighted average lete hain — bade child ko zyada weight (Nc/NN_c/N), kyunki usme zyada data hai. Jo split GsplitG_{split} ko sabse kam karta hai (yaani gain sabse zyada), wahi chunte hain.

Ek important baat: Gini ko zabardasti 0 tak le jaana hamesha accha nahi. Agar training data pe har leaf perfectly pure ho gayi, iska matlab tree ne noise rat liya (overfitting). Isliye hum depth limit karte hain ya pruning karte hain. Gini sirf split choose karne ka tool hai, final goal generalization hai — yeh farak yaad rakhna exam aur interview dono mein.

Go deeper — visual, from zero

Test yourself — Tree-Based & Instance Methods

Connections