Information Gain

Information Gain measures how much a split reduces entropy - it's the metric a Decision Tree uses to decide which feature and threshold to split on at each node.

The Information Gain Formula

InformationGain = Entropy(parent) - WeightedAverageEntropy(children)

# A higher information gain means the split does a better job
# of separating the classes.

Calculating Information Gain in Python

import numpy as np

def entropy(labels):
    values, counts = np.unique(labels, return_counts=True)
    probs = counts / counts.sum()
    return -np.sum(probs * np.log2(probs))

def information_gain(parent, left, right):
    weight_left = len(left) / len(parent)
    weight_right = len(right) / len(parent)
    return entropy(parent) - (weight_left * entropy(left) + weight_right * entropy(right))

parent = [0, 0, 0, 1, 1, 1, 1, 1]
left   = [0, 0, 0]
right  = [1, 1, 1, 1, 1]

print("Information Gain:", information_gain(parent, left, right))

How the Tree Uses It

At each node, the Decision Tree evaluates information gain for every possible feature and split point, then picks whichever split produces the highest gain.

Information Gain can be biased toward features with many distinct values - the Gain Ratio is a variant that corrects for this bias.

Coming Up Next

Next, you'll learn about the Greedy Approach that Decision Trees follow when choosing splits.

Working Through the Numbers by Hand

It helps to calculate information gain once without code, so the formula stops feeling abstract. Take the same parent node of eight labels: three 0s and five 1s.

  1. Parent entropy: the class proportions are 0.375 and 0.625. Entropy = −(0.375 × log2 0.375 + 0.625 × log2 0.625) ≈ 0.954 bits.
  2. Perfect split: left holds only 0s and right holds only 1s. Both children have entropy 0, so the gain is the full 0.954.
  3. Weak split: now suppose left = [0, 0, 1, 1] and right = [0, 1, 1, 1]. Left entropy is 1.0, right entropy is about 0.811.
  4. Weighted child entropy: each side holds half the rows, so 0.5 × 1.0 + 0.5 × 0.811 ≈ 0.906.
  5. Gain: 0.954 − 0.906 ≈ 0.049. The split barely helps, so the tree would prefer another feature.

This contrast is the whole idea: a useful feature pushes each child towards a single class, while a useless one leaves the children as mixed as the parent.

Choosing a Threshold for Numeric Features

For a numeric column such as age, the tree sorts the values and tests candidate cut points, usually midpoints between neighbouring distinct values. Each cut point creates a left and right group, and the one with the highest gain wins. The snippet below reuses the information_gain function from above.

ages   = np.array([22, 25, 31, 35, 41, 48, 52, 60])
labels = np.array([0,  0,  0,  1,  1,  1,  1,  1])

best = None
for cut in (ages[:-1] + ages[1:]) / 2:
    left, right = labels[ages <= cut], labels[ages > cut]
    gain = information_gain(labels, left, right)
    if best is None or gain > best[1]:
        best = (cut, gain)

print("Best threshold:", best[0], "gain:", round(best[1], 3))

In scikit-learn you do not write this loop yourself. Setting the criterion is enough:

from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(criterion="entropy", max_depth=4, random_state=42)
tree.fit(X_train, y_train)

Information Gain, Gini and Gain Ratio Compared

MeasureIdeaTypical use
Information gainDrop in entropy after a splitID3, C4.5, scikit-learn with entropy
Gini impurityChance of mislabelling a random sampleCART, scikit-learn default
Gain ratioGain divided by split informationC4.5, columns with many categories

Gini and entropy usually pick very similar splits. Gini is slightly cheaper to compute because it avoids logarithms, which is why many libraries use it by default.

Mistakes to Watch For

  • Keeping ID-like columns: a customer ID splits every row into its own pure leaf, giving a huge gain and zero real-world value. Drop such columns.
  • Chasing gain at every depth: very deep trees memorise noise. Limit max_depth or min_samples_leaf.
  • Reading gain as feature importance on its own: one high-gain split near the root does not prove a feature matters most overall; check importance across the whole tree or with permutation importance.

Takeaway: compute parent entropy, compute weighted child entropy, subtract, and repeat for every candidate split. The largest difference wins.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course