Gini and entropy
Quick answer
Classification split criteria measure how mixed the class labels are. Gini impurity and entropy are zero in a pure node and larger when the classes are more evenly mixed. A candidate split is useful if it reduces weighted impurity. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Compute weighted Gini decrease
from fractions import Fraction as Fdef g(y): return 1-sum(F(y.count(c),len(y))**2 for c in set(y))l=[0]; r=[0,1,1]; n=len(l+r)print(g(l+r)-(len(l)*g(l)+len(r)*g(r))/n)Expected output:
1/6The small pure child is weighted by one quarter.
How it works
For class proportions p_k, Gini G=1−sum(p_k²). Entropy H=−sum(p_k log2(p_k)), treating zero terms as zero. The two-class node [0,0,1,1] has G=0.5 and H=1 bit. A split into [0,0] and [1,1] has weighted child impurity zero. For left and right child sizes n_L and n_R, information gain is parent impurity minus [(n_L/n)*I_L+(n_R/n)*I_R]. For Gini call this impurity decrease. For example, a pure singleton and a 3-row child with labels [0,1,1] have weighted Gini 1/3, so the decrease is 1/6. Unweighted averaging of child impurities gives a misleading advantage to tiny leaves. Empty children are invalid splits. With sample weights use summed weights instead of counts. In scikit-learn the criterion entropy and log_loss use Shannon information; Gini uses squared proportions. Equal gains can lead to different valid trees. Impurity decrease is a training objective, not validation accuracy. Impurity-based feature importance sums weighted decreases across nodes and can favor high-cardinality features that offer more candidate splits. A held-out permutation analysis is a complementary measure but correlated features can obscure individual contributions.
Verify node impurity for two criteria
from sklearn.tree import DecisionTreeClassifierX=[[0],[1],[2],[3]]; y=[0,0,1,1]for c in ["gini","entropy"]: m=DecisionTreeClassifier(criterion=c,max_depth=1,random_state=7).fit(X,y) print(c,m.tree_.impurity.round(5).tolist())Expected output:
gini [0.5, 0.0, 0.0]entropy [1.0, 0.0, 0.0]The root is mixed and the chosen split creates pure leaves.
Common mistakes
Do not average the child scores equally when their weights differ. Avoid log2(0) by skipping zero terms. A pure training leaf does not prove the next unseen sample has the same label.
Practice and next steps
Compare splitting [0,0,0,1,1,1] after the second or third label. Compute weighted Gini decrease and choose the larger one. Complete the knowledge check and coding exercise. Next: Regression trees.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gini and entropy — knowledge check
Apply gini and entropy to new examples.
Gini of a balanced binary node?
Entropy of a pure node?
Which child weighting is appropriate for unweighted samples?
Why can high-cardinality noise look important?
Does zero training impurity guarantee perfect test accuracy?