COMP5318 — Applied Machine Learning

Week 5: Decision Trees & Ensembles

Week 5 starts with interpretable tree models and ends with some of the most practically strong classical ensembles. The tree lecture explains entropy, information gain, pruning, and gain ratio; the ensemble lecture explains bagging, random forests, AdaBoost, and gradient boosting; the notebook turns those ideas into side-by-side sklearn experiments on iris and the moons dataset.

🌲Entropy splits
Pruning
🎁Bagging / RF
🚀Boosting
DecisionTreeClassifier entropy / information gain BaggingClassifier RandomForestClassifier AdaBoost / GradientBoosting
📐 Math Foundations → 🗺 Mind Map →

Course materials

These files together answer two questions: how does a single tree decide where to split, and why do many imperfect trees often outperform one perfectly fitted tree?

FileWhat it contributes
w5.ipynbIris decision trees, graphviz visualisation, pre-pruning, moons-based bagging/random forest/boosting experiments, and concrete sklearn parameters.
w5-solutions.ipynbInterpretation of pruning and comparison of ensemble families.
ml5a.pdfDecision-tree theory: entropy, information gain, pruning strategies, numeric splits, gain-ratio correction, and CART's Gini note.
ml5b.pdfEnsemble theory: why diversity matters, bootstrap aggregation, AdaBoost, gradient boosting, and random forest construction.
t5.pdfBy-hand entropy and information-gain calculations for a small toy dataset.
t5-solutions.pdfWorked entropy values, gain comparison, and the final tree root choice.

Decision trees: purity, splits, and pruning

A decision tree recursively partitions the feature space. Each internal node tests an attribute, each branch corresponds to an outcome, and each leaf predicts a class. The central design question is always: which split should be chosen next?

Entropy

Measures class impurity. For a binary task, entropy is 0 when all examples belong to one class and 1 when the classes are evenly mixed.

Information gain

The reduction in entropy caused by splitting on an attribute. The lecture defines the best split as the one with the highest gain.

Gain ratio

A correction for information gain's bias toward highly branching attributes, such as ID-like features.

Gini note

The lecture mentions that CART uses Gini rather than entropy. Same goal, different impurity measure.

Lecture formulas

Entropy measures purity, and information gain is the decrease in entropy after a split. The Week 5 tutorial trains you to compute both by hand because that is the logic behind root and branch selection.

Numeric attributes

The lecture discretises numeric features by considering binary thresholds. The procedure is: sort by the numeric value, propose split points where the class changes, evaluate each candidate split, and keep the best one.

Pruning

Deep trees overfit easily. The lecture distinguishes pre-pruning (stop earlier) from post-pruning (grow fully, then remove subtrees using validation performance).

Notebook implementation: the iris example uses DecisionTreeClassifier(criterion='entropy', random_state=42). The pre-pruning example limits tree depth with max_depth=4. The visualisation step exists to make the node statistics legible: sample counts, class counts, and predicted majority class at each node.

Interpretability

The lecture repeatedly frames tree models as transparent. You can follow a single path from root to leaf and explain the prediction as a chain of tests.

Why highly branching features are risky

A feature such as an ID code can create very pure tiny subsets and therefore look artificially attractive to raw information gain, even though it overfits badly.

Validation-set role

When pruning, the lecture explicitly uses a validation set to decide whether replacing a subtree with a leaf improves or preserves generalisation.

Quick check - Gain ratio
Why does the lecture introduce gain ratio as an alternative to plain information gain?
Because information gain cannot handle binary classes
Because trees otherwise cannot be visualised with graphviz
Because information gain is biased toward attributes with many branches

Ensembles: why many weak or unstable models can win

The second half of Week 5 is about diversity. Ensembles help when the base models are individually useful but make different mistakes. If all models are identical, voting does nothing.

Bagging

Bagging stands for bootstrap aggregation. Build many models on bootstrap samples and combine their predictions, usually by majority vote for classification.

The notebook uses BaggingClassifier with 500 decision trees, each trained on 100 bootstrapped examples from the moons training split.

Random forest

Random forest adds a second source of randomness: at each split, only a subset of features is considered. The lecture emphasises that this reduces correlation between trees.

The notebook uses 500 trees and highlights max_features as a key parameter because it controls the strength-vs-correlation tradeoff.

AdaBoost

Sequentially focuses on examples that earlier learners handled badly. The lecture describes weighted training data and weighted voting; the notebook uses shallow trees (decision stumps) as base learners.

Gradient boosting

Also adds models sequentially, but instead of reweighting data in the AdaBoost style, it adds new learners that minimise the error of the current ensemble.

Lecture intuition for why ensembles help

  1. If all base learners make the same mistakes, voting does not improve anything.
  2. If their errors are less correlated, combining them reduces variance.
  3. Bagging and random-feature selection are tools for creating that diversity.

Bagging boundary

The notebook explicitly shows that bagging creates a smoother decision boundary than a single unpruned tree on the moons data.

Random-forest comment

The lecture says the ideal forest has accurate individual trees that are still not too correlated with each other.

Boosting comment

AdaBoost can turn weak learners into a strong learner if they are slightly better than random. That is the conceptual point of the theorem slide.

from sklearn.ensemble import BaggingClassifier, RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier

bag_clf = BaggingClassifier(
    DecisionTreeClassifier(criterion="entropy", random_state=42),
    n_estimators=500,
    max_samples=100,
    bootstrap=True,
    random_state=42
)

rnd_clf = RandomForestClassifier(
    criterion="entropy",
    n_estimators=500,
    max_leaf_nodes=16,
    random_state=42
)
Quick check - Random forest
Why does random forest select only a subset of features at each split?
To guarantee every tree is shallow
To reduce correlation between trees and increase ensemble diversity
Because decision trees cannot evaluate all features

Tutorial calculation: entropy and information gain by hand

Toy dataset result
Entropy

The tutorial solution computes the class entropy of the 8-example dataset as:

\(H(S)=0.95\) bits

Shape vs color
Information gain

The solution then computes:

  • gain(shape) = 0.45 bits
  • gain(color) = 0.34 bits

Therefore shape is selected as the root of the decision tree.

The important exam habit is to show the weighted entropy after the split, not just the parent entropy. You first compute the impurity of each branch subset, weight those impurities by branch size, and subtract the result from the parent entropy. The attribute with the largest reduction is chosen.

Comparison skill
Model tradeoffs

Be ready to compare trees with k-NN and linear models. Trees give interpretability and nonlinear splits; ensembles trade away some interpretability for accuracy and stability.

Chapter quizzes

Self-test and math questions for this week are in the Quiz Hub (practice or exam mode).

Open Quiz Hub Chapter flashcards