Gini Impurity
A measure of class impurity in a decision tree node, an alternative to entropy. The score is based on class shares: the more even the distribution, the higher the value.
How it is calculated
where \(p_k\) is the share of objects of class \(k\) and \(K\) is the number of classes. The shares are squared, summed, and subtracted from one.
One interpretation: the Gini impurity equals the probability that a randomly chosen object is assigned a class other than its own — provided the class is drawn at random from the class distribution in the node.
Values for two classes
| Class distribution | Gini impurity | Entropy |
|---|---|---|
| 10 and 0 | 0 | 0 |
| 9 and 1 | 0.18 | ≈ 0.469 |
| 7 and 3 | 0.42 | ≈ 0.882 |
| 5 and 5 | 0.5 | 1 |
For two classes the maximum is 0.5. For a pure node the Gini impurity is zero.
Comparison with entropy
Both measures show the same qualitative direction: they grow as classes mix and fall to zero for a pure node. The numerical values differ because the formulas are different; the choice of measure depends on the implementation.
The decrease in impurity after a split is computed in the same way as the information gain: the value before the split is compared with the weighted value of the child nodes. The Gini impurity is often used in tree ensembles, for example in a random forest.