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.
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:
Pick a random item. Probability it truly belongs to class i is ==pi==.
Why?pi is defined as that class's share of the node.
Independently pick a random label from the same distribution. Probability that label is class i is also pi.
We are wrong whenever the true class (i) and the guessed label (j) differ: i=j.
Probability of a match on class i is pi⋅pi=pi2.
Why square? Two independent draws both landing on i.
Total probability of a correct match (any class): ∑ipi2.
Therefore probability of a mistake:
G=1−∑i=1Kpi2
Rewrite algebraically to see the "per-class error" view:
1−∑ipi2=∑ipi−∑ipi2=∑ipi(1−pi)Why this matters? Each term pi(1−pi) = (chance item is class i) × (chance our guess is NOT class i). It reads as "expected misclassification per class."
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.
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=1−∑pi2.
Formula yaad rakhne ka trick: "One minus sum of squares". Har class ka proportion pi nikaalo, square karo, sabko add karo, aur 1 se minus kardo. Jab ek hi class dominate karti hai to ∑pi2 bada ho jaata hai, isliye G chhota (pure) ho jaata hai. Jab sab equal hote hain, tab G 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/N), kyunki usme zyada data hai. Jo split Gsplit 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.