MACHINE LEARNING • LESSON 9

Overfitting in Decision Trees

A Decision Tree can become too complicated and learn the training data too closely. This is called overfitting.

THE SIMPLEST IDEA

A tree can learn the training data too well.

When a Decision Tree keeps making more and more splits, it can start learning small details and noise that do not represent useful patterns. The result may be excellent on training data but poor on new data.

01

What Is Overfitting?

Overfitting happens when a Machine Learning model learns the training examples too closely instead of learning general patterns.

With a Decision Tree, this usually happens when the tree becomes very deep and creates many small branches.

HEALTHY TREE Learns useful patterns

Makes reasonable decisions that can work on new data.

OVERFIT TREE Memorizes training details

Matches training examples extremely closely but may fail on new data.

02

A Simple Student Example

Suppose we want to predict whether a student will Pass or Fail.

We have training data containing information such as:

STUDY HOURS ATTENDANCE RESULT
2 60% Fail
3 70% Fail
5 85% Pass
6 90% Pass

A simple tree might learn a useful pattern such as:

SIMPLE PATTERN More study + better attendance → higher chance of passing
03

What Happens When the Tree Gets Too Deep?

Instead of stopping after learning useful patterns, the tree may continue splitting the data again and again.

It may create rules that are extremely specific to individual training examples.

FIRST DECISION Study Hours > 3?
NO Mostly Fail
SECOND DECISION Attendance > 80%?
YES Pass
NO Continue Splitting...

If the tree keeps splitting until it almost perfectly separates every training example, it can become too complicated.

04

Memorizing Instead of Learning

This is the easiest way to understand overfitting.

Imagine a student memorizes the exact answers from a practice exam instead of learning the underlying topic.

LEARNING Understands the pattern

Can solve new questions that use the same concept.

MEMORIZING Remembers exact examples

Performs well on familiar questions but struggles with new ones.

An overfitted Decision Tree behaves similarly. It remembers the training examples too closely instead of learning patterns that generalize.

05

Training Performance vs New Data

One of the clearest signs of overfitting is the difference between performance on training data and unseen data.

TRAINING DATA 100% Accuracy

The tree has learned the training examples extremely closely.

NEW DATA 70% Accuracy

The tree struggles when it sees examples it did not memorize.

High training accuracy alone does not prove that a Decision Tree is good.
06

Why Does a Deep Tree Overfit?

A Decision Tree can keep creating more specific rules as it grows deeper.

SHALLOW TREE Few decisions

Learns broad patterns.

DEEPER TREE More decisions

Learns more specific patterns.

TOO DEEP Very specific rules

Can memorize training data.

More complexity is not automatically better.

07

A Simple Example of Overfitting

Imagine the training data contains a student who studied 5 hours and passed.

An overly complex tree might eventually create a rule that is effectively specific to that training example.

TOO SPECIFIC Study Hours = 5 AND Attendance = 85% AND Student ID = 1042

→ Pass

This rule may perfectly describe that training example, but Student ID = 1042 does not tell us anything useful about whether another student will pass.

An overfitted model can learn details that are irrelevant to the real problem.
08

How Do We Recognize Overfitting?

A common warning sign is a large gap between training performance and performance on unseen data.

GOOD SIGN Training: 92%

New data: 89%

POSSIBLE OVERFITTING Training: 100%

New data: 65%

The exact numbers are only an example. The important idea is the gap between training performance and performance on unseen data.

09

Overfitting vs Underfitting

These are opposite problems.

UNDERFITTING Too simple

The model has not learned enough from the data.

GOOD FIT Learns useful patterns

The model performs reasonably well on both training and new data.

OVERFITTING Too specific

The model learns the training data too closely.

Too Simple Good Fit Too Complex
10

How Can We Reduce Overfitting?

The basic idea is to prevent the tree from becoming unnecessarily complex.

CONTROL TREE SIZE Limit how deep the tree can grow.
REQUIRE USEFUL SPLITS Avoid creating branches from very small or weak splits.

In Python, Decision Tree libraries provide parameters that can control the complexity of the tree.

We will use those parameters when we build a Decision Tree with Python later in this lesson.

11

The Real Goal

The goal is not to create the tree with the highest possible training accuracy.

The goal is to create a model that can make good predictions on data it has never seen before.

NOT THE GOAL Memorize training data
THE GOAL Generalize to new data
REMEMBER THIS

A Decision Tree should learn patterns, not memorize examples.

If the tree becomes too deep and specific, it may fit the training data extremely well while performing poorly on new data. That problem is called overfitting.

Training Data Tree Learns Too Complex Memorization Poor New Predictions
QUICK CHECK

Check Your Understanding

What is overfitting? When a model learns the training data too closely and performs poorly on new data.
Why can Decision Trees overfit? A tree can become very deep and create overly specific rules.
Is 100% training accuracy always good? No. A very high training score can be a warning sign if performance on new data is much lower.
What is the goal of a Decision Tree? To learn useful patterns that generalize well to new data.
NEXT TOPIC

Build a Decision Tree With Python

Next, we will build a Decision Tree classifier in Python and use it to make predictions from real data.