Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Machine Learning
Decision Trees Classification
Mustafa Jarrar
Birzeit University
Jarrar © 2018
1
Watch this lecture
and download the slides
Acknowledgement:
This lecture is based on (but not limited to) to the lecture notes found in [1,2,3]
Jarrar © 2018
2
Mustafa Jarrar: Lecture Notes on Decision Trees Machine Leaning
Birzeit University, 2018
Machine Learning
Decision Tree Classification
In this lecture:
Jarrar © 2018
3
Example of a Decision Tree
Outlook
New data:
D15 Rain High Weak ???
Jarrar © 2018
5
Decision Tree – Motivation Example
9 Yes / 5 No
Outlook
4/0
Overcast
2/3 3/2
Day Outlook Humid Wind
Sunny D3 Overcast High Weak Rain
D7 Overcast Normal Strong
D12 Overcast High Strong
Day Outlook Humid Wind Day Outlook Humid Wind
D13 Overcast Normal Weak
D1 Sunny High Weak D4 Rain High Weak
D2 Sunny High Strong D5 Rain Normal Weak
D8 Sunny High Weak D6 Rain Normal Strong
D9 Sunny Normal Weak D10 Rain Normal Weak
D11 Sunny Normal Strong D14 Rain High Strong
Jarrar © 2018
6
Decision Tree – Motivation Example
9 Yes / 5 No
Outlook
4/0
Overcast
2/3 3/2
Day Outlook Humid Wind
Sunny D3 Overcast High Weak Rain
D7 Overcast Normal Strong
D12 Overcast High Strong
D13 Overcast Normal Weak Day Outlook Humid Wind
Humidity D4 Rain High Weak
D5 Rain Normal Weak
0/3 2/0 D6 Rain Normal Strong
D10 Rain Normal Weak
High Normal D14 Rain High Strong
Day Outlook Humid Wind Day Outlook Humid Wind
D1 Sunny High Weak D9 Sunny Normal Weak
D2 Sunny High Strong D11 Sunny Normal Strong
D8 Sunny High Weak
Jarrar © 2018
7
Decision Tree – Motivation Example
9 Yes / 5 No
Outlook
4/0
Overcast
2/3 3/2
Day Outlook Humid Wind
Sunny D3 Overcast High Weak Rain
D7 Overcast Normal Strong
D12 Overcast High Strong
D13 Overcast Normal Weak
Humidity Wind
2/0 3/0 0/2
0/3
Jarrar © 2018
8
Decision Tree – Motivation Example
Yes ⇔ (Outlook=Overcast) ∨
Decision Rule: (Outlook=Sunny ∧ Humidity=Normal)∨
(Outlook=Rain ∧ Wind=Weak)
Outlook
Jarrar © 2018
10
Mustafa Jarrar: Lecture Notes on Decision Trees Machine Leaning
Birzeit University, 2018
Machine Learning
Decision Tree Classification
In this lecture:
q Part 1: Motivation Example
Jarrar © 2018
11
ID3 Algorithm
Based on [1]
Jarrar © 2018
12
ID3 Algorithm
From [Sagarnil Das]
Jarrar © 2018
13
C4.5 (and C5.0) Algorithms
Jarrar © 2018
14
Mustafa Jarrar: Lecture Notes on Decision Trees Machine Leaning
Birzeit University, 2018
Machine Learning
Decision Tree Classification
In this lecture:
q Part 1: Motivation Example
q Part 2: ID3 Algorithm
Jarrar © 2018
15
Which attribute to split on?
9 Yes / 5 No 9 Yes / 5 No
Outlook or? Wind
– can’t use the probability of “yes” given the set, P(“yes” | set):
• must be symmetric: 4 yes / 0 no as pure as 0 yes / 4 no
Jarrar © 2018
16
Entropy
Jarrar © 2018
17
Entropy
+ +--
+ +-+---
+++- -
++
-+-- - + ++ +
+- -+ - +- -+-
+- -
--
- -- -
-
- -- -- - + +++
++++ ++
-- - + +++
+
Jarrar © 2018
18
Information Gain
9 Yes / 5 No
6/2 3/3
= 0.94 - (8/14)∙0.81 - (6/14)∙1
Weak Strong = 0.048
E=0.81 E=1
Jarrar © 2018
19
Full Example (Entropy & Information Gain)
9 Yes / 5 No 9 Yes / 5 No 9 Yes / 5 No
E=0.94
Humidity Outlook Wind
G=0.151
3/4
6/1 2/3 4/0 3/2 6/2 3/3
E=0.98 E=0.59
Jarrar © 2018
20
Full Example (Entropy & Information Gain)
9 Yes / 5 No 9 Yes / 5 No 9 Yes / 5 No
E=0.94 E=0.94
Humidity Outlook Wind
G=0.151 G=0.247
3/4
6/1 2/3 4/0 3/2 6/2 3/3
Jarrar © 2018
21
Full Example (Entropy & Information Gain)
9 Yes / 5 No 9 Yes / 5 No 9 Yes / 5 No
E=0.94 E=0.94 E=0.94
Humidity Outlook Wind
G=0.151 G=0.247 G=0.048
3/4
6/1 2/3 4/0 3/2 6/2 3/3
Jarrar © 2018
22
Full Example (Entropy & Information Gain)
9 Yes / 5 No 9 Yes / 5 No 9 Yes / 5 No
E=0.94 E=0.94 E=0.94
Humidity Outlook Wind
G=0.151 G=0.247 G=0.048
3/4
6/1 2/3 4/0 3/2 6/2 3/3
Jarrar © 2018
23
Full Example (Entropy & Information Gain)
9 Yes / 5 No
Classify on
Outlook
(Humid | Wind)?
2/3 4/0 3/2
2/3 2/3
E=0.97 E=0.94 Day Outlook Humid Wind
Humidity Wind
G=0.97 D1 Sunny High Weak
D2 Sunny High Strong
0/3 2/0 1/2 1/1 D8 Sunny High Weak
D9 Sunny Normal Weak
High Normal Weak Strong
D11 Sunny Normal Strong
E=0 E=0 E=0.81 E=1
2/3 2/3
E=0.97 E=0.97 Day Outlook Humid Wind
Humidity Wind
G=0.97 G=-0.012 D1 Sunny High Weak
D2 Sunny High Strong
0/3 2/0 1/2 1/1 D8 Sunny High Weak
D9 Sunny Normal Weak
High Normal Weak Strong
D11 Sunny Normal Strong
E=0 E=1 E=0.81 E=1
Outlook
2/3
Humidity
0/3 2/0
High Normal
No Yes
Jarrar © 2018
26
Full Example (Entropy & Information Gain)
9 Yes / 5 No
Classify on
Outlook (Humid | Wind)?
Outlook
2/3 3/2
Humidity Wind
Yes ⇔ (Outlook=Overcast) ∨
Decision Rule: (Outlook=Sunny ∧ Humidity=Normal)∨
(Outlook=Rain ∧ Wind=Weak)
Jarrar © 2018
29
Problems with Information Gain
Based on [1]
Jarrar © 2018
30
Mustafa Jarrar: Lecture Notes on Decision Trees Machine Leaning
Birzeit University, 2018
Machine Learning
Decision Tree Classification
In this lecture:
q Part 1: Motivation Example
q Part 2: ID3 Algorithm
q Part 3: Entropy and Information Gain
Jarrar © 2018
31
Overfitting in Decision Trees
• Can always classify training examples perfectly
– keep splitting until each node contains 1 example
– singleton = pure
• The more we split the the higher accuracy, but also the bigger the tree,
the more specific decision tree.
• As a result: The decision tree will be too specific and accurate for the
training data, but becomes less accurate for new data. Thus, the tree now
not be able to classify data that didn’t see before.
Jarrar © 2018
32
Overfitting in Decision Trees
• The test performance tells us how well our model generalizes, not the
training performance
Jarrar © 2018
33
Avoid Overfitting – Pruning
Jarrar © 2018
34
Avoid Overfitting – post-pruning
Creating the validation set
D
Be sure to have separate training, validation, and test sets
• Training set DT, to build the tree
• Validation set Dv to prune the tree
• Test set Dt to evaluate the final model.
Testing data for later evaluation should not be used for pruning or you
will not get honest estimate of the model’s performance
Jarrar © 2018
35
Avoid Overfitting – post-pruning
Prune the branches that will not do well on the future data
Jarrar © 2018
36
Mustafa Jarrar: Lecture Notes on Decision Trees Machine Leaning
Birzeit University, 2018
Machine Learning
Decision Tree Classification
In this lecture:
q Part 1: Motivation Example
q Part 2: ID3 Algorithm
q Part 3: Entropy and Information Gain
q Part 4: Overfitting and Pruning
Jarrar © 2018
37
Continuous Attributes
Based on [1]
Jarrar © 2018
38
Continuous Attributes
Based on [1]
Jarrar © 2018
39
Mustafa Jarrar: Lecture Notes on Decision Trees Machine Leaning
Birzeit University, 2018
Machine Learning
Decision Tree Classification
In this lecture:
q Part 1: Motivation Example
q Part 2: ID3 Algorithm
q Part 3: Entropy and Information Gain
q Part 4: Overfitting and Pruning
Jarrar © 2018
40
What is good about Decision Trees
Jarrar © 2018
41
Limitations for Decision Trees
Machine Learning
Decision Tree Classification
In this lecture:
q Part 1: Motivation Example
Jarrar © 2018
43
Using R to learn Decision Trees
DT_example.R
require(C50) # the package that has the C5.0 decision tree
Input.csv require(gmodels) # a package used draw diagrams and
Day,Outlook,Humidity,Wind,Play graphs
D1,Sunny,High,Weak,No
D2,Sunny,High,Strong,No
D3,Overcast,High,Weak,Yes print("Choose the data file when prompted")
D4,Rain,High,Weak,Yes
D5,Rain,Normal,Weak,Yes dataset = read.table(file.choose(), header = T, sep=",")
D6,Rain,Normal,Strong,No # to exclude the DayNo column (column #1)
D7,Overcast,Normal,Strong,Yes
D8,Sunny,High,Weak,No dataset = dataset[,-1]
D9,Sunny,Normal,Weak,Yes
D10,Rain,Normal,Weak,Yes
D11,Sunny,Normal,Strong,Yes # apply the decision tree algorithm to the training data
D12,Overcast,High,Strong,Yes feature columns, and class column (output), and generate a
D13,Overcast,Normal,Weak,Yes
D14,Rain,High,Strong,No DT Model.
model = C5.0(dataset[, -4], dataset[, 4])
Jarrar © 2018
44
Using R to learn Decision Trees
Output Diagram
Jarrar © 2018
45
References
[1] Victor Lavrenko, Charles Su;on: IAML: Decision Trees Lecture Notes 2011
[3] Sami Ghawi, Mustafa Jarrar: Lecture Notes on Introduction to Machine Learning, Birzeit University, 2018
[4] Mustafa Jarrar: Lecture Notes on Decision Trees Machine Learning, Birzeit University, 2018
[5] Mustafa Jarrar: Lecture Notes on Linear Regression Machine Learning, Birzeit University, 2018
Jarrar © 2018
46