Class10 14 PatternClassification - 13 24sept2019
Class10 14 PatternClassification - 13 24sept2019
Data Modeling
Data Science
• Multi-disciplinary field that uses scientific methods,
processes, algorithms and systems to extract
knowledge and insight from structured and
unstructured data
• Central concept is gaining insight from data
• Machine learning uses data to extract knowledge
Data Preprocessing
Data
Feature
Database Cleaning and
Representation
Cleansing
2
1
24-09-2019
Data Science
• Multi-disciplinary field that uses scientific methods,
processes, algorithms and systems to extract
knowledge and insight from structured and
unstructured data
• Central concept is gaining insight from data
• Machine learning uses data to extract knowledge
Data Preprocessing
Data
Feature
Database Cleaning and
Representation
Cleansing
3
2
24-09-2019
Data Science
• Multi-disciplinary field that uses scientific methods,
processes, algorithms and systems to extract
knowledge and insight from structured and
unstructured data
• Central concept is gaining insight from data
• Machine learning uses data to extract knowledge
Data Preprocessing
Data
Feature
Database Cleaning and
Representation
Cleansing
5
3
24-09-2019
Pattern Classification
Classification
• Problem of identifying to which of a set of categories a
new observation belongs
• Predicts categorical labels
• Example:
– Assigning a given email to the "spam" or "non-spam"
class
– Assigning a diagnosis (disease) to a given patient based
on observed characteristics of the patient
• Classification is a two step process
– Step1: Building a classifier (data modeling)
• Learning from data (training phase)
– Step2: Using classification model for classification
• Testing phase
4
24-09-2019
10
5
24-09-2019
2-class Classification
• Example: Classifying a person as child or adult
Weight (x2)
Adult
Height, x1 Class
Adult/Child
Classifier Child
Weight, x2
Adult :Class C1
Child :Class C2 Height (x1)
x = [x1 x2]T
11
Weight
in Kg
Height in cm 12
6
24-09-2019
13
7
24-09-2019
15
Feature
Training extraction
98 28.43 Child Classifier
Phase
Feature
extraction
183 90 Adult
Feature
extraction
163 67.45 Adult
16
8
24-09-2019
17
Feature
extraction Training Examples
90 21.5 Child
Feature
extraction
100 32.45 Child
Class label
Feature
(Adult)
Training extraction
98 28.43 Child Classifier
Phase
Feature
extraction
183 90 Adult
Feature
extraction
163 67.45 Adult
Feature
extraction
Testing
150 50.6
Phase
18
9
24-09-2019
x2 x2 x2
x1 x1 x1
Linearly Nonlinearly Overlapping
separable separable classes
classes classes
19
20
10
24-09-2019
Image Classification
Tiger
Giraffe
Horse
Bear
22
11
24-09-2019
23
Nearest-Neighbour Method
• Training data with N samples: D {x n , y n }n 1 ,
N
x n R d and y n {1, 2, , M }
– d: dimension of input example
– M: Number of classes
• Step 1: Compute Euclidian distance for a test example
x with every training examples, x1, x2, …, xn, …, xN
Euclidean distance x n x
(x n x)T (x n x)
x2 d
T
x [ x1 x2 ]
(x
i 1
ni xi ) 2
x1
24
12
24-09-2019
Nearest-Neighbour Method
• Training data:D {x n , y n }n 1 ,
N
x n R d and yn {1, 2, , M }
– d: dimension of input example
– M: Number of classes
• Step 1: Compute Euclidian distance for a test example
x with every training examples, x1, x2, …, xn, …, xN
• Step 2: Sort the examples
in the training set in the
ascending order of the
x2 distance to x
Weight
in Kg
Height in cm
• Step 1: Compute Euclidian distance
(ED) will each training examples
26
13
24-09-2019
Weight
in Kg
Height in cm
• Step 2: Sort the examples in the
training set in the ascending order
of the distance to test example
27
Weight
in Kg
Height in cm
• Step 3: Assign the class of the
training example with the
minimum distance to the test
example
– Class: Adult
28
14
24-09-2019
Nearest-Neighbour Method
• Training data:D {x n , y n }n 1 ,
N
x n R d and yn {1, 2, , M }
– d: dimension of input example
– M: Number of classes
• Step 1: Compute Euclidian distance for a test example
x with every training examples, x1, x2, …, xn, …, xN
• Step 2: Sort the examples
in the training set in the
ascending order of the
x2 distance to x
Weight
in Kg
Height in cm
• Step 1: Compute Euclidian distance
(ED) will each training examples
30
15
24-09-2019
Weight
in Kg
Height in cm
• Step 2: Sort the examples in the
training set in the ascending order
of the distance to test example
31
Weight
in Kg
Height in cm
• Step 3: Assign the class of the
training example with the minimum
distance to the test example
– Class: Adult
32
16
24-09-2019
Euclidean distance x n x
(x n x)T (x n x)
x2 d
T
x [ x1 x2 ] (x
i 1
ni xi ) 2
x1
33
17
24-09-2019
Weight
in Kg
Height in cm
• Consider K=5
• Step 3: Choose the first K=5
examples in the sorted list
35
Weight
in Kg
Height in cm
• Consider K=5
• Step 4: Test example is assigned
the most common class among its
K neighbours
– Class: Adult
36
18
24-09-2019
37
Data Normalization
• Since the distance measure is used, K-NN classifier
require normalising the values of each attribute
• Normalising the training data:
– Compute the minimum and maximum values of each of
the attributes in the training data
– Store the minimum and maximum values of each of the
attributes
– Perform the min-max normalization on training data set’
• Normalizing the test data:
– Use the stored minimum and maximum values of each
of the attributes from training set to normalise the test
examples
• NOTE: Ensure that test examples are not causing out-
of-bound error
38
19
24-09-2019
20
24-09-2019
41
21
24-09-2019
Image Classification
Tiger
Giraffe
Horse
Bear
43
44
22
24-09-2019
45
23
24-09-2019
47
24
24-09-2019
Confusion Matrix
Actual Class
Class1 Class2
(Positive) (Negative)
Predicted
Class1
Class
Class
25
24-09-2019
Confusion Matrix
Actual Class
Class1 Class2
Class Predicted
(Positive) (Negative)
Total test
samples
in class1
51
Confusion Matrix
Actual Class
Class1 Class2
Class Predicted
(Positive) (Negative)
Total test
samples
in class2
52
26
24-09-2019
Confusion Matrix
Actual Class
Class1 Class2
Class Predicted
(Positive) (Negative)
Total test
Class1 True False
Class
samples
(Positive) Positive Positive predicted as
class1
Class2 False True
(Negative) Negative Negative
53
Confusion Matrix
Actual Class
Class1 Class2
Class Predicted
(Positive) (Negative)
54
27
24-09-2019
Accuracy
•
Actual Class
Class1 Class2
(Positive) (Negative)
Predicted
Class1
Class
Class
55
Class1
C11 C21 C31
Class2
C12 C22 C32
28
24-09-2019
Actual Class
Class1
C11 C21 C31 predicted as
class1
Total samples
Class2
C12 C22 C32 predicted as
class2
Total samples
Class2
C13 C23 C33 predicted as
class3
Total samples Total samples Total samples in
Total
in class1 in class2 class3
Actual Class
Class1
C11 C21 C31
Class2
C12 C22 C32
58
29
24-09-2019
30
24-09-2019
Probability Distribution
• Data of a class is represented by a probability
distribution
• For a class whose data is considered to be forming a
single cluster, it can be represented by a normal or
Gaussian distribution
• Multivariate Gaussian distribution:
– Adult-Child class
Weight
in Kg
Height in cm 62
31
24-09-2019
Probability Distribution
• Data of a class is represented by a probability
distribution
• For a class whose data is considered to be forming a
single cluster, it can be represented by a normal or
Gaussian distribution
• Multivariate Gaussian distribution:
– Adult-Child class
– Bivariate
Gaussian p(x)
distribution
– Each example is
sampled from
Gaussian
distribution
Weight
in Kg
Height in cm 63
12 12
Σ
21 2
2
E x1 1 2
Σ
E x1 1 x 2 2
E x 2 2 x1 1
E x 2 2
2
64
32
24-09-2019
33
24-09-2019
C1 C2 • Conditional probability:
px, Ci
p(x | Ci ) (1)
P(Ci )
px, Ci
p(Ci | x) (2)
x P(x)
Ci • From (1) and (2)
CM
p(x | Ci ) P(Ci ) p(Ci | x) P(x)
67
34
24-09-2019
35
24-09-2019
Illustration of ML Method:
Training Set: Adult-Child
• Number of training examples (N) = 20
• Dimension of a training example = 2
• Class label attribute is 3rd dimension
• Class:
– Child (0)
– Adult (1)
Weight
in Kg
Height in cm 71
109.3778 61.3500
61.3500 43.5415
Weight
in Kg
Height in cm 72
36
24-09-2019
109.3778 61.3500
61.3500 43.5415
µ2
µ1 74
37
24-09-2019
38
24-09-2019
77
Class
label
arg max p x μ i , Σ i
i
θM=[μM ΣM]
p(x|µM,ΣM)
78
39
24-09-2019
109.3778 61.3500
Σ1
61.3500 43.5415
– Compute sample mean vector and sample
covariance matrix from training data of
class 2 (Adult)
μ 2 166.0000 67.1150
110.6667 160.5278
Σ2
160.5278 255.4911
79
40
24-09-2019
• Statistical model:
– Unimodal Gaussian density
• Univariate
• Multivariate
p(x)
Weight
in Kg
Height in cm 81
– Unimodal Gaussian
density
• Univariate
• Multivariate
[103.6 30.1]
Height in cm
• The real world data need not be unimodal
– The shape of the density can be arbitrary
– Bayes classifier?
• Multimodal density function
82
41
24-09-2019
Adult-Child Data
[149.7 65.1]
Weight in Kg
Weight in Kg
[117.2 31.5]
Height in cm Height in cm
83
[129.9 32.6]
[101.7 30.1]
Height in cm Height in cm
84
42
24-09-2019
M2
M1
p(x)
Weight
in Kg
Height in cm
85
p (x|Ci ) wq N x | μ q , Σ q
Q
q 1
43
24-09-2019
p (x|Ci ) wq N x | μ q , Σ q
Q
q 1
88
44
24-09-2019
90
45
24-09-2019
j j
j 1
• wq : mixture coefficient or prior probability of component q,
• γq(x) gives the posterior probability of the component q for
the observation x
1
γ1(xn) = 0.99 3 γ1(xm) = 0.08
γ2(xn) = 0.01 γ2(xm) = 0.42
γ3(xn) = 0.00
2 γ3(xm) = 0.34
γ4(xn) = 0.00 4 γ4(xm) = 0.16
91
92
46
24-09-2019
Log likelihood
L (θ i ) ln p (D i | θ i )
1 2 3 4 5 6 7 8 9 10 11 12
Iterations
93
47
24-09-2019
p(x | Ci ) wqN x | μq , Σq
Q
q 1
95
96
48
24-09-2019
p(x | λ1)
λ1
Class
label
Test Example
x
p(x | λ2) Decision (class 1)
logic
λ2
arg max p x i
Class
label i
p(x | λM)
λM
97
98
49
24-09-2019
99
Text Books
1. J. Han and M. Kamber, Data Mining: Concepts and
Techniques, Third Edition, Morgan Kaufmann Publishers,
2011.
100
50