Data Mining Classification - Basic Concepts and Techniques
Data Mining Classification - Basic Concepts and Techniques
RV College
Engineering world
of
Engineering
UNIT -3
▪ Supervised Learning
▪ Decision Tree Classifier
▪ Model Overfitting
▪ Model Selection
R V College of Engineering 1
RV College of
Engineering
R V College of Engineering 2
Classification: Definition
RV College of
Engineering
l Task:
– Learn a model that maps each attribute set x
into one of the predefined class labels y
R V College of Engineering 3
RV College of
Engineering Examples of Classification Task
R V College of Engineering 4
RV College of
General Approach for Building
Engineering Classification Model
R V College of Engineering 5
RV College of
General Approach for Building
Engineering Classification Model
R V College of Engineering 6
Classification Techniques
RV College of
Engineering
Base Classifiers
– Decision Tree based Methods
– Rule-based Methods
– Nearest-neighbor
– Naïve Bayes and Bayesian Belief Networks
– Support Vector Machines
– Neural Networks, Deep Neural Nets
Ensemble Classifiers
– Boosting, Bagging, Random Forests
R V College of Engineering 7
RV College of
Engineering
R V College of Engineering 8
RV College of
Engineering
Example of a Decision Tree
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
< 80K > 80K
8 No Single 85K Yes
9 No Married 75K No NO YES
10 No Single 90K Yes
10
R V College of Engineering 9
RV College of
Engineering Apply Model to Test Data
Test Data
Start from the root of tree.
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Single, Divorced Married
Income NO
< 80K > 80K
NO YES
R V College of Engineering 10
Apply Model to Test Data
RV College of
Engineering
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Single, Divorced Married
Income NO
< 80K > 80K
NO YES
R V College of Engineering 11
RV College of
Engineering
Apply Model to Test Data
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Single, Divorced Married
Income NO
< 80K > 80K
NO YES
R V College of Engineering 12
Apply Model to Test Data
RV College of
Engineering
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Single, Divorced Married
Income NO
< 80K > 80K
NO YES
R V College of Engineering 13
Apply Model to Test Data
RV College of
Engineering
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Single, Divorced Married
Income NO
< 80K > 80K
NO YES
R V College of Engineering 14
Apply Model to Test Data
RV College of
Engineering
Test Data
Home Marital Annual Defaulted
Owner Status Income Borrower
No Married 80K ?
Home 10
Yes Owner No
NO MarSt
Single, Divorced Married Assign Defaulted to
“No”
Income NO
< 80K > 80K
NO YES
R V College of Engineering 15
RV College of
Another Example of Decision
Engineering 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
9 No Married 75K No There could be more than one tree that
fits the same data!
10 No Single 90K Yes
10
R V College of Engineering 16
RV College of
Decision Tree Classification
Engineering Task
6 No Medium 60K No
Training Set
Apply Decision
Tid Attrib1 Attrib2 Attrib3 Class
Model Tree
11 No Small 55K ?
15 No Large 67K ?
10
Test Set
R V College of Engineering 17
RV College of
Engineering Decision Tree Induction
Many Algorithms:
– Hunt’s Algorithm (one of the earliest)
– CART
– ID3, C4.5
– SLIQ,SPRINT
R V College of Engineering 18
RV College of
General Structure of Hunt’s
Engineering Algorithm
Home Marital Annual Defaulted
l Let Dt be the set of training ID
Owner Status Income Borrower
records that reach a node t 1 Yes Single 125K No
2 No Married 100K No
3 No Single 70K No
l General Procedure:
4 Yes Married 120K No
– If Dt contains records that 5 No Divorced 95K Yes
R V College of Engineering 19
RV College of
Engineering
Hunt’s Algorithm
Home Marital Annual Defaulted
Home ID
Owner Status Income Borrower
Owner
1 Yes Single 125K No
Yes No
Defaulted = No 2 No Married 100K No
Defaulted = No Defaulted = No 3 No Single 70K No
(7,3)
(3,0) (4,3) 4 Yes Married 120K No
Yes No
Status
(3,0) Single,
Married
Defaulted = No Marital Divorced
Status
Defaulted = No
(3,0) Single,
Married
Annual
Divorced Income
(3,0)
Defaulted = Yes Defaulted = No < 80K >= 80K
Yes No
Status
(3,0) Single,
Married
Defaulted = No Marital Divorced
Status
Defaulted = No
(3,0) Single,
Married
Annual
Divorced Income
(3,0)
Defaulted = Yes Defaulted = No < 80K >= 80K
Yes No
Status
(3,0) Single,
Married
Defaulted = No Marital Divorced
Status
Defaulted = No
(3,0) Single,
Married
Annual
Divorced Income
(3,0)
Defaulted = Yes Defaulted = No < 80K >= 80K
Yes No
Status
(3,0) Single,
Married
Defaulted = No Marital Divorced
Status
Defaulted = No
(3,0) Single,
Married
Annual
Divorced Income
(3,0)
Defaulted = Yes Defaulted = No < 80K >= 80K
R V College of Engineering 25
RV College of
Test Condition for Nominal
Engineering Attributes
Multi-way split:
Marital
– Use as many partitions as Status
distinct values.
Binary split:
– Divides values into two subsets
R V College of Engineering 26
RV College of
Test Condition for Ordinal
Engineering Attributes
l Multi-way split: Shirt
Size
– Use as many partitions
as distinct values
Small
Medium Large Extra Large
Shirt Shirt
l Binary split: Size Size
{Small, {Medium,
Large} Extra Large}
R V College of Engineering 27
RV College of
Test Condition for
Engineering Continuous Attributes
Annual Annual
Income Income?
> 80K?
< 10K > 80K
Yes No
R V College of Engineering 28
RV College of
Splitting Based on Continuous
Engineering Attributes
Different ways of handling
– Discretization to form an ordinal categorical
attribute
Ranges can be found by equal interval bucketing,
equal frequency bucketing (percentiles), or
clustering.
◆ Static – discretize once at the beginning
C0: 5 C0: 9
C1: 5 C1: 1
R V College of Engineering 31
RV College of
Engineering Measures of Node Impurity
l Gini Index 𝑐−1 Where 𝒑𝒊 𝒕 is the frequency
2 of class 𝒊 at node t, and 𝒄 is
𝐺𝑖𝑛𝑖 𝐼𝑛𝑑𝑒𝑥 = 1 − 𝑝𝑖 𝑡
the total number of classes
𝑖=0
l Entropy 𝑐−1
l Misclassification error
𝐶𝑙𝑎𝑠𝑠𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑒𝑟𝑟𝑜𝑟 = 1 − max[𝑝𝑖 (𝑡)]
R V College of Engineering 32
RV College of
Engineering Finding the Best Split
1. Compute impurity measure (P) before splitting
2. Compute impurity measure (M) after splitting
l Compute impurity measure of each child node
l M is the weighted impurity of child nodes
Gain = P - M
R V College of Engineering 33
RV College of
Engineering Finding the Best Split
Before Splitting: C0 N00
P
C1 N01
A? B?
Yes No Yes No
M1 M2
Gain = P – M1 vs P – M2
R V College of Engineering 34
RV College of
Engineering Measure of Impurity: GINI
Gini Index for a given node 𝒕
𝑐−1
𝐺𝑖𝑛𝑖 𝐼𝑛𝑑𝑒𝑥 = 1 − 𝑝𝑖 𝑡 2
𝑖=0
Where 𝒑𝒊 𝒕 is the frequency of class 𝒊 at node 𝒕, and 𝒄 is the total
number of classes
R V College of Engineering 35
RV College of
Engineering Measure of Impurity: GINI
Gini Index for a given node t :
𝑐−1
𝐺𝑖𝑛𝑖 𝐼𝑛𝑑𝑒𝑥 = 1 − 𝑝𝑖 𝑡 2
𝑖=0
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
R V College of Engineering 36
Computing Gini Index of a
Single Node
RV College of
Engineering
𝑐−1
𝐺𝑖𝑛𝑖 𝐼𝑛𝑑𝑒𝑥 = 1 − 𝑝𝑖 𝑡 2
𝑖=0
R V College of Engineering 37
RV College of
Computing Gini Index for a
Engineering Collection of Nodes
l When a node 𝑝 is split into 𝑘 partitions (children)
𝑘
𝑛𝑖
𝐺𝐼𝑁𝐼𝑠𝑝𝑙𝑖𝑡 = 𝐺𝐼𝑁𝐼(𝑖)
𝑛
𝑖=1
R V College of Engineering 38
RV College of Binary Attributes: Computing
Engineering
GINI Index
Splits into two partitions (child nodes)
Effect of Weighing partitions:
– Larger and purer partitions are sought
Parent
B? C1 7
Yes No C2 5
Gini = 0.486
Node N1 Node N2
Gini(N1)
= 1 – (5/6)2 – (1/6)2 N1 N2 Weighted Gini of N1 N2
= 0.278
C1 5 2 = 6/12 * 0.278 +
Gini(N2) C2 1 4 6/12 * 0.444
= 1 – (2/6)2 – (4/6)2 = 0.361
Gini=0.361
= 0.444 Gain = 0.486 – 0.361 = 0.125
R V College of Engineering 39
RV College of
Categorical Attributes:
Engineering Computing Gini Index
l For each distinct value, gather counts for each class in
the dataset
l Use the count matrix to make decisions
R V College of Engineering 40
RV College of
Continuous Attributes:
Engineering Computing Gini Index
l Use Binary Decisions based on one ID
Home
Owner
Marital
Status
Annual
Income
Defaulted
value
1 Yes Single 125K No
l Several Choices for the splitting value 2 No Married 100K No
Annual Income ?
gather count matrix and compute
its Gini index
≤ 80 > 80
– Computationally Inefficient!
Repetition of work. Defaulted Yes 0 3
Defaulted No 3 4
R V College of Engineering 41
RV College of
Continuous Attributes: Computing
Engineering Gini Index...
l For efficient computation: for each attribute,
– Sort the attribute on values
– Linearly scan these values, each time updating the count matrix
and computing gini index
– Choose the split position that has the least gini index
No 0 7 1 6 2 5 3 4 3 4 3 4 3 4 4 3 5 2 6 1 7 0
Gini 0.420 0.400 0.375 0.343 0.417 0.400 0.300 0.343 0.375 0.400 0.420
R V College of Engineering 42
RV College of
Continuous Attributes: Computing
Engineering Gini Index...
l For efficient computation: for each attribute,
– Sort the attribute on values
– Linearly scan these values, each time updating the count matrix
and computing gini index
– Choose the split position that has the least gini index
No 0 7 1 6 2 5 3 4 3 4 3 4 3 4 4 3 5 2 6 1 7 0
Gini 0.420 0.400 0.375 0.343 0.417 0.400 0.300 0.343 0.375 0.400 0.420
R V College of Engineering 43
RV College of
Continuous Attributes:
Engineering Computing Gini Index...
l For efficient computation: for each attribute,
– Sort the attribute on values
– Linearly scan these values, each time updating the count matrix
and computing gini index
– Choose the split position that has the least gini index
No 0 7 1 6 2 5 3 4 3 4 3 4 3 4 4 3 5 2 6 1 7 0
Gini 0.420 0.400 0.375 0.343 0.417 0.400 0.300 0.343 0.375 0.400 0.420
R V College of Engineering 44
RV College of
Continuous Attributes: Computing
Engineering Gini Index...
l For efficient computation: for each attribute,
– Sort the attribute on values
– Linearly scan these values, each time updating the count matrix
and computing gini index
– Choose the split position that has the least gini index
No 0 7 1 6 2 5 3 4 3 4 3 4 3 4 4 3 5 2 6 1 7 0
Gini 0.420 0.400 0.375 0.343 0.417 0.400 0.300 0.343 0.375 0.400 0.420
R V College of Engineering 45
RV College of Continuous Attributes: Computing
Engineering
Gini Index...
l For efficient computation: for each attribute,
– Sort the attribute on values
– Linearly scan these values, each time updating the count matrix
and computing gini index
– Choose the split position that has the least gini index
No 0 7 1 6 2 5 3 4 3 4 3 4 3 4 4 3 5 2 6 1 7 0
Gini 0.420 0.400 0.375 0.343 0.417 0.400 0.300 0.343 0.375 0.400 0.420
R V College of Engineering 46
RV College of
Engineering Measure of Impurity: Entropy
l Entropy at a given node 𝒕
𝑐−1
𝑐−1
R V College of Engineering 48
RV College of
Computing Information Gain
Engineering After Splitting
l Information Gain:
𝑘
𝑛𝑖
𝐺𝑎𝑖𝑛𝑠𝑝𝑙𝑖𝑡 = 𝐸𝑛𝑡𝑟𝑜𝑝𝑦 𝑝 − 𝐸𝑛𝑡𝑟𝑜𝑝𝑦(𝑖)
𝑛
𝑖=1
R V College of Engineering 49
RV College of
Problem with large number of
Engineering 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
l Gain Ratio:
𝑘
𝐺𝑎𝑖𝑛𝑠𝑝𝑙𝑖𝑡 𝑛𝑖 𝑛𝑖
𝐺𝑎𝑖𝑛 𝑅𝑎𝑡𝑖𝑜 = 𝑆𝑝𝑙𝑖𝑡 𝐼𝑛𝑓𝑜 = − 𝑙𝑜𝑔2
𝑆𝑝𝑙𝑖𝑡 𝐼𝑛𝑓𝑜 𝑛 𝑛
𝑖=1
R V College of Engineering 51
RV College of
Engineering
Gain Ratio
l Gain Ratio:
𝑘
𝐺𝑎𝑖𝑛𝑠𝑝𝑙𝑖𝑡 𝑛𝑖 𝑛𝑖
𝐺𝑎𝑖𝑛 𝑅𝑎𝑡𝑖𝑜 = 𝑆𝑝𝑙𝑖𝑡 𝐼𝑛𝑓𝑜 = 𝑙𝑜𝑔2
𝑆𝑝𝑙𝑖𝑡 𝐼𝑛𝑓𝑜 𝑛 𝑛
𝑖=1
R V College of Engineering 52
RV College of
Measure of Impurity:
Engineering Classification Error
𝐸𝑟𝑟𝑜𝑟 𝑡 = 1 − max[𝑝𝑖 𝑡 ]
𝑖
R V College of Engineering 53
RV College of
Computing Error of a Single
Engineering Node
𝐸𝑟𝑟𝑜𝑟 𝑡 = 1 − max[𝑝𝑖 𝑡 ]
𝑖
R V College of Engineering 54
Comparison among Impurity
Measures
RV College of
Engineering
R V College of Engineering 55
RV College of Misclassification Error vs Gini
Engineering
Index
A? Parent
C1 7
Yes No
C2 3
Node N1 Node N2 Gini = 0.42
Gini(N1) N1 N2
= 1 – (3/3)2 – (0/3)2 Gini(Children)
C1 3 4 = 3/10 * 0
=0
C2 0 3 + 7/10 * 0.489
Gini(N2) Gini=0.342 = 0.342
= 1 – (4/7)2 – (3/7)2
= 0.489 Gini improves but
error remains the
same!!
R V College of Engineering 56
RV College of Misclassification Error vs Gini
Engineering
Index
A? Parent
C1 7
Yes No
C2 3
Node N1 Node N2 Gini = 0.42
N1 N2 N1 N2
C1 3 4 C1 3 4
C2 0 3 C2 1 2
Gini=0.342 Gini=0.416
R V College of Engineering 57
RV College of
Decision Tree Based
Engineering Classification
l Advantages:
– Relatively inexpensive to construct
– Extremely fast at classifying unknown records
– Easy to interpret for small-sized trees
– Robust to noise (especially when methods to avoid overfitting are
employed)
– Can easily handle redundant attributes
– Can easily handle irrelevant attributes (unless the attributes are
interacting)
l Disadvantages: .
– Due to the greedy nature of splitting criterion, interacting attributes (that
can distinguish between classes together but not individually) may be
passed over in favor of other attributed that are less discriminating.
– Each decision boundary involves only a single attribute
R V College of Engineering 58
RV College of
Engineering
Handling interactions
R V College of Engineering 59
RV College of Handling interactions
Engineering
R V College of Engineering 60
RV College of
Handling interactions given irrelevant
Engineering
attributes
R V College of Engineering 61
RV College of
Engineering
Generalization errors: Expected error of a model over random selection of records from same distribution
3 No Small 70K No
6 No Medium 60K No
Training Set
Apply
Tid Attrib1 Attrib2 Attrib3 Class Model
11 No Small 55K ?
15 No Large 67K ?
10
Test Set
RV College of
Engineering
Example Data Set
o : 5400 instances
• Generated from a uniform
distribution
Decision Tree
Decision Tree
•As the model becomes more and more complex, test errors can start increasing even
though training error may be decreasing
Underfitting: when model is too simple, both training and test errors are large
Overfitting: when model is too complex, training error is small but test error is large
RV College of
Model Overfitting – Impact of Training
Engineering
Data Size
• Increasing the size of training data reduces the difference between training and
testing errors at a given size of model
RV College of
Model Overfitting – Impact of Training
Engineering
Data Size
• Increasing the size of training data reduces the difference between training and
testing errors at a given size of model
RV College of
Engineering Reasons for Model Overfitting
Not enough training data
R V College of Engineering 78
RV College of
Engineering Model Selection
Drawback:
– Less data available for training
RV College of Model Selection:
Engineering Incorporating Model Complexity
e(TL) = 4/24
+: 3 +: 5 +: 1 +: 3 +: 3 e(TR) = 6/24
-: 0 -: 2 -: 4 -: 0 -: 6
+: 3 +: 2 +: 0 +: 1 +: 3 +: 0 =1
-: 1 -: 1 -: 2 -: 2 -: 1 -: 5
Resubstitution Estimate:
– Using training error as an optimistic estimate of
generalization error
– Referred to as optimistic error estimate
e(TL) = 4/24
e(TR) = 6/24
+: 3 +: 5 +: 1 +: 3 +: 3
-: 0 -: 2 -: 4 -: 0 -: 6
+: 3 +: 2 +: 0 +: 1 +: 3 +: 0
-: 1 -: 1 -: 2 -: 2 -: 1 -: 5
A1 A4
A2 A3
Purpose:
– To estimate performance of classifier on previously
unseen data (test set)
Holdout
– Reserve k% for training and (100-k)% for testing
– Random subsampling: repeated holdout
Cross validation
– Partition data into k disjoint subsets
– k-fold: train on k-1 partitions, test on the remaining one
– Leave-one-out: k=n
RV College of
Engineering Cross-validation Example
3-fold cross-validation
RV College of
Engineering
Variations on Cross-validation
Repeated cross-validation
– Perform cross-validation a number of times
– Gives an estimate of the variance of the
generalization error
Stratified cross-validation
– Guarantee the same percentage of class
labels in training and test
– Important when classes are imbalanced and
the sample is small
Use nested cross-validation approach for model
selection and evaluation