Skip to main content

Decision Trees

Decision Tree Structure

You Already Think in Trees

Every day, you make decisions using “if-then” logic: Should I bring an umbrella?
  1. Is it raining? → Yes → Bring umbrella
  2. Is it cloudy? → Yes → Bring umbrella
  3. Is forecast rain > 50%? → Yes → Bring umbrella
  4. Otherwise → Don’t bring umbrella
That’s a decision tree!
Loan Approval Decision Tree

The Loan Approval Problem

Imagine you’re a bank deciding whether to approve loans:

How Would a Human Decide?

A loan officer might think:
A decision tree learns these rules from data!

Building a Decision Tree from Scratch

The Key Question: How Do We Choose Splits?

At each step, we need to decide:
  1. Which feature to split on?
  2. What value to split at?
We want splits that separate classes well.

Measuring “Purity” with Gini Impurity

Imagine reaching into a bag of colored balls. Gini Impurity measures: “If I pick two balls at random, how likely are they to be different colors?” Gini=1i=1Cpi2Gini = 1 - \sum_{i=1}^{C} p_i^2 Where pip_i is the proportion of class ii.
  • Gini = 0: Perfect purity — every ball is the same color. The split created a “pure” group.
  • Gini = 0.5: Maximum impurity for 2 classes — a 50/50 mix. Picking two balls at random gives you different colors half the time.
  • The goal: Each split should create child nodes with lower Gini than the parent — we’re sorting the balls into separate bags.

Information Gain

We want the split that reduces impurity the most: Gain=GiniparentnleftntotalGinileftnrightntotalGinirightGain = Gini_{parent} - \frac{n_{left}}{n_{total}} Gini_{left} - \frac{n_{right}}{n_{total}} Gini_{right}

Building the Tree


Using scikit-learn


Real Example: Iris Classification


The Problem: Overfitting

Decision trees can get too specific:
Overfitting Signs:
  • Training accuracy much higher than test accuracy (e.g., 100% train vs 70% test)
  • Very deep trees (many levels) — a tree with depth=50 has likely memorized individual data points
  • Leaves with very few samples — if a leaf has 1-2 samples, the tree is “remembering” specific examples instead of learning general patterns
Why trees overfit so easily: An unrestricted tree can always achieve 100% training accuracy by creating one leaf per training sample. It’s like answering a quiz by memorizing every question-answer pair — you ace the practice test but fail any new question. This is why we always limit max_depth or min_samples_leaf.

Controlling Tree Complexity


Regression Trees

Trees can also predict numbers!
Notice the “staircase” pattern — trees predict constant values in each region! This is a fundamental limitation of decision trees for regression: they can only output discrete values (the mean of training points in each leaf), creating a piecewise-constant approximation. For smooth, continuous relationships, you’ll need many leaves (risking overfitting) or an ensemble of trees (like Gradient Boosting, Module 6) that sums many small staircases into a smooth curve.

Advantages and Disadvantages

Advantages

  • Easy to understand and visualize
  • No feature scaling needed
  • Handles both numeric and categorical
  • Feature importance built-in
  • Fast predictions

Disadvantages

  • Prone to overfitting
  • Unstable (small data changes = different tree)
  • Axis-aligned splits only
  • Not as accurate as ensemble methods
  • Can be biased toward features with many levels

🚀 Mini Projects

Project 1

Build a loan approval classifier

Project 2

Titanic survival prediction

Project 3

Visualize and interpret decision rules

Key Takeaways

If-Then Rules

Trees learn rules from data automatically

Gini Impurity

Measures how mixed a group is (lower = purer)

Information Gain

Choose splits that reduce impurity most

Depth Control

Limit depth to prevent overfitting

What’s Next?

Before moving to ensemble methods, let’s explore two more powerful classifiers:

Continue to SVM

Support Vector Machines - find the optimal boundary between classes

Skip to Ensembles

Or jump ahead to Random Forests and Gradient Boosting