Tabellab
Data, tech, science & health

How to calculate information gain and entropy (decision tree example)

To calculate information gain, take the entropy of the parent node and subtract the size-weighted entropy of the child nodes: IG = H(parent) − Σ (n_child ÷ n) × H(child). Entropy is H = −p log₂ p − (1 − p) log₂(1 − p) for a yes/no target. In the example, 20 past loans with 8 defaults have an entropy of 0.971 bits. Splitting on "debt-to-income above 40%" lowers the weighted entropy to 0.610, an information gain of 0.361 bits, far more than employment type (0.102) or income (0.060). So a decision tree would ask about DTI first.

Updated 21 Oct 2026 5 min read By Tabellab team

Entropy: how mixed is a node?

Entropy measures the uncertainty in a group. For two classes with shares p and 1 − p:

H = −p × log₂(p) − (1 − p) × log₂(1 − p)

  • A pure node, where every loan defaulted or none did, has H = 0.
  • A 50/50 node has H = 1 bit, the maximum for two classes.
  • The root here has 8 defaults in 20 loans, p = 40%, so H = −0.4 × log₂(0.4) − 0.6 × log₂(0.6) = 0.971 bits.

In Excel: =-D2*LOG(D2,2)-(1-D2)*LOG(1-D2,2). LOG with base 2 gives the result in bits. Some libraries use the natural log, which changes the unit but never the ranking of splits.

Information gain step by step

Take the split "DTI above 40%". It sends 7 loans to the left (6 defaults) and 13 to the right (2 defaults).

  • Entropy left: p = 6/7 = 85.7%, H = 0.592.
  • Entropy right: p = 2/13 = 15.4%, H = 0.619.
  • Weighted child entropy: 7/20 × 0.592 + 13/20 × 0.619 = 0.610.
  • Information gain: 0.971 − 0.610 = 0.361 bits.

The weights matter. A tiny pure group must not count as much as a large mixed one, so each child is weighted by its share of the parent's rows. Without the weights, a split that isolates one loan would look perfect.

Worked example: three candidate splits on 20 loans

Rows 2 to 8 count the loans and defaults in the root and in the two children of each candidate split. Rows 11 to 13 combine each pair into weighted entropy, information gain and the Gini decrease. The lender and loans are fictional.

Worked example: three candidate splits on 20 loans
ABCDEF
1NodeLoansDefaultsp (default)Entropy (bits)Gini
2All loans (root)20840.0%0.9710.480
3Self-employed8562.5%0.9540.469
4Salaried12325.0%0.8110.375
5DTI above 40%7685.7%0.5920.245
6DTI 40% or below13215.4%0.6190.260
7Income under $40k9555.6%0.9910.494
8Income $40k or more11327.3%0.8450.397
9
10SplitWeighted entropyInformation gainWeighted GiniGini decrease
11Employment0.8690.1020.4130.068
12DTI > 40%0.6100.3610.2550.225
13Income < $40k0.9110.0600.4400.040
14
15Best first split (highest information gain)DTI > 40%
16Split information of the DTI split0.934
17Gain ratio of the DTI split (C4.5)0.387
Key formulas
  • E2=IF(OR(C2=0,C2=B2),0,-D2*LOG(D2,2)-(1-D2)*LOG(1-D2,2))Entropy, guarded for pure nodes
  • F2=1-D2^2-(1-D2)^2Gini impurity
  • B12=B5/$B$2*E5+B6/$B$2*E6Weighted child entropy
  • C12=$E$2-B12Information gain
  • C17=C12/C16Gain ratio

Fictional loan data for practice only.

Comparing candidate splits

A tree tries every candidate question and keeps the one with the highest gain. That is how the classic ID3 and C4.5 algorithms grow a tree.

  • Employment (self-employed vs salaried): gain 0.102.
  • DTI above 40%: gain 0.361.
  • Income under $40k: gain 0.060.

=INDEX(A11:A13,MATCH(MAX(C11:C13),C11:C13,0)) returns the winner. The tree then repeats the same search inside each child, on the rows that reached it, until the nodes are pure enough, too small or the maximum depth is reached. For numeric features such as DTI, every possible cut-point is a candidate split, and the best threshold is the one with the highest gain.

Counting the nodes with COUNTIFS

On raw data with one row per loan, COUNTIFS fills the node table: =COUNTIFS(DTI,">40%",Default,"Yes") counts the defaults among high-DTI loans and =COUNTIF(DTI,">40%") the loans. Divide to get p. A pure child has p = 0 or 1, and LOG(0,2) is an error in Excel, although 0 × log₂0 counts as 0 in the formula. Guard the entropy with =IF(OR(C2=0,C2=B2),0,…), as every entropy cell in the example does. The same counting pattern drives support, confidence and lift in market basket analysis.

Information gain vs Gini impurity

CART trees, and the default settings of scikit-learn, use Gini impurity instead of entropy: Gini = 1 − p² − (1 − p)². The root has a Gini of 0.480, and the DTI split lowers it to 0.255, a Gini decrease of 0.225. Gini is cheaper to compute because it needs no logarithm, and in practice both criteria choose the same split most of the time. They do here: DTI wins on both, ahead of employment (0.068) and income (0.040).

Gain ratio: the C4.5 correction

Information gain favours features with many values. A customer ID would split every loan into its own pure leaf and score the maximum gain while predicting nothing. C4.5 therefore divides the gain by the split information, the entropy of the split proportions themselves: for 7 and 13 loans it is 0.934, so the gain ratio of the DTI split is 0.361 ÷ 0.934 = 0.387. A split into many small branches has a high split information and is penalised.

To judge the finished tree, compare its predictions with the actual outcomes on held-out loans, see confusion matrix, precision and recall.

Common mistakes

  • Not weighting the children. A simple average of the child entropies lets a tiny pure node dominate. Weight each child by its share of the rows.
  • Dividing by all rows inside a node. p in a child is defaults in that child ÷ loans in that child, not ÷ all 20 loans.
  • LOG of zero. A pure node makes LOG(0,2) an error. Return 0 for pure nodes with an IF.
  • Mixing log bases. Bits (base 2) and nats (natural log) are both fine, but the parent and the children must use the same base.
  • Trusting gain on ID-like features. A feature with a unique value per row has maximum gain and no predictive value. Use gain ratio or drop it.
  • Choosing splits on the test data. Gains are computed on the training rows only. Score the finished tree on loans it has not seen.

Frequently asked questions

What is the formula for information gain?

Information gain = entropy of the parent − the weighted average entropy of the children, where each child is weighted by its number of rows divided by the parent's rows.

How do you calculate entropy for a decision tree?

For two classes with share p, entropy = −p log2(p) − (1 − p) log2(1 − p). A pure node has entropy 0 and a 50/50 node has entropy 1. In Excel use LOG(p,2) for the base-2 logarithm.

Can information gain be negative?

No. The weighted entropy of the children is never higher than the entropy of the parent, so the gain is zero or positive. A gain of zero means the split does not change the class mix.

Is Gini or entropy better for decision trees?

Both usually pick the same splits. Gini is slightly faster because it needs no logarithm and is the default in CART and scikit-learn. Entropy is used in ID3 and C4.5 and behind information gain.

Practise it in a lab

Related guides

Reading is step one. Doing is step two.

Every guide links to a lab where you build the same calculation in an online spreadsheet and get instant feedback.

Open the lab