Last updated 13 November 2026
What is information gain?
Information gain is how much a split reduces entropy in a decision tree. It equals the entropy of the parent node minus the size-weighted average entropy of the child nodes. A decision tree computes it for every candidate feature and splits on the one with the highest gain, since that split makes the children purest. On TensorTonic you implement information gain from scratch, then use it to find a decision tree's best split, with tests that check your output.
Building a Decision Tree involves asking a sequence of questions to split your data. But which question should you ask first?
Should you ask "Is it raining?" or "Is it humid?" first? To answer this, we need a way to measure how much "cleaner" (purer) the data becomes after a split.
Information Gain (IG) is exactly this measure. It quantifies the reduction in entropy (uncertainty) achieved by splitting on a specific feature. It is essentially Mutual Information between the feature and the label.
Intuition: The 20 Questions Game
Imagine playing "20 Questions." Your goal is to identify a secret object with yes/no questions.
Bad Question
"Is the object a banana?"
99% chance the answer is "No." You are left with almost the same uncertainty. Low Information Gain.
Good Question
"Is the object alive?"
Splits the world roughly in half. Regardless of the answer, you have eliminated 50% of possibilities. High Information Gain.
Decision Trees use Information Gain to find the "Is it alive?" questions, not the "Is it a banana?" questions.
Recap: Entropy as Impurity
Before we split, we calculate the entropy of the current dataset (the parent node). Entropy measures "impurity" or "disorder."
The formula
Information Gain = Entropy Before - Entropy After.
Since a split creates multiple child nodes, "Entropy After" is the weighted average of child entropies.
Entropy of parent node.
Large children matter more than small ones.
Entropy of child node v.
Two facts follow from the formula. Information gain is never negative, because the weighted average entropy of the children can never exceed the parent's. It reaches its maximum, , when every child is pure. Information gain is also the mutual information between the feature and the label, measured on the samples at that node.
Interactive: Split Visualization
Drag the split point to see how Information Gain changes. The best split creates the purest child nodes.
Recursive Partitioning
Build a Depth-2 Decision Tree. First split the root, then split the children.
Total Information Gain: 0.001 bits
(Sum of IG at each active node)
Worked calculation
Dataset: 10 examples, 5 Positive (+), 5 Negative (-). Testing feature "Windy" (True/False).
Step 1: Parent Entropy
Step 2: Split Data
- Windy=False (6 samples): 4 Pos, 2 Neg
- Windy=True (4 samples): 1 Pos, 3 Neg
Step 3: Child Entropies
Step 4: Information Gain
Knowing whether it is windy removes only 0.125 of the 1 bit of uncertainty, so “Windy” is a weak split. The code below repeats the calculation and compares it with a three-valued “Forecast” feature on the same 10 days, using both entropy and Gini impurity:
import numpy as np
def entropy(y):
_, counts = np.unique(y, return_counts=True)
p = counts / counts.sum()
return -np.sum(p * np.log2(p))
def gini(y):
_, counts = np.unique(y, return_counts=True)
p = counts / counts.sum()
return 1 - np.sum(p ** 2)
def gain(y, feature, impurity):
"""Parent impurity minus the size-weighted impurity of each child."""
children = sum((feature == v).mean() * impurity(y[feature == v])
for v in np.unique(feature))
return impurity(y) - children
# 10 days: label 1 = play, 0 = don't play
y = np.array([1, 1, 1, 1, 0, 0, 1, 0, 0, 0])
windy = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1])
forecast = np.array([0, 0, 0, 0, 1, 1, 0, 1, 1, 2]) # 0 sunny, 1 rain, 2 cloudy
for name, x in [("windy", windy), ("forecast", forecast)]:
print(f"{name:<9} info gain = {gain(y, x, entropy):.3f} bits "
f"gini gain = {gain(y, x, gini):.3f}")windy info gain = 0.125 bits gini gain = 0.083 forecast info gain = 1.000 bits gini gain = 0.500
Forecast separates the classes perfectly, so its gain equals the full parent entropy of 1 bit, and both criteria rank it above Windy. Keep the three-way split in mind for the limitations section below.
The ID3 Algorithm
ID3 (Iterative Dichotomiser 3) uses Information Gain to build trees greedily:
- Calculate H(S) for the current node.
- For every possible feature A, calculate IG(S, A).
- Select the feature with the highest Information Gain.
- Split the data using that feature.
- Recurse on each child until nodes are pure or max depth is reached.
ID3 only handles categorical features. Its successors C4.5 and CART split a numeric feature such as temperature with a threshold: sort the values, try a cut between each pair of neighbouring values, and keep the threshold with the highest gain. The interactive split above does exactly this for one feature.
Gini vs entropy: which should you use?
Scikit-Learn's CART algorithm uses Gini Impurity by default instead of Entropy. Why?
Entropy
Pro: Information-theoretically grounded.
Con: Slower (log computation).
Gini Impurity
Pro: Faster (only square).
Con: No information-theory interpretation.
| Entropy | Gini impurity | Information gain | |
|---|---|---|---|
| What it measures | Impurity of one node, in bits | Impurity of one node: the chance of mislabelling a random sample | How much a split lowers impurity |
| Formula | Parent impurity minus the size-weighted impurity of the children | ||
| Maximum (2 classes) | 1 bit at a 50/50 split | 0.5 at a 50/50 split | The parent's impurity, reached when every child is pure |
| Windy split above | Gain of 0.125 bits | Gain of 0.083 | Either number, depending on which impurity you use |
| Computation | Needs a logarithm per class | Only squares | One impurity per node, then a weighted average |
| Used by | ID3, C4.5; criterion="entropy" in scikit-learn | CART; scikit-learn's default | Every decision tree, as the split score |
The two scales differ, so compare gains within one criterion, never across them. The rankings almost always agree: Raileanu and Stoffel (Annals of Mathematics and Artificial Intelligence, 2004) found the two criteria choose different splits in only about 2% of cases. Gini is the common default because it skips the logarithm, and entropy is a reasonable choice when you want gains in bits that connect to the rest of information theory. Tree depth, minimum leaf size and pruning affect accuracy far more than this choice.
Gini Impurity vs Entropy
Compare the shape of Gini Impurity and Entropy. Notice how Gini is just a quadratic approximation of Entropy.
Entropy H(p)
Max at p=0.5 (1.0 bits)
Gini Impurity G(p)
Max at p=0.5 (0.5)
Limitations & Gain Ratio
The "User ID" Problem
Information Gain is biased towards features with many unique values.
If you split on "User ID" (unique for everyone), each child has 1 sample with H = 0. Maximum IG! But this is useless for generalization.
Solution: Gain Ratio (C4.5)
Divides by the "intrinsic information" of the split itself, penalizing splits with many branches.
For the Forecast feature, the branches hold 5, 4 and 1 of the 10 days, so its split information is about 1.36 bits and its gain ratio is about 0.73. A User ID with one branch per row would have split information , which pulls its gain ratio down as n grows.
Frequently asked questions about information gain
What is the information gain formula?
Information gain IG(S, A) equals the parent entropy H(S) minus the sum, over each value v of feature A, of |S_v| / |S| times H(S_v). In words, it is the entropy before the split minus the size-weighted average entropy of the child nodes. In the worked example, splitting 10 samples on Windy gives 1.0 minus 0.875, which is 0.125 bits.
Should you use Gini impurity or entropy for a decision tree?
Either works, because the two criteria almost always rank splits the same way, and Raileanu and Stoffel (2004) found they choose different splits in only about 2% of cases. Gini is the scikit-learn default because it skips the logarithm, while entropy gives gains in bits that connect to the rest of information theory.
Practice this
On TensorTonic you implement this from scratch and test it in the browser. These problems build up from one node's entropy to the best split across several features.
- Compute Entropy for a Node: the parent and child entropies used in every gain calculation.
- Compute Information Gain for a Split: the formula from this lesson, on a single split.
- Compute Gini Impurity for a Split: the weighted Gini version, for comparison.
- Decision Tree Best Split: search thresholds across features and keep the largest impurity reduction.
More problems on this topic include Decision Tree Classifier (CART), which grows a full tree with Gini splits and depth limits, and Random Forest from Scratch, which combines many trees.