SEM 6 › Data Mining › Week 11
Data Mining · Week 11

Classification & Decision Tree Induction

Classification vs prediction, the two-step process, preparing data, comparing classifiers, and building decision trees with information gain, gain ratio and the Gini index.

ITE34326 videos · 76 min📄 week 11.pdf

▶Lecture videos

Click any timestamp — here or inside the notes — to jump the video to that moment. Your position is remembered.

Decision Tree (short)

Short decision tree clip.

1Classification vs prediction

Both build a model from data to describe data classes or predict future trends.

Classification

Predicts categorical (discrete, unordered) class labels.

  • Bank: is a loan applicant "safe" or "risky"?
  • Medical: should a patient get treatment A, B or C?

Prediction (numeric)

Predicts a continuous-valued (ordered) attribute.

  • Bank: what loan amount (in $) is safe for this applicant?

There's no "class label" here. We call it the predicted attribute and build a predictor.

Key terms

Term Meaning
Tuple X An n-dimensional attribute vector X = (x₁, x₂, …, xₙ), with values for attributes A₁ … Aₙ
Class label attribute The attribute giving each tuple's predefined class; discrete and unordered
Training tuples Tuples used to build the model. Also called samples, examples, instances, data points, objects
Supervised learning The class label of each training tuple is given. Classification works this way
Unsupervised learning (clustering) Class labels are not known, and often the number of classes isn't either. E.g. find "groups of like tuples" that might be risk groups

2The two-step classification process

Step 1 – Learning: training data → classification algorithm → classifier (e.g. rules)Step 2 – Classification: test data → estimate accuracy → classify new data

Step 1: learning (training phase). The algorithm analyses a training set of tuples with their class labels and builds a classifier. In the loan example the classifier is a set of classification rules:

IF age = youth THEN loan_decision = risky
IF income = high THEN loan_decision = safe
IF age = middle_aged AND income = low THEN loan_decision = risky

Step 2: classification. First estimate predictive accuracy on a test set.

Why not test on the training data?

That estimate would be optimistic, because the classifier overfits: it learns particular anomalies of the training data that aren't in the general data. So the test set must be randomly selected and independent of the training tuples.

Accuracy = % of test-set tuples correctly classified

If the accuracy is acceptable, the classifier labels future ("unknown", "previously unseen") tuples, e.g. approving or rejecting new loan applicants.

3Preparing data & comparing methods

Preprocessing for classification

Step What and why
Data cleaning Reduce noise (smoothing) and treat missing values (most common value or most probable value). Reduces confusion during learning
Relevance analysis Correlation analysis removes redundant attributes (if A1 and A2 are strongly correlated, drop one). Attribute relevance analysis removes irrelevant ones, which would slow down and mislead learning
Transformation and reduction Normalization for neural networks and distance-based methods, so large-range attributes (income) don't outweigh small-range ones (binary). Generalization via concept hierarchies (income → low/medium/high, street → city) compresses data, so there is less I/O

Criteria to compare classifiers and predictors

Criterion Meaning
Accuracy Ability to correctly predict the class (or value) of new, unseen data. It's an estimate, so confidence limits can be computed
Speed Computational cost of building and using the model
Robustness Correct predictions on noisy data or data with missing values
Scalability Builds efficiently from large amounts of data
Interpretability Understanding and insight the model gives. Subjective, so harder to assess (e.g. extracting rules from a "black box" backpropagation neural network)
Memory hook: "A-S-R-S-I"

Accuracy · Speed · Robustness · Scalability · Interpretability

4Decision trees & their history

A decision tree is a flowchart-like tree:

  • Internal (non-leaf) node = a test on an attribute
  • Branch = an outcome of the test
  • Leaf (terminal) node = a class label
  • The top node is the root
                    age?
          ┌──────────┼──────────────┐
        youth    middle_aged      senior
          │          │               │
      student?      yes        credit_rating?
       ┌──┴──┐                  ┌────┴─────┐
      no    yes               fair     excellent
      │      │                 │          │
      no    yes               yes         no

Concept "buys_computer". The final tree from the worked example.

History

Who / when What
Hunt, Marin & Stone Earlier concept learning systems
J. Ross Quinlan, late 1970s – early 1980s ID3 (Iterative Dichotomiser)
Quinlan, later C4.5, ID3's successor and a benchmark for new algorithms (J48 in Weka)
Breiman, Friedman, Olshen, Stone, 1984 CART (Classification and Regression Trees): binary trees

ID3, C4.5 and CART all use a greedy (non-backtracking), top-down recursive divide-and-conquer approach. The training set is recursively partitioned into smaller subsets as the tree grows.

Basic algorithm (Figure 11-4) in plain words

  1. Create node N.
  2. If all tuples in D are in the same class → N becomes a leaf with that class.
  3. If there are no attributes left → leaf labelled with the majority class.
  4. Otherwise, use an attribute selection measure to find the best splitting criterion, label N with it, and remove the attribute if it was discrete with multiway splits.
  5. For each outcome j, form partition Dⱼ. If Dⱼ is empty → attach a leaf with the majority class of D. Otherwise recurse on Dⱼ.

5Attribute selection measures

An attribute selection measure (or splitting rule) is a heuristic for choosing the split that best separates D into classes. Ideally every partition is pure (one class). The measure ranks the attributes, and the best one is the splitting attribute. For continuous attributes we also need a split point; for binary trees, a splitting subset.

Measure Used by Split type
Information gain ID3 Multiway
Gain ratio C4.5 Multiway
Gini index CART Binary

Information gain (ID3)

Based on Claude Shannon's information theory. Choose the attribute with the highest gain: it minimises the information still needed (least impurity) and so the expected number of tests.

Info(D) = − Σi=1..m pi log₂(pi) (entropy of D)
InfoA(D) = Σj=1..v ( |Dj| / |D| ) × Info(Dj)
Gain(A) = Info(D) − InfoA(D)
  • pi = |Ci,D| / |D| = probability that a tuple belongs to class Ci.
  • |Dⱼ|/|D| is the weight of partition j.

Gain ratio (C4.5)

Information gain is biased

It prefers attributes with many values. Splitting on a unique product_ID gives one pure partition per tuple, so Info = 0 and the gain is maximal, yet the split is useless for classification.

C4.5 normalises gain using split information: the potential information from splitting D into v partitions, regardless of class.

SplitInfoA(D) = − Σj=1..v ( |Dj| / |D| ) × log₂( |Dj| / |D| )
GainRatio(A) = Gain(A) / SplitInfoA(D)

Pick the attribute with the maximum gain ratio.

Gini index (CART)

Gini(D) = 1 − Σi=1..m pi²
GiniA(D) = ( |D₁| / |D| ) Gini(D₁) + ( |D₂| / |D| ) Gini(D₂) (binary split)
ΔGini(A) = Gini(D) − GiniA(D)
  • Gini considers binary splits only. For a discrete attribute with v values there are 2ᵛ subsets. Excluding the full set and the empty set leaves 2ᵛ − 2 subsets. Each split appears twice ({low} vs {medium, high} is the same split), so there are (2ᵛ − 2)/2 distinct binary splits.
  • For continuous attributes, try the midpoint between each pair of adjacent sorted values as the split point: D₁ = A ≤ split, D₂ = A > split.
  • Choose the attribute and split with the minimum GiniA(D), i.e. the maximum reduction in impurity.

6Worked example — buys_computer (14 tuples)

RID age income student credit_rating buys_computer
1 youth high no fair no
2 youth high no excellent no
3 middle_aged high no fair yes
4 senior medium no fair yes
5 senior low yes fair yes
6 senior low yes excellent no
7 middle_aged low yes excellent yes
8 youth medium no fair no
9 youth low yes fair yes
10 senior medium yes fair yes
11 youth medium yes excellent yes
12 middle_aged medium no excellent yes
13 middle_aged high yes fair yes
14 senior medium no excellent no

9 yes, 5 no.

Information gain

Step 1: entropy of D

Info(D) = −(9/14) log₂(9/14) − (5/14) log₂(5/14) = 0.940 bits

Step 2: split on age

age yes no Info(Dⱼ)
youth (5) 2 3 0.971
middle_aged (4) 4 0 0
senior (5) 3 2 0.971
Infoage(D) = (5/14)(0.971) + (4/14)(0) + (5/14)(0.971) = 0.694
Gain(age) = 0.940 − 0.694 = 0.246 bits

Step 3: the other attributes. Gain(income) = 0.029, Gain(student) = 0.151, Gain(credit_rating) = 0.048.

Step 4: choose the root. age has the highest gain, so it is the root. The middle_aged branch is all "yes", so it becomes a leaf. Recursing on youth (best is student) and senior (best is credit_rating) gives the final tree shown earlier.

Gain ratio for income

Income splits D into low (4), medium (6) and high (4):

SplitInfoincome(D) = −(4/14)log₂(4/14) − (6/14)log₂(6/14) − (4/14)log₂(4/14) = 1.557
GainRatio(income) = 0.029 / 1.557 = 0.019
Note on the PDF's numbers

The lesson PDF prints SplitInfo = 0.926 and GainRatio = 0.031. Those values come from an older textbook edition and are arithmetically wrong. Calculating the formula gives 1.557 and 0.019. The tool below shows the working. In an exam, show your working and you'll get the marks either way.

Gini index

Gini(D) = 1 − (9/14)² − (5/14)² = 0.459
Attribute Best binary split GiniA(D)
income {low, medium} vs 0.443 (the others: {low, high}/{medium} 0.458, {medium, high}/{low} 0.450)
age {youth, senior} vs {middle_aged} 0.357 ← minimum
student yes vs no 0.367
credit_rating fair vs excellent 0.429

Worked line for income ∈ {low, medium}: D₁ has 10 tuples (7 yes, 3 no), D₂ has 4 (2 yes, 2 no).

Gini = (10/14)[1 − (7/10)² − (3/10)²] + (4/14)[1 − (2/4)² − (2/4)²] = (10/14)(0.42) + (4/14)(0.5) = 0.443

So the root split is age ∈ {youth, senior}, with impurity reduction 0.459 − 0.357 = 0.102.

Note on the PDF's numbers

The PDF says income {medium, high} gives 0.300 and is chosen. That's also from the older edition's error. Computed exactly, age {youth, senior} = 0.357 is the minimum. Every number in the table above can be checked with the calculator below.

🧪 Try it — Decision tree calculator (ID3 / C4.5 / CART measures)
Exam focus

For the exam:

  • Know all three formulas.
  • Be able to compute Info(D), Info_A(D) and Gain for each attribute, show the root choice, and draw the final tree.
  • Explain why gain ratio is needed (bias toward many-valued attributes) and what Gini considers (binary splits, 2ᵛ − 2 subsets).

7Weka: J48, filters and visualisation

The Weka videos this week cover:

Video Key takeaways
1.4 Building a classifier Classify tab → choose J48 (Weka's C4.5) → run with default 10-fold cross-validation. Read the accuracy, the confusion matrix and the printed tree. Pruning options change tree size
1.5 Using a filter Filters (in Preprocess) transform data. E.g. the Remove filter deletes attributes. Removing an attribute can change, and even improve, the tree
1.6 Visualizing your data The Visualize panel plots attribute pairs coloured by class. You can also visualise classifier errors: misclassified instances are shown as squares

8Quick revision sheet

Concept Remember it as
Classification vs prediction Categorical label vs continuous value
Supervised vs unsupervised Labels given (classification) vs not (clustering)
Two steps Learn on training set → test accuracy on an independent test set → classify new data
Overfitting Learns training anomalies, so a training-set accuracy estimate is optimistic
Prep Cleaning · relevance analysis · transformation & reduction
Compare Accuracy · Speed · Robustness · Scalability · Interpretability
DT parts Internal node = test · branch = outcome · leaf = class
Algorithms ID3 (info gain) · C4.5 (gain ratio) · CART (Gini, binary); greedy top-down
Entropy Info(D) = −Σ p log₂ p; buys_computer = 0.940
Gain(age) 0.940 − 0.694 = 0.246 (highest → root)
Gain ratio Gain / SplitInfo; fixes many-valued bias
Gini 1 − Σp²; binary splits; choose minimum; 2ᵛ − 2 subsets

?Self-check quiz

11 questions on this week. Answer them all, then press Check my answers. Your best and latest scores are saved and shown on the subject page.