1 of 48

Decision Tree

CMSC 320 - Introduction to Data Science

​

​

Fardina F. Alam

2 of 48

Before We Start, Remember:

No free lunch theorem

Much of ML is concerned with devising different models and different

algorithms to fit them. However, there’s no single best model that works optimally for all ends of problems.

2

Topics to cover today:

  • What is Decision Tree
  • Common Terminology
  • Concept of How to Split a Node
  • Entropy
  • Information Gain
  • Overfitting Problem
    • Pre-Pruning
    • Post- Pruning

3 of 48

Machine Learning Fundamentals

What Is a Decision Tree?

Decision Tree: Learning Through Questions

  • Supervised ML Algorithm: A core predictive model in machine learning.
  • Sequential Questions: Makes predictions by asking a sequence of questions about the data.
  • Feature Splitting: Splits data based on key feature values at each node.
  • Dual Purpose: Can be used for both classification and regression tasks.

Example Scenario

Example: Will a customer buy a product?

“Think of a decision tree as a flowchart. Depending on the answer, we follow a different branch until we reach a final prediction. Unlike KNN, which compares distances between points, a decision tree learns decision rules from the training data.”

4 of 48

Terminology: Decision Tree Structure

A binary decision tree is a decision tree in which each node has two branches.

Starting point�(does not have any incoming branches)

Terminal points�(represent all the possible outcomes within the dataset.)

​

Root Node: The top decision node (starting point for decision making).

​

Decision/Internal Node: Tests a feature and splits into branches.

​

Leaf/ Terminal Node: Final node with a prediction (no further splits).

​

Branch/Sub-Tree: A connection between nodes, representing a possible outcome based on a feature test.

Layer: Nodes at the same depth from the root.

Depth: The maximum number of layers from root to farthest leaf.

5 of 48

6 of 48

Output of Decision Trees

8

1. Classification Trees

Output is a discrete category (e.g., "Yes/No", "Cat/Dog").

2. Regression Trees

Predict a continuous numerical value (e.g., house price, temperature).

Decision Rules

Each path from root to leaf represents an IF-THEN rule.

7 of 48

Example: Decision Trees for Classification

Below Left: Training Data  |  Below Right: Decision Tree constructed using this data

9

Predict: Deciding whether to play or not to play Tennis on a Saturday.

• 4 Categorical Features: Outlook, Temp., Humidity, Wind• Target: Binary classification (Play vs. No-Play)

Example credit: Tom Mitchell

8 of 48

Slide by Tom Mitchell and Victor Lavrenko

9 of 48

Decision Tree:

Divide and Conquer

Start with: outlook feature

10 of 48

Decision Tree:

Divide and Conquer

Start with: outlook feature

11 of 48

Next:

split further in order to get pure subsets

Start with: Let’s consider “Humid”

12 of 48

Next:

split further in order to get pure subsets

Start with: Let’s consider “Wind”

13 of 48

14 of 48

Resulting

Final

Decision

Tree

A logical Predicates or formula that told you in which cases John will play and vice versa.

15 of 48

How Do We Choose the Best Split?

Goal: Create groups that are as pure as possible.

  • A good split separates different classes.
  • A pure group contains mostly (or entirely) one class.
  • We need a way to measure how mixed a group is.

​

16 of 48

Concept Learning in Decision Trees

Key Concept: Decision trees learn boolean functions (True/False outputs) from training examples.

REPRESENTATION

• Learn True/False (Yes/No) outputs

• Tree = OR of AND rules

- Each Path → AND (conjunction)

- Full Tree → OR (disjunction)

EXPRESSIVITY

• Can represent any Boolean function (AND, OR, XOR)

• Some functions → very large (exponential) trees

• Produces human-interpretable rules

DESIGN PRINCIPLE

• Prefer simpler trees (Occam's Razor)

• Fewer splits → better generalization

• Easier to interpret & explain

DECISION TREE DIAGRAM

RULE FORMULA EXAMPLE

IF (Outlook=Sunny AND Humidity=High)

OR IF (Outlook=Rain AND Wind=Strong)

THEN Play Tennis = No

17 of 48

Decision Tree Induction

Overview: The process of automatically constructing a decision tree from training data

ALGORITHMS OVERVIEW

• Many Algorithms: C4.5, Hunt’s Algorithm, CART, SLIQ, SPRINT

TARGET FOCUS

ID3 (Iterative Dichotomiser 3)

➔ We will focus only on this algorithm

HISTORICAL MILESTONES

• Ross Quinlan: ID3 (1986), C4.5 (1993)

• Breiman et al.: CART (1984) from statistics

ALGORITHM PSEUDOCODE (ID3)

18 of 48

Concept of “pure” vs “impure” node

​

Try:

Pure Node: All (or majority) samples belong to one class

  • No further splitting needed
  • Easy to make prediction

​

​

​

​

​

​

Impure Node: Contains mixed classes. Requires further splitting

Example “Sunny”, “Rain” → Impure nodes (need splitting)

Here, “Overcast” is a pure node

Purity Goal: Seek splits that create subsets that are: Either all positive (e.g., 4Y/0N) Or all negative (e.g., 0Y/4N)

19 of 48

How Does a Decision Tree Choose a Split?

Goal: Create groups that are as pure as possible.

  • A good split separates different classes cleanly.
  • A pure group contains mostly (or entirely) one class.
  • We need a quantitative way to measure how mixed a group is.

Key takeaway: A good split reduces impurity.

20 of 48

Entropy: Measuring Impurity

Entropy measures how mixed or uncertain the class labels are in a dataset.

Key Characteristics

Entropy = 0 (Pure)

All samples belong to a single class. Zero uncertainty.

Entropy = 1 (Maximum Impurity)

Classes are equally represented (e.g., 50/50 binary split).

Higher Entropy → Greater Uncertainty

As class mixing increases, entropy rises toward its peak.

Binary Entropy Curve

p_i = proportion of samples in class i; 0 log₂ 0 is treated as 0.

21 of 48

22 of 48

ENTROPY

23 of 48

24 of 48

Entropy Computing Example:

24

Entropy =

25 of 48

From Entropy to Information Gain

How Does a Decision Tree Choose the Best Split?

We know that lower entropy means a purer node.

But what if we have multiple ways to split the data?

Information Gain measures how much a split reduces entropy.

26 of 48

Calculating Information Gain

Information Gain = Entropy Before − Weighted Entropy After

FORMAL DEFINITION & NOTATION

COMPONENT BREAKDOWN

Key takeaway: Higher Information Gain = Greater reduction in impurity = Better split.

27 of 48

28 of 48

Information Gain with Formula:

  •  

28

 

This split has a low IG

(in fact zero IG)

This split has higher IG

29 of 48

Consider, Training Examples

Example: Entropy and Information Gain

Day

Outlook

Temp

Humidity

Wind

Tennis?

D1

Sunny

Hot

High

Weak

No

D2

Sunny

Hot

High

Strong

No

D3

Overcast

Hot

High

Weak

Yes

D4

Rain

Mild

High

Weak

Yes

D5

Rain

Cool

Normal

Weak

Yes

D6

Rain

Cool

Normal

Strong

No

D7

Overcast

Cool

Normal

Strong

Yes

D8

Sunny

Mild

High

Weak

No

D9

Sunny

Cool

Normal

Weak

Yes

D10

Rain

Mild

Normal

Weak

Yes

D11

Sunny

Mild

Normal

Strong

Yes

D12

Overcast

Mild

High

Strong

Yes

D13

Overcast

Hot

Normal

Weak

Yes

D14

Rain

Mild

High

Strong

No

30 of 48

31 of 48

Determine the Root Attribute:

Let’s use IG based criterion to construct a DT for the Tennis example

  • At root node, let’s compute IG of each of the 4 features
  • Consider feature “wind”. Root contains all examples S = [9+,5-]

​

​

Sweak = [6+, 2−] ⇒ H(Sweak ) = 0.811

Sstrong = [3+, 3−] ⇒ H(Sstrong) = 1

​

​

​

​

​

​

​

​

​

​

31

 

 

Wind

Weak

Strong

6+, 2−�Ε=0.811

3+, 3−�Ε=1.000

9+, 5− Ε=0.940

32 of 48

Determine the Root Attribute:

Let’s use IG based criterion to construct a DT for the Tennis example

  • At root node, let’s compute IG of each of the 4 features
  • Consider feature “wind”. Root contains all examples S = [9+,5-]

​

​

Sweak = [6+, 2−] ⇒ H(Sweak ) = 0.811

Sstrong = [3+, 3−] ⇒ H(Sstrong) = 1

​

​

​

​

​

​

​

​

​

​

32

 

 

  • Likewise, at root: IG(S, outlook) = 0.246, IG(S, humidity) = 0.151, IG(S,temp) = 0.029
  • Thus we choose “outlook” feature to be tested at the root node

33 of 48

Now

  • Now how to grow the DT, i.e., what to do at the next level? Which feature to test next?
  • Rule: Iterate - for each child node, select the feature with the highest IG

​

​

​

​

​

​

​

​

​

​

​

33

34 of 48

Sort the Training Example and Growing the tree

Ssunny= {D1,D2,D8,D9,D11}

Gain (Ssunny, Humidity) = .970

Gain (Ssunny, Temp) = .570

Gain (Ssunny, Wind) = .019

Outlook

Sunny

Overcast

9+, 5− {D1,…,D14}

Rain

{D3,D7,D12,D13}� 4+, 0−

Yes

{D4,D5,D6,D10,D15}� 3+, 2−

{D1,D2,D8,D9,D11}� 2+, 3−

?

?

34

Organize training data at each decision node based on the attribute that best separates the examples into distinct classes.

35 of 48

Growing the tree

​

​

​

​

​

​

  • So, proceeding as before, for level 2, left node, we can verify that
    • IG(S,temp) = 0.570, IG(S, humidity) = 0.970, IG(S, wind) = 0.019
  • Thus humidity chosen as the feature to be tested at level 2, left node
    • No need to expand the middle node (already “pure” - all “yes” training examples)
  • Can also verify that wind has the largest IG for the right node.
  • Note: If a feature has already been tested along a path earlier, we don’t consider it again

​

​

​

35

36 of 48

Final Decision Tree

Outlook

Sunny

Overcast

Rain

Humidity

Wind

Yes

High

Normal

Strong

Weak

No

36

Yes

Yes

No

37 of 48

One more example: Training a Decision Tree

​

Split horizontally

Split vertically

Final decision tree!

38 of 48

What about? If features are continuous:

Internal nodes can test the value of a feature against a threshold.

​

38

39 of 48

39

+ + + +

+ + - +

+ + + +

​

  • - - -
  • - - -
  • - - -
  • - - -

+ + + +

+ + + +

+ + + +

​

+ + + +

+ + + +

+ + + +

  • - - -
  • - - -
  • - - -
  • - - -
  • - - -
  • - - -
  • - - -
  • - - -

Attribute 1

Attribute 2

A very deep tree required

To fit just one odd training

example

​

Overfitting

40 of 48

Overfitting: When to stop Splitting Further?

40

+ + + +

+ + - +

+ + + +

​

  • - - -
  • - - -
  • - - -
  • - - -

+ + + +

+ + + +

+ + + +

​

+ + + +

+ + + +

+ + + +

  • - - -
  • - - -
  • - - -
  • - - -
  • - - -
  • - - -
  • - - -
  • - - -

Attribute 1

Attribute 2

A very deep tree required

To fit just one odd training

example

​

41 of 48

  • Consider adding noisy training example (should be +):

​

​

  • What effect on earlier tree?

Day

Outlook

Temp

Humidity

Wind

Tennis?

D15

Sunny

Hot

Normal

Strong

No

41

Overfitting in Decision Tree

42 of 48

42

Outlook

Overcast

Rain

3,7,12,13

4,5,6,10,14

3+,2-

Sunny

1,2,8,9,11

4+,0-

2+,3-

Yes

Humidity

Wind

Normal

High

No

Weak

Strong

No

Yes

Weak

Strong

No

Yes

Wind

Noise or other

coincidental regularities

Overfitting in Decision Tree - Example

43 of 48

When to Stop Growing the Decision Tree?

Goal: Avoid overfitting while keeping the tree useful.

When to Stop Splitting

  • When a node is sufficiently pure.
  • When there are too few samples to split further.
  • When the maximum tree depth is reached.
  • When additional splits provide little improvement.
  • Use validation performance to help choose tree complexity.

No Need to Obsess Too Much for Purity

(To help prevent the tree from growing too much!)

• It is okay to have a leaf node that is not fully pure.

Predictions at Impure Leaf Nodes:

For test inputs reaching an impure leaf, predict probability of each class (e.g. p(green) = 80%), or simply predict the majority label (Green).

44 of 48

Avoiding Overfitting in DTs

44

Goal: Keep the tree simple while maintaining good prediction accuracy.

Two main approaches:

Pre-pruning:

Stop the tree from growing too much.

Post-pruning:

Grow the tree first, then remove unnecessary branches.

How do we decide what to prune?

  • Use a separate validation set to evaluate tree performance.
  • Compare simpler trees with more complex trees.
  • Prefer a simpler tree when additional complexity does not improve validation performance.

Key takeaway: Pruning reduces unnecessary complexity and helps prevent overfitting.

45 of 48

46 of 48

Scikit Learn Decision Tree

47 of 48

Scikit Learn: Example with Custom Parameters:

48 of 48

The End