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.
▶Lecture videos
Click any timestamp — here or inside the notes — to jump the video to that moment. Your position is remembered.
Support Lecture – Classification & Prediction
Full support lecture on classification and prediction.
00:01Classification and prediction: introduction01:47Classification models: decision tree, Bayesian, neural network03:36Applications: loan approval, medical diagnosis04:34Two steps: model construction (training) and model usage (testing)08:13Criteria for evaluating classifiers: accuracy, speed, robustness10:03Decision tree induction: a greedy, top-down, recursive algorithm12:42Stopping conditions and leaf nodes14:24Attribute selection measure 1: information gain19:09Worked example: buys_computer, Info(D) = 0.94020:55Information for age (≤30, 31–40, >40)22:53Gain(age) and choosing the root25:29Growing the subtrees29:59Weather data: Outlook as the root34:37Gain ratio and split information37:23Gini index40:13Extracting rules from the treeClassification
Short explainer on classification.
00:00Training data with known outcomes; test data01:46Example rule: IF rank = professor…03:27Weather data: class distribution per attribute value05:12Info before the split (9 yes / 5 no)06:02Information gain for humidity and the other attributes06:52Selecting the root by highest gain07:45Growing the Outlook = sunny branchDecision Tree (short)
Short decision tree clip.
Weka 1.4 – Building a classifier
Using J48 (C4.5) in Weka's Classify panel.
00:16Introduction: the glass dataset01:06Choosing the J48 decision tree classifier02:05Running it: accuracy and the confusion matrix03:03The configuration panel: unpruned tree04:50Reading the tree: leaf counts and errors05:41Visualising the tree06:31More options; J48 = C4.5 historyWeka 1.5 – Using a filter
Removing attributes and other preprocessing with filters.
00:17Introduction: filters in Weka01:12Choosing a filter: removing attributes02:03Specifying attribute ranges03:46Filter help ("More")05:29Removing Fe (iron) and re-running J4806:20Result: nearly 69% accuracyWeka 1.6 – Visualizing your data
The Visualize panel and classifier errors.
00:19Introduction: the iris dataset01:10The Visualize panel: scatter plots coloured by class02:59Jitter: separating overlapping points04:46Visualising classifier errors05:36Changing the axes07:24Output predictions from J481Classification 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 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.
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.
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) |
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
- Create node N.
- If all tuples in D are in the same class → N becomes a leaf with that class.
- If there are no attributes left → leaf labelled with the majority class.
- 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.
- 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.
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)
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.
GainRatio(A) = Gain(A) / SplitInfoA(D)
Pick the attribute with the maximum gain ratio.
Gini index (CART)
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
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 |
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):
GainRatio(income) = 0.029 / 1.557 = 0.019
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
| 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).
So the root split is age ∈ {youth, senior}, with impurity reduction 0.459 − 0.357 = 0.102.
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.
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.