Classification

Classification assigns an object to one of several predefined classes. It has two steps: learning, in which a model is built from a training set whose class labels are known, and classification, in which the model is tested on a separate test set and then used on new data. It is supervised learning.

Decision Tree Induction

A decision tree is a flowchart-like tree: each internal node tests an attribute, each branch is an outcome of the test and each leaf holds a class. It is built top-down by choosing, at each node, the attribute that best separates the classes. ID3 uses information gain, C4.5 uses gain ratio and CART uses the Gini index.

  • Entropy of a set: Info(D) = - Σ pi log2(pi); it is 0 for a pure set and 1 for a 50:50 two-class set.
  • Information gain of attribute A = Info(D) - Σ (|Dj| / |D|) x Info(Dj).

The attribute with the highest gain becomes the split. Trees are pruned to avoid overfitting.

Naive Bayes

Naive Bayes uses Bayes' theorem: P(C | X) = P(X | C) x P(C) / P(X). It is called naive because it assumes that attributes are independent given the class, so P(X | C) is the product of P(xi | C). The class with the highest value of P(X | C) x P(C) is chosen. It is fast and works well with small data; a Laplace correction (adding 1 to counts) avoids zero probabilities.

Definition: Information gain. The reduction in entropy obtained by splitting a data set on an attribute; the attribute with the highest gain is chosen for the split.

Table 2.3: Decision tree and Naive Bayes

Point Decision tree Naive Bayes
Model Tree of rules Probabilities
Basis Entropy or Gini Bayes' theorem
Assumption None Independent attributes
Easy to read Yes Less
Handles small data Needs enough data Works well

Example 2.3: Information gain of an attribute

Problem: Ten records have 6 Yes and 4 No. Splitting on Outlook gives Sunny (1 Yes, 3 No), Overcast (3 Yes, 0 No) and Rain (2 Yes, 1 No). Find the information gain. Also find P(Yes | Sunny) by Bayes' theorem.

Solution:

  1. Info(D) = -(0.6 log2 0.6 + 0.4 log2 0.4) = 0.971.
  2. Info(Sunny) = 0.811, Info(Overcast) = 0, Info(Rain) = 0.918.
  3. Weighted entropy = 0.4 x 0.811 + 0.3 x 0 + 0.3 x 0.918 = 0.600.
  4. Gain(Outlook) = 0.971 - 0.600 = 0.371.
  5. P(Yes) x P(Sunny | Yes) = 0.6 x 1/6 = 0.10 and P(No) x P(Sunny | No) = 0.4 x 3/4 = 0.30, so P(Yes | Sunny) = 0.10 / 0.40 = 0.25.

Result: Gain(Outlook) is 0.371; a Sunny day is classified as No, with probability 0.75.

from math import log2
def info(counts):
    n = sum(counts)
    return -sum(c / n * log2(c / n) for c in counts if c)
parts = [(1, 3), (3, 0), (2, 1)]
total = info([6, 4])
rest = sum(sum(p) / 10 * info(p) for p in parts)
print("Info(D):", round(total, 3))
print("Weighted:", round(rest, 3))
print("Gain:", round(total - rest, 3))
yes = 0.6 * (1 / 6)
no = 0.4 * (3 / 4)
print("P(Yes|Sunny):", round(yes / (yes + no), 2))

Output

Info(D): 0.971
Weighted: 0.6
Gain: 0.371
P(Yes|Sunny): 0.25

Common mistakes

  • Judging a classifier only on the training data, which hides overfitting.
  • Splitting on an attribute with many unique values such as an ID.
  • Forgetting the Laplace correction, so one zero probability wipes out the product.
  • Using Naive Bayes with strongly dependent attributes and trusting the exact probabilities.

Key takeaways

  • Classification is supervised: train, then test.
  • ID3 uses information gain; CART uses the Gini index.
  • Gain = Info(D) minus weighted entropy after the split.
  • Naive Bayes assumes independent attributes.

Practice questions

  1. What is the entropy of a pure set?
  2. Which measure does ID3 use to choose a split?
  3. Why is Naive Bayes called naive?

Answers to the practice questions

  1. Zero.
  2. Information gain.
  3. It assumes that attributes are independent given the class.

Sources

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