๐๐ฒ๐บ๐๐๐๐ถ๐ณ๐๐ถ๐ป๐ด ๐๐ถ๐ป๐ถ ๐๐บ๐ฝ๐๐ฟ๐ถ๐๐: ๐๐ผ๐ ๐๐ฒ๐ฐ๐ถ๐๐ถ๐ผ๐ป ๐ง๐ฟ๐ฒ๐ฒ๐ ๐๐ต๐ผ๐ผ๐๐ฒ ๐๐ต๐ฒ ๐ข๐ฝ๐๐ถ๐บ๐ฎ๐น ๐ฆ๐ฝ๐น๐ถ๐
A ๐ฎ๐ข๐ต๐ฉ๐ฆ๐ฎ๐ข๐ต๐ช๐ค๐ข๐ญ ๐ข๐ฏ๐ฅ ๐ฑ๐ณ๐ข๐ค๐ต๐ช๐ค๐ข๐ญ ๐ฅ๐ฆ๐ฆ๐ฑ ๐ฅ๐ช๐ท๐ฆ ๐ช๐ฏ๐ต๐ฐ ๐๐๐๐ ๐ฅ๐ฆ๐ค๐ช๐ด๐ช๐ฐ๐ฏ ๐ต๐ณ๐ฆ๐ฆ๐ด, ๐๐ช๐ฏ๐ช ๐ช๐ฎ๐ฑ๐ถ๐ณ๐ช๐ต๐บ ๐ฅ๐ฆ๐ณ๐ช๐ท๐ข๐ต๐ช๐ฐ๐ฏ๐ด, ๐ด๐ฑ๐ญ๐ช๐ต ๐ฆ๐ท๐ข๐ญ๐ถ๐ข๐ต๐ช๐ฐ๐ฏ, ๐ข๐ฏ๐ฅ ๐๐บ๐ต๐ฉ๐ฐ๐ฏ ๐ช๐ฎ๐ฑ๐ญ๐ฆ๐ฎ๐ฆ๐ฏ๐ต๐ข๐ต๐ช๐ฐ๐ฏ. โโโโโโโโโโโโโโโโโโโโ
At the heart of every decision tree classifier lies a fundamental question: Which feature and threshold will split the data into the cleanest, most distinct subsets?
In algorithms like CART (Classification and Regression Trees), this decision is driven by a metric known as Gini Impurity. While modern ensemble methods like Random Forests and Gradient Boosted Trees obscure the math behind abstractions, understanding how Gini Impurity drives node splits is essential for diagnosing tree behavior, controlling overfitting, and interpreting classification boundaries.
โโโโโโโโโโโโโโโโโโโโ
๐ญ. ๐ช๐ต๐ฎ๐ ๐๐ ๐๐ถ๐ป๐ถ ๐๐บ๐ฝ๐๐ฟ๐ถ๐๐?
Gini Impurity measures the likelihood that a randomly chosen element from a dataset would be incorrectly labeled if it were randomly labeled according to the class distribution in the subset.
For a node ๐ containing samples across ๐พ distinct classes, let ๐_๐ denote the proportion of training instances belonging to class ๐:
๐_๐ = (1 / ๐_๐) โ_{๐ โ ๐ _๐} ๐(๐ฆแตข = ๐)
The Gini Impurity ๐ผ_๐บ(๐) is defined as:
๐ผ_๐บ(๐) = โ{๐=1..๐พ} ๐_๐(1 โ ๐_๐) = 1 โ โ{๐=1..๐พ} ๐_๐ยฒ
๐๐ผ๐๐ป๐ฑ๐ฎ๐ฟ๐ ๐๐ผ๐ป๐ฑ๐ถ๐๐ถ๐ผ๐ป๐: โข ๐ ๐ฎ๐ ๐ถ๐บ๐๐บ ๐ฃ๐๐ฟ๐ถ๐๐ (๐ผ_๐บ = 0): Every sample in node ๐ belongs to a single class (๐_๐ = 1, and ๐_๐ = 0 for all ๐ โ ๐). The node is pure and requires no further splitting. โข ๐ ๐ฎ๐ ๐ถ๐บ๐๐บ ๐๐บ๐ฝ๐๐ฟ๐ถ๐๐: Samples are equally distributed among all ๐พ classes (๐_๐ = 1 / ๐พ for all ๐): ๐ผ_{๐บ, max} = 1 โ โ (1 / ๐พ)ยฒ = 1 โ 1 / ๐พ For binary classification (๐พ = 2), maximum impurity is 0.5. For ๐พ = 3, it is 0.667.
โโโโโโโโโโโโโโโโโโโโ
๐ฎ. ๐๐๐ฎ๐น๐๐ฎ๐๐ถ๐ป๐ด ๐ฆ๐ฝ๐น๐ถ๐๐: ๐ช๐ฒ๐ถ๐ด๐ต๐๐ฒ๐ฑ ๐๐ถ๐ป๐ถ ๐๐บ๐ฝ๐๐ฟ๐ถ๐๐ ๐ฅ๐ฒ๐ฑ๐๐ฐ๐๐ถ๐ผ๐ป
A decision tree evaluates every candidate feature ๐ and threshold ๐ก, splitting node ๐ into left (๐_๐ฟ) and right (๐_๐ ) subsets: โข ๐_๐ฟ(๐, ๐ก) = { (๐ฑ, ๐ฆ) โ ๐ โฃ ๐ฅ_๐ โค ๐ก } โข ๐_๐ (๐, ๐ก) = { (๐ฑ, ๐ฆ) โ ๐ โฃ ๐ฅ_๐ > ๐ก }
The quality of the split is evaluated using the Weighted Gini Impurity:
๐บ_split(๐, ๐ก) = (๐_๐ฟ / ๐_๐) ๐ผ_๐บ(๐_๐ฟ) + (๐_๐ / ๐_๐) ๐ผ_๐บ(๐_๐ )
Equivalently, the tree maximizes the Gini Gain (ฮ๐ผ_๐บ):
ฮ๐ผ_๐บ(๐, ๐ก) = ๐ผ_๐บ(๐) โ ๐บ_split(๐, ๐ก)
The chosen split satisfies: (๐*, ๐ก*) = arg min_{๐, ๐ก} ๐บ_split(๐, ๐ก)
โโโโโโโโโโโโโโโโโโโโ
๐ฏ. ๐ฆ๐๐ฒ๐ฝ-๐ฏ๐-๐ฆ๐๐ฒ๐ฝ ๐ก๐๐บ๐ฒ๐ฟ๐ถ๐ฐ๐ฎ๐น ๐๐ ๐ฎ๐บ๐ฝ๐น๐ฒ
Suppose a parent node contains 10 samples: 6 Class A and 4 Class B.
Get Vinaitheerthan Renganathanโs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
๐ฆ๐๐ฒ๐ฝ ๐ญ: Calculate Parent Gini Impurity ๐_๐ด = 6 / 10 = 0.6, ๐_๐ต = 4 / 10 = 0.4 ๐ผ_๐บ(Parent) = 1 โ (0.โถยฒ + 0.โดยฒ) = 1 โ (0.36 + 0.16) = 0.48
๐ฆ๐๐ฒ๐ฝ ๐ฎ: Candidate Split 1 divides the data into: โข Left Node (4 samples): 4 Class A, 0 Class B โ ๐ผ_๐บ(Left) = 1 โ (1.โฐยฒ + 0.โฐยฒ) = 0.0 โข Right Node (6 samples): 2 Class A, 4 Class B โ ๐ผ_๐บ(Right) = 1 โ ((2/6)ยฒ + (4/6)ยฒ) = 1 โ 0.555 = 0.444
๐บ_split = (4 / 10)(0.0) + (6 / 10)(0.444) = 0.266 ฮ๐ผ_๐บ = 0.48 โ 0.266 = 0.214
๐ฆ๐๐ฒ๐ฝ ๐ฏ: Candidate Split 2 divides the data into: โข Left Node (5 samples): 3 Class A, 2 Class B โ ๐ผ_๐บ(Left) = 1 โ (0.โถยฒ + 0.โดยฒ) = 0.48 โข Right Node (5 samples): 3 Class A, 2 Class B โ ๐ผ_๐บ(Right) = 1 โ (0.โถยฒ + 0.โดยฒ) = 0.48
๐บ_split = (5 / 10)(0.48) + (5 / 10)(0.48) = 0.48 ฮ๐ผ_๐บ = 0.48 โ 0.48 = 0.000
The tree selects Split 1 because it yields a significant reduction in impurity (0.266 vs 0.480).
โโโโโโโโโโโโโโโโโโโโ
๐ฐ. ๐๐ถ๐ป๐ถ ๐๐บ๐ฝ๐๐ฟ๐ถ๐๐ ๐๐. ๐๐ป๐๐ฟ๐ผ๐ฝ๐ (๐๐ป๐ณ๐ผ๐ฟ๐บ๐ฎ๐๐ถ๐ผ๐ป ๐๐ฎ๐ถ๐ป)
โข Gini Impurity (1 โ โ ๐_๐ยฒ): Computationally faster because it avoids logarithmic operations. It tends to favor isolating the most frequent class quickly. โข Entropy (โโ ๐_๐ logโ(๐_๐)): Slower due to log calls. It penalizes mixed distributions somewhat more heavily, often yielding slightly more balanced subtrees.
In practice, both criteria make identical split choices 97โ99% of the time, making Gini the preferred default in high-throughput implementations like scikit-learn.
โโโโโโโโโโโโโโโโโโโโ
๐ฑ. ๐ฃ๐๐๐ต๐ผ๐ป ๐๐บ๐ฝ๐น๐ฒ๐บ๐ฒ๐ป๐๐ฎ๐๐ถ๐ผ๐ป
Python
import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_text# 1. Define synthetic feature and binary target
X = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
y = np.array([0, 0, 0, 0, 1, 1, 0, 0, 1, 1])# 2. Fit CART decision tree (single depth stump)
clf = DecisionTreeClassifier(criterion="gini", max_depth=1, random_state=42)
clf.fit(X, y)# 3. Print learned split rule
print(export_text(clf, feature_names=["Feature_X"]))# 4. Verification function
def calculate_gini(labels):
_, counts = np.unique(labels, return_counts=True)
probs = counts / len(labels)
return 1.0 - np.sum(probs ** 2)print(f"Parent Gini Impurity: {calculate_gini(y):.4f}")
โโโโโโโโโโโโโโโโโโโโ
๐ฒ. ๐๐ฒ๐ ๐ง๐ฎ๐ธ๐ฒ๐ฎ๐๐ฎ๐๐ ๐ณ๐ผ๐ฟ ๐ฃ๐ฟ๐ฎ๐ฐ๐๐ถ๐๐ถ๐ผ๐ป๐ฒ๐ฟ๐
- Pre-pruning is Essential: Without depth limits or leaf constraints, trees partition until every node reaches ๐บ = 0, capturing dataset noise.
- High-Cardinality Bias: Categorical features with many levels can unfairly minimize Gini impurity through sheer partition variety.
- Foundation of Ensembles: The speed of Gini impurity calculations allows ensembles like Random Forests to evaluate thousands of candidate feature splits across hundreds of trees in seconds.




