Week 4 - Classification - Decision Tree 1
Week 4 - Classification - Decision Tree 1
Week 4:
Classification:
Decision Tree 1
2
Informatics Engineering | Universitas Surabaya
What is Classification
• Given a collection of records (training set)
– Each record is characterized by a tuple (x, y), where:
• x is the attribute set, predictor, independent variable, input
• y is the class label, response, dependent variable, output
• Task:
– Build a model that maps each attribute set x into one of the predefined
class label y.
Supervised Learning
• Classification is supervised
learning
– Supervision: the training data
such as observations or
measurements are accompanied
by labels indicating the classes
which they belong to.
– New data is classified based on
the models built from the training
set
Classification Techniques
• Base Classifiers
– Decision tree
– Rule-based
– Nearest-neighbor
– Naïve Bayes and Bayesian Belief Networks
– Support Vector Machines
– Neural Networks, Deep Neural Networks
• Ensemble Classifiers
– Boosting, Bagging, Random Forests
Decision Tree
6
Informatics Engineering | Universitas Surabaya
Decision Tree Induction
Splitting Attributes
Home Marital Annual Defaulted
ID
Owner Status Income Borrower
1 Yes Single 125K No Home
2 No Married 100K No Owner
Yes No
3 No Single 70K No
4 Yes Married 120K No NO MarSt
5 No Divorced 95K Yes Single, Divorced Married
6 No Married 60K No
Income NO
7 Yes Divorced 220K No
8 No Single 85K
< 80K > 80K
Yes
9 No Married 75K No NO YES
10 No Single 90K Yes
10
Yes Owner No
NO MarSt
Income NO
< 80K > 80K
NO YES
Apply Model to Test Data (2)
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Income NO
< 80K > 80K
NO YES
Apply Model to Test Data (3)
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Income NO
< 80K > 80K
NO YES
Apply Model to Test Data (4)
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Income NO
< 80K > 80K
NO YES
Apply Model to Test Data (5)
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Income NO
< 80K > 80K
NO YES
Apply Model to Test Data (5)
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Assign defaulted to “No”
Single, Divorced Married
Income NO
< 80K > 80K
NO YES
Another Example of Decision Tree
MarSt Single,
Married Divorced
Home Marital Annual Defaulted
ID
Owner Status Income Borrower
NO Home
1 Yes Single 125K No
Yes Owner No
2 No Married 100K No
3 No Single 70K No NO Income
4 Yes Married 120K No < 80K > 80K
5 No Divorced 95K Yes
NO YES
6 No Married 60K No
7 Yes Divorced 220K No
8 No Single 85K Yes There could be more than one tree that fits the same data!
9 No Married 75K No
10 No Single 90K Yes
10
Decision Tree Classification Task
Tid Attrib1 Attrib2 Attrib3 Class
Tree
1 Yes Large 125K No Induction
2 No Medium 100K No algorithm
3 No Small 70K No
6 No Medium 60K No
Training Set
Apply Decision Tree
Tid Attrib1 Attrib2 Attrib3 Class
Model
11 No Small 55K ?
15 No Large 67K ?
10
Test Set
Decision Tre Induction Algorithms
• Many algorithms:
– Hunt’s algorithm (one of the earliest)
– CART (Classification and Regression Trees – generation of binary decision tree)
– ID3 (Iterative Dichotomizer), C4.5 (became benchmark to which newer supervised learning
algorithms are compared)
– SLIQ, SPRINT
Let Dt be the set of training records that reach a 1 Yes Single 125K No
Status
Defaulted = No
(3,0) Single,
Married
Annual
Divorced Income
(3,0)
Defaulted = Yes Defaulted = No < 80K >= 80K
Small
Medium Large Extra Large
{Small, {Medium,
{Small, {Large, {Small} {Medium, Large, Large} Extra Large}
Medium} Extra Large} Extra Large}
Test Condition for Continuous Attributes
• Multi-way split: use as many partitions as distinct values.
C0: 5 C0: 9
C1: 5 C1: 1
• Choose the attribute test condition that produces the highest gain:
Gain = P – M
A? B?
Yes No Yes No
M1 M2
Gain = P – M1 vs P – M2
Problem with Large Number of Partitions
Node impurity measures tend to prefer splits that result in large number
of partitions, each being small but pure.
Gender Car Customer
Type ID
Customer ID has highest information gain because impurity for all the
children is zero.
Gini Index, Gini Split, Gain
• If a dataset D contains records from n classes, gini(D) is defined as:
𝑛
• The attribute provides the smallest ginisplit(D) or the larget reduction in impurity (Gain) is chosen to split
the node.
• Maximum of 1-1/n when records are equally distributed among all classes, implying the least
beneficial situation for classification.
• Minimum of – when all records belong to one class, implying the most beneficial situation for
classification.
Examples: Gini Index
• Gini index for a given node t:
𝑛
2
𝑔𝑖𝑛𝑖(𝐷𝑡) = 1 − 𝑝𝑗 𝑡
𝑗=1
C1 0 C1 1 C1 2 C1 3
C2 6 C2 5 C2 4 C2 3
Gini=0.000 Gini=0.278 Gini=0.444 Gini=0.500
Computing Gini Index of a Single Node
𝑛
2
𝑔𝑖𝑛𝑖(𝐷𝑡) = 1 − 𝑝𝑗 𝑡
𝑗=1
35
Informatics Engineering | Universitas Surabaya
Tutorial
Calculate the gain in the Gini index when splitting on A and B. Which
attribute would the decision tree induction algorithm choose?
Homework
• Do all the exercises.
• You can write the solution on papers or you can use tools like Excel
or Python and explain in detail step-by-step of your work until it finds
the solution.
• Create a pdf file for your solution and submit it to ULS
• You can upload one more file that you use to do the computation
(.xlsx or .ipynb) along with your .pdf file. Upload those files
separately.
• Note: do not forget to put your Student ID and name at the first page
of the pdf file.
Exercise 1: Loan Borrower
Compute Gini index for the Home owner and Marital status attributes
and find the best split. ID Home owner Marital status Defaulted?
1 Yes Single No
2 No Married No
3 No Single No
4 Yes Married No
5 No Divorced Yes
6 No Single No
7 Yes Divorced No
8 No Single Yes
9 No Married No
10 No Single Yes
Exercise 2: Customer DB training examples
a) Compute Gini index for the overall
collection of training examples
b) Compute Gini index for the Gender
attribute.
c) Compute Gini index for the Car Type
attribute using multiway split.
d) Compute Gini index for the Shirt
Size attribute using multiway split.
e) Which attribute is better? Explain
your answer.
Question?
40
Informatics Engineering | Universitas Surabaya