How do decision trees split? Walk me through Gini impurity vs information gain.
Written by the mlprep team · Updated
How does a decision tree decide where to split? What's the difference between Gini impurity and information gain, and which should you use?
The splitting objective#
At each node, a decision tree searches over all features and all threshold values to find the split that maximally reduces impurity. For a node t with split into children t_L and t_R:
ΔImpurity = Impurity(t) - [|t_L|/|t|·Impurity(t_L) + |t_R|/|t|·Impurity(t_R)]
The tree greedily picks the feature-threshold pair maximizing ΔImpurity. This is a local greedy search — not globally optimal.
Gini impurity#
Gini(t) = 1 - ∑_c p_c²
where p_c is the fraction of samples of class c at node t.
Interpretation: the probability of misclassifying a randomly drawn sample if you assigned it a label drawn from the node's class distribution. Perfect node (all one class): Gini = 0. Maximally impure node (equal mix): Gini = 1 - 1/K where K is number of classes (0.5 for binary).
Gini favors the most frequent class — it penalizes nodes where the dominant class is weak.
Information gain (entropy)#
Entropy(t) = -∑_c p_c · log₂(p_c)
Maximum entropy (all classes equal) = log₂(K). Zero entropy = pure node.
Information gain IG = Entropy(parent) - weighted Entropy(children). Equivalent to selecting splits that maximize mutual information between the feature and the label.
Which to use?#
In practice, they almost never produce meaningfully different trees. Gini is slightly faster to compute (no log). Information gain can prefer splits that create more balanced partitions (log penalizes extremes more than Gini). For most problems, the choice is irrelevant.
CART algorithm (sklearn) uses Gini by default. ID3/C4.5 uses information gain.
Regression trees#
For regression, the splitting criterion is variance reduction:
ΔVariance = Var(t) - [|t_L|/|t|·Var(t_L) + |t_R|/|t|·Var(t_R)]
Equivalent to minimizing MSE within each leaf.
Overfitting and tree depth#
An unconstrained tree will grow until every leaf has one sample — perfect training accuracy, terrible generalization. Control overfitting with:
max_depth: hard cap on tree depthmin_samples_split: minimum samples to consider splitting a nodemin_samples_leaf: minimum samples in any leafmax_features: consider only a random subset of features per split (key for random forests)- Post-pruning: grow full tree, then prune back branches that don't improve validation performance by more than a threshold (cost-complexity pruning, sklearn's
ccp_alpha)
Why decision trees are weak learners#
Single trees are high-variance — they overfit unless heavily constrained. They're also piecewise constant (step functions) which limits expressiveness. Power comes from ensembling: random forests (parallel trees, averaging) or gradient boosting (sequential trees, error correction). See ensemble-methods question.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Decision trees split greedily to maximize impurity reduction. Gini impurity = 1 - ∑p²_c (probability of mislabeling). Information gain = entropy reduction (uses log). Both produce nearly identical trees in practice; Gini is faster. Regression trees minimize variance. Prevent overfitting via max_depth, min_samples_leaf, or cost-complexity pruning. Trees are high-variance weak learners — power comes from ensembling (bagging, boosting).
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- What is cost-complexity pruning and how does it differ from setting max_depth?
- How does a decision tree handle numeric vs categorical features differently?
- Why do decision trees partition the feature space into axis-aligned rectangles? What does this mean for modeling circular or diagonal decision boundaries?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions