Calculate the Gini impurity index used for evaluating decision tree splits.
The Gini index measures node impurity by estimating the probability that two randomly drawn samples from the node belong to different classes: Gini = 1 − Σ p_i², where p_i is the proportion of class i in the node. A Gini of 0 means the node is perfectly pure (all samples belong to one class), while higher values indicate a more evenly mixed distribution — the maximum, 1 − 1/n for n classes, occurs at a perfectly uniform split. CART (Classification and Regression Trees), used by scikit-learn's default DecisionTreeClassifier, uses Gini index rather than entropy as its default splitting criterion because it's slightly cheaper to compute (no logarithms).
Gini = 1 - sum(p_i ^ 2)
Both measure node impurity and tend to select similar splits in practice, but Gini uses squared proportions (1 − Σp²) while entropy uses logarithms (−Σp·log2(p)); Gini is computationally cheaper since it avoids logarithm calculations, which is why CART/scikit-learn use it as the default criterion.
For n classes with a perfectly uniform distribution (each class equally represented), the maximum Gini is 1 − 1/n — for 2 classes that's 0.5, and it approaches 1 as the number of classes grows large.
Gini index avoids computing logarithms, making it marginally faster to evaluate across many candidate splits during tree construction, and in practice it tends to produce trees very similar in quality to those built with entropy-based information gain.