Tree Building Algorithm

Decision tree induction builds a tree top-down by recursive partitioning. At each node the algorithm picks the best splitting attribute, makes a branch for each value, and repeats on each part. It stops when all records of a node belong to one class, when no attributes remain (the majority class is used), or when a node has no records.

Attribute Selection Measures

  • Information gain (ID3): the reduction in entropy; it favours attributes with many values.
  • Gain ratio (C4.5): gain divided by split information = - Σ (|Dj| / |D|) log2(|Dj| / |D|), which corrects that bias.
  • Gini index (CART): Gini(D) = 1 - Σ pi². It is 0 for a pure set. CART makes binary splits and chooses the split with the largest reduction in Gini.

Tree Pruning

A tree grown fully may overfit noise. Pre-pruning stops the growth early (a minimum gain or a minimum number of records). Post-pruning grows the full tree and then removes branches that do not improve accuracy on new data (cost-complexity pruning in CART, pessimistic pruning in C4.5).

Extracting Rules

A tree is easily turned into IF-THEN rules, one for each path from the root to a leaf: IF Outlook = Overcast THEN Play = Yes.

Strengths and Limits

Trees need no data preparation beyond cleaning, handle numeric and categorical data and are easy to read. They can be unstable, since a small change in data may change the tree, and they can overfit.

Definition: Gini index. A measure of the impurity of a set, equal to one minus the sum of the squared class proportions; a pure set has a Gini index of 0.

Table 3.2: Attribute selection measures

Measure Used by Note
Information gain ID3 Biased to many-valued attributes
Gain ratio C4.5 Corrects the bias
Gini index CART Binary splits, no logarithm

Example 3.2: Gini index and gain ratio

Problem: Ten records have 6 Yes and 4 No. Outlook splits them into Sunny (1 Yes, 3 No), Overcast (3 Yes, 0 No) and Rain (2 Yes, 1 No), with a gain of 0.371. Find the Gini index of the data, the Gini of the binary split Sunny against others, and the gain ratio of Outlook.

Solution:

  1. Gini(D) = 1 - (0.6² + 0.4²) = 1 - 0.52 = 0.48.
  2. Sunny (4 records): Gini = 1 - (0.25² + 0.75²) = 0.375. Others (5 Yes, 1 No in 6 records): Gini = 1 - (5/6)² - (1/6)² = 0.278.
  3. Weighted Gini = 0.4 x 0.375 + 0.6 x 0.278 = 0.317, a reduction of 0.48 - 0.317 = 0.163.
  4. Split information = -(0.4 log2 0.4 + 0.3 log2 0.3 + 0.3 log2 0.3) = 1.571.
  5. Gain ratio = 0.371 / 1.571 = 0.236.

Result: Gini(D) = 0.48, split Gini = 0.317 and gain ratio of Outlook = 0.236.

from math import log2
def gini(c):
    n = sum(c)
    return 1 - sum((x / n) ** 2 for x in c)
print("Gini(D):", round(gini([6, 4]), 3))
left, right = [1, 3], [5, 1]
w = 0.4 * gini(left) + 0.6 * gini(right)
print("Split Gini:", round(w, 3), "reduction:", round(0.48 - w, 3))
sizes = [4, 3, 3]
split = -sum(s / 10 * log2(s / 10) for s in sizes)
print("Split info:", round(split, 3))
print("Gain ratio:", round(0.371 / split, 3))

Output

Gini(D): 0.48
Split Gini: 0.317 reduction: 0.163
Split info: 1.571
Gain ratio: 0.236

Common mistakes

  • Splitting until every leaf is pure, which memorises the noise.
  • Using information gain on an ID-like attribute with a unique value for every record.
  • Forgetting that CART makes only binary splits.
  • Pruning using the training data alone.

Key takeaways

  • ID3 uses gain, C4.5 gain ratio and CART the Gini index.
  • Gini(D) = 1 - Σ pi²; it is 0 for a pure set.
  • Pruning (pre or post) avoids overfitting.
  • Each root-to-leaf path gives one IF-THEN rule.

Practice questions

  1. Which algorithm uses the Gini index?
  2. Why is gain ratio used?
  3. What is post-pruning?

Answers to the practice questions

  1. CART.
  2. To correct the bias of information gain toward many-valued attributes.
  3. Growing the full tree and then removing branches that do not help.

Sources

  • Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit III