A decision tree classifies an instance by asking a sequence of questions about its features, each answer leading down a branch until a leaf gives the class.

Structure

Every internal node tests one feature, every branch is an outcome of the test and every leaf holds a class label. To classify, we start at the root and follow the branches that match the instance. Trees are easy to read, handle numerical and categorical features and need no scaling. They are built top-down: at each node choose the best feature to split on, split the data, and repeat on each part until the parts are pure or a stopping rule applies.

Entropy and Information Gain

The ID3 algorithm measures impurity with entropy: H(S) = − Σ pᵢ log₂ pᵢ, where pᵢ is the proportion of class i in the set S. Entropy is 0 for a pure set and 1 for a 50:50 two-class set. The information gain of a feature A is the reduction in entropy after splitting: Gain(S, A) = H(S) − Σ (|Sᵥ| / |S|) H(Sᵥ). ID3 splits on the feature with the highest gain. The CART algorithm instead uses the Gini index, Gini = 1 − Σ pᵢ², and builds binary trees; it can also do regression.

Overfitting and Pruning

A tree grown until every leaf is pure memorises the training data and overfits. We control this by limiting the maximum depth, requiring a minimum number of instances per leaf, or pruning branches that do not improve validation accuracy. A random forest, many trees on random samples whose votes are combined, reduces the variance of a single tree.

Definition: Information gain. The reduction in entropy of a data set obtained by splitting it on a feature; the feature with the highest gain is chosen at each node by the ID3 algorithm.

Table 3.5: ID3 compared with CART

Aspect ID3 CART
Impurity measure Entropy and information gain Gini index
Splits Multi-way on categories Binary
Tasks Classification Classification and regression
Pruning Basic Cost-complexity pruning

Example 3.5: Choosing the root feature

Problem: A set has 14 instances, 9 Yes and 5 No. Splitting on Outlook gives Sunny (2 Yes, 3 No), Overcast (4 Yes, 0 No) and Rain (3 Yes, 2 No). Splitting on Windy gives Weak (6 Yes, 2 No) and Strong (3 Yes, 3 No). Which feature is better?

Solution:

  1. H(S) = −(9/14) log₂(9/14) − (5/14) log₂(5/14) = 0.940.
  2. Outlook: H(Sunny) = H(Rain) = 0.971 and H(Overcast) = 0. The weighted entropy is (5/14)(0.971) + (4/14)(0) + (5/14)(0.971) = 0.694, so the gain is 0.940 − 0.694 = 0.247.
  3. Windy: H(Weak) = 0.811 and H(Strong) = 1. The weighted entropy is (8/14)(0.811) + (6/14)(1) = 0.892, so the gain is 0.048.
  4. Outlook has the larger gain.

Result: Outlook (gain 0.247) is chosen as the root, not Windy (gain 0.048).

from math import log2
def H(*counts):
    n = sum(counts)
    return -sum(c / n * log2(c / n) for c in counts if c)
parent = H(9, 5)
outlook = 5 / 14 * H(2, 3) + 4 / 14 * H(4, 0) + 5 / 14 * H(3, 2)
windy = 8 / 14 * H(6, 2) + 6 / 14 * H(3, 3)
print("H(S) =", round(parent, 3))
print("Gain(Outlook) =", round(parent - outlook, 3))
print("Gain(Windy)   =", round(parent - windy, 3))
gini = 1 - (9 / 14) ** 2 - (5 / 14) ** 2
print("Gini(S) =", round(gini, 3))

Output

H(S) = 0.94
Gain(Outlook) = 0.247
Gain(Windy)   = 0.048
Gini(S) = 0.459

Common mistakes

  • Splitting on an identifier such as a roll number, which has high gain but no meaning.
  • Growing a tree until every leaf is pure and trusting its accuracy.
  • Confusing entropy (impurity) with information gain (the reduction in impurity).
  • Forgetting that a pure set has entropy 0.

Key takeaways

  • Entropy H = −Σ p log₂ p; it is 0 for a pure set and 1 for a 50:50 split.
  • Gain = H(parent) − weighted H(children); split on the highest gain.
  • CART uses the Gini index = 1 − Σ p².
  • Prune or limit depth to avoid overfitting.

Practice questions

  1. What is the entropy of a pure node?
  2. Which feature does ID3 choose at each node?
  3. Name two ways to prevent a tree from overfitting.

Answers to the practice questions

  1. Zero.
  2. The one with the highest information gain.
  3. Limiting depth and pruning.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit III