Machine Learning Classification Techniques
Machine Learning Classification Techniques
COMP9417 1
Introduction
COMP9417 2
Classification
COMP9417 3
Classification
Example: classifying sea bass vs. salmon
Features that can be used: width, length, weight, lightness, fins,
eyes/mouth position, etc.
Question: how to separate these two classes?
Width Sea bass
Salmon
Lightness
COMP9417 4
Classification
Example: Maybe we can find a line that separates the two classes.
Salmon
Lightness
COMP9417 5
Classification
Example: If we find the line that separated the two classes, then how
our algorithm makes prediction?
COMP9417 6
Classification
Example:
Can we do something different than finding the discriminative line (or
some boundary) to be able to separate the two groups?
Salmon
Lightness
COMP9417 7
Classification
Example:
Instead of finding a discriminative line, maybe we can focus on one class at a time and build a model
that describes how that class looks like; and then do the same for the other class. This type of models
are called generative learning algorithm.
Width
Lightness
COMP9417 8
Classification
Generative algorithm: builds some models for each of
the classes and then makes classification predictions
based on looking at the test example and see it is more
similar to which of the models.
– Learns 𝑝(𝑥|𝑦) (and also 𝑝 𝑦 , called class prior)
– So, we can get 𝑝 𝑥, 𝑦 = 𝑝 𝑥 𝑦 𝑝(𝑦)
– It learns the mechanism by which the data has
been generated
COMP9417 9
Classification
COMP9417 10
Linear classification in two dimensions
x2 : Width
Positive class
w Negative class
x1 : Lightness
• We find the line that separates the two class: 𝑎𝑥1 + 𝑏𝑥2 + 𝑐 = 0
• We define a weight vector 𝑤 𝑇 = [𝑎, 𝑏], 𝑥 𝑇 = [𝑥1 , 𝑥2 ]
• So, the line can be defined by 𝑥 𝑇 𝑤 = −𝑐 = 𝑡
• 𝑤 is perpendicular to decision boundary (in direction of positive
class)
• 𝑡 is the decision threshold (if 𝑥 𝑇 𝑤 > t then 𝑥 belongs to positive
class and if 𝑥 𝑇 𝑤 < t then 𝑥 belongs to negative class)
COMP9417 11
Basic Linear Classifier
Salmon
Lightness
COMP9417 12
Basic Linear Classifier
COMP9417 13
Logistic Regression
a probabilistic linear classifier
COMP9417 14
Logistic Regression
COMP9417 15
Logistic Regression
COMP9417 16
Logistic Regression
• In binary classification, we can transform the y values into probability values
(values are in range [0,1])
P(y=1|x)
• We can model this with a s-curve (sigmoid curve)
as above:
1 x
𝑃 𝑦=1𝑥 =
1 + 𝑒 −𝑓(𝑥)
𝑓 𝑥 = 𝑥𝑇𝛽
1
Consequently: 𝑃 𝑦 = 0 𝑥 = 1 − 𝑃 𝑦 = 1 𝑥 = 1 −
1+𝑒 −𝑓(𝑥)
COMP9417 17
Logistic Regression
COMP9417 18
Logistic Regression
1
𝑃 𝑦 = 1 𝑥 = 𝑇𝛽
1 + 𝑒 −𝑥
• Now we have a linear solution to our problem, and this is what makes
Logistic Regression a linear model.
COMP9417 19
Logistic Regression Parameter Estimation
COMP9417 20
Logistic Regression Parameter Estimation
𝑚
1
𝐽 𝛽 = − [𝑦 (𝑖) log ℎ𝛽 𝑥 𝑖 + 1−𝑦 𝑖 log(1 − ℎ𝛽 𝑥 𝑖 )]
𝑚
𝑖=1
• This cost function is convex (single global minimum) but there is no closed
form solution
• We can use the Gradient Descent algorithm.
• We can not use a similar cost function as we used in linear regression here,
because it will result a non-convex function with many local minimums and
would be very difficult to find the global minimum.
COMP9417 21
Logistic Regression
Pros:
– Relatively easy to implement
– Easy to interpret
– Relatively fast at training and very fast at testing
– Can easily extend to multi-classes
– Provide probabilistic predictions
Cons:
–Prone to overfitting in high-dimensional data (one remedy:
regularization)
– It provides a linear decision boundary. For non-linear decision
boundaries, feature transformation is required
– Requires moderate or no correlation (collinearity) between input
variables, and may lead to poor model (dimensionality reduction is
useful)
– Sensitive to outlier
COMP9417 22
How solve a task with machine learning
COMP9417 23
Generalisation
Generalisation is a key objective of machine learning.
E.g., when we train a model to classify between dogs and cats, if the
model is provided with a dog image dataset with only two breeds, it
may obtain a good performance during training; But, it possibly gets a
low classification score when it is tested by other breeds of dogs as
well.
COMP9417 24
Generalisation
COMP9417 25
Train-and-test
1. Holdout method:
Train Test
COMP9417 26
Train-and-test
Test
Iteration 1
Iteration 2
Iteration 3
Iteration 4
.
.
.
Iteration m
COMP9417 27
Train-and-test
Iteration 1
Iteration 2
Iteration 3
Iteration 4
.
.
.
Iteration 7
COMP9417 28
Cross-validation
COMP9417 29
Cross-validation
Validation set: To make the hyperparameter tuning and model selection
independent from the test set, we define another set within the train set
COMP9417 30
Data Types
COMP9417 31
Data Types
COMP9417 32
Data Types
COMP9417 33
Binary Classification task
COMP9417 34
Evaluation of error
COMP9417 35
Contingency table
Positive Negative
Positive
Negative
COMP9417 36
Classification Accuracy
where 𝑇𝑒𝑠𝑡 is a test set and 𝐼[] is the indicator function which is 1 iff its
argument evaluates to true, and 0 otherwise.
COMP9417 37
Other evaluation metrics
Precision/correctness
– is the number of relevant objects classified
correctly divided by the total number of relevant
objects classified
𝑇𝑃
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 =
𝑇𝑃 + 𝐹𝑃
𝑇𝑃
𝑅𝑒𝑐𝑎𝑙𝑙 =
𝑇𝑃 + 𝐹𝑁
COMP9417 38
Other evaluation metrics
COMP9417 39
Other evaluation metrics
𝑇𝑃
• 𝑇𝑃𝑅 = 𝑇𝑃+𝐹𝑁
𝐹𝑃
• 𝐹𝑃𝑅 = 𝐹𝑃+𝑇𝑁 AUC
COMP9417 40
Missing Value: An issue to consider
COMP9417 41
Missing Values
o Human errors
o Sensor errors
o Software bugs
o Faulty preprocessing
o…
COMP9417 42
Missing Values
COMP9417 43
Missing Values
COMP9417 44
Missing Values
COMP9417 45
Missing Values
COMP9417 46
Missing Values
COMP9417 47
Missing Values
COMP9417 48
Nearest Neighbor Algorithm for Classification
COMP9417 49
Nearest Neighbour
Nearest Neighbour
1
Where | 𝑧 |𝑝 = (σ𝑑𝑗=1 |𝑧𝑗 |𝑝 ) ൗ𝑝 is the 𝑝 − 𝑛𝑜𝑟𝑚 (sometimes denoted 𝐿𝑝
norm) of the vector 𝑧.
COMP9417 51
Minkowski distance
𝐷𝑖𝑠1 𝑥, 𝑦 = |𝑥𝑗 − 𝑦𝑗 |
𝑗=1
COMP9417 52
Minkowski distance
• If we now let 𝑝 grow larger, the distance will be more and more
dominated by the largest coordinate-wise distance, from which we
can infer that 𝐷𝑖𝑠∞ = 𝑚𝑎𝑥𝑗 |𝑥𝑗 − 𝑦𝑗 |; this is also called Chebyshev
distance.
• You will sometimes see references to the 0-norm (or 𝐿0 norm) which
counts the number of non-zero elements in a vector. The
corresponding distance then counts the number of positions in which
vectors x and y differ. This is not strictly a Minkowski distance;
however, we can define it as:
𝑑 𝑑
COMP9417 53
Minkowski distance
COMP9417 54
Circles and ellipses
COMP9417 55
Distance metric
Distance metric: Given an instance space 𝒳 , a distance metric is a
function 𝐷𝑖𝑠 ∶ 𝒳 × 𝒳 → 0, ∞ such that for any 𝑥, 𝑦, 𝑧 ∈ 𝒳:
1. distances between a point and itself are zero: 𝐷𝑖𝑠(𝑥, 𝑥) = 0
2. all other distances are larger than zero: if 𝑥 ≠ 𝑦 then 𝐷𝑖𝑠(𝑥, 𝑦) > 0
3. distances are symmetric: 𝐷𝑖𝑠(𝑦, 𝑥) = 𝐷𝑖𝑠(𝑥, 𝑦)
4. detours can not shorten the distance (triangle inequality):
𝐷𝑖𝑠(𝑥, 𝑧) ≤ 𝐷𝑖𝑠(𝑥, 𝑦) + 𝐷𝑖𝑠(𝑦, 𝑧)
o It can be shown that triangle inequality does not hold for 𝑝 < 1
COMP9417 57
Exemplar
• It can be shown that the arithmetic mean is the unique point that
minimizes the sum of squared Euclidean distances to those data
points
COMP9417 58
Means and distances
COMP9417 59
Distance-based models
COMP9417 60
Nearest Centroid Classifier
COMP9417 61
Nearest Centroid Classifier
• This is a classifier based on minimum distance principle, where the
class exemplars are just the centroids (or means)
COMP9417 62
Basic Linear Classifier & Nearest Centroid
Classifier
• The basic linear classifier is distance-based.
• An alternative, distance-based way to classify instances without
direct reference to a decision boundary is by the following decision
rule: if 𝑥 is nearest to 𝜇 ⊕ then classify it as positive, otherwise as
negative; or equivalently, classify an instance to the class of the
nearest exemplar.
• If we use Euclidean distance as our closeness measure, simple
geometry tells us we get exactly the same decision boundary.
• So the basic linear classifier can be interpreted from a distance-
based perspective as constructing exemplars that minimise squared
Euclidean distance within each class, and then applying a nearest-
exemplar decision rule.
COMP9417 63
Nearest Centroid Classifier
• What happens if a class has more than one mode? (similar to the
image)
1. If there is only one centroid per class, then it will perform poorly
2. If we can somehow find different modes, we can define one
centroid per each mode which helps the classifier
generalizability
(1) (2)
COMP9417 64
Nearest Centroid Classifier
Advantages:
o Simple
o Fast
o works well when classes are compact and far from each
other.
COMP9417 65
Nearest Centroid Classifier
Disadvantages:
o For complex classes (e.g., Multimodal, non-spherical) may
give very poor results
o Can not handle outliers and noisy data well
o Can not handle missing data
COMP9417 66
Nearest neighbour classification
COMP9417 67
Nearest neighbour classification
• Related to the simplest form of learning: rote learning or
memorisation
o Training instances are searched for instance that most
closely resembles new or query instance
o The instances themselves represent the knowledge
o Called: instance-based, memory-based learning or case-
based learning; often a form of local learning
COMP9417 68
Nearest neighbour classification
COMP9417 69
Nearest Neighbour
𝑘-Nearest neighbour:
o Given 𝑥𝑞 , take vote among its 𝑘 nearest neighbours (if
discrete-valued target function) (see next slide)
o take mean of 𝑓 values of 𝑘 nearest neighbours (if real-
valued)
σ𝑘𝑗=1 𝑓(𝑥𝑗 )
መ 𝑞) ←
𝑓(𝑥
𝑘
COMP9417 70
K-Nearest Neighbour (KNN) Algorithm
Training algorithm:
o For each training example 𝑥𝑗 , 𝑓(𝑥𝑗 ) , add the example to the
list training _examples.
Classification algorithm:
– Given a query instance 𝑥𝑞 to be classified,
o Let 𝑥1 ,…,𝑥𝑘 be the 𝑘 instances from training examples that
are nearest to 𝑥𝑞 by the distance function
o Return
𝑘
መ 𝑞 ) ← arg max 𝛿(𝜐, 𝑓(𝑥𝑗 ))
𝑓(𝑥
𝜐∈𝑉
𝑗=1
COMP9417 71
Distance function again
COMP9417 72
Distance function again
COMP9417 73
KNN Example
• What is the predicted class for the green point given the data for?
k=12
hℎ𝑡𝑡𝑝𝑠://𝑡𝑜𝑤𝑎𝑟𝑑𝑠𝑑𝑎𝑡𝑎𝑠𝑐𝑖𝑒𝑛𝑐𝑒.
t t p s : over over t o w a. r d s d a. t a. s c i. e𝑐𝑜𝑚/𝑘𝑛𝑛
n c e . c o m over k n n minus
− 𝑢𝑠𝑖𝑛𝑔 u s i. n g minus
− 𝑠𝑐𝑖𝑘𝑖𝑡 s c i. k −
− 𝑙𝑒𝑎𝑟𝑛 i. t 𝑐6𝑏𝑒𝑑765𝑏𝑒75
minus l e a. r n minus
c 6 b e d 765 b e 75
COMP9417 74
Normalisation and other issues
COMP9417 75
Normalisation and other issues
• Min-max normalisation:
′
𝑥𝑗𝑟 − min(𝑥𝑗𝑟 )
𝑥𝑗𝑟 =
m𝑎𝑥 𝑥𝑗𝑟 − min(𝑥𝑗𝑟 )
′
where 𝑥𝑗𝑟 is the actual value of attribute/feature 𝑟 and 𝑥𝑗𝑟 is the
normalised value.
COMP9417 76
When To Consider Nearest Neighbour
COMP9417 77
K-Nearest Neighbour
Advantages:
• Statisticians have used KNN since early 1950s
• Can be very accurate
• Training is very fast
• Can learn complex target functions
COMP9417 78
K-Nearest Neighbour
Disadvantages:
• 1NN perfectly separates training data, so low bias but high variance
COMP9417 80
Distance-Weighted KNN
COMP9417 81
Distance-Weighted KNN
For real-valued target functions replace the final line of the algorithm
by:
𝑘
መ 𝑞 ) ← σ𝑖=1𝑘𝑤𝑖 𝑓(𝑥𝑖 )
𝑓(𝑥 σ 𝑖=1 𝑤𝑖
Now we can consider using all the training examples instead of just 𝑘:
o using all examples (i.e., when 𝑘 = 𝑚 and 𝑚 is number of
training samples) with the rule above is called Shepard’s
method
COMP9417 82
Evaluation
Solution:
Leave-one-out cross-validation (LOOCV) – leave out each example and
predict it given the rest:
𝑥1 , 𝑦1 , 𝑥2 , 𝑦2 , … , 𝑥𝑖−1 , 𝑦𝑖−1 , 𝑥𝑖+1 , 𝑦𝑖+1 , … , (𝑥𝑚 , 𝑦𝑚 )
COMP9417 83
KNN Computational Time
• KNN uses the training data as exemplars, so using simple search for
prediction is 𝑂(𝑛)!
• There are algorithms to search for neighbours more efficiently with 𝑂(log 𝑛)
but they do not work very well for above 10 dimensions (more than 10
features/attributes)
COMP9417 84
When is KNN meaningful?
You may think that this is an exceptional example, and this doesn’t really
happen in practice!!
COMP9417 85
Curse of Dimensionality
COMP9417 86
Curse of Dimensionality
Curse of Dimensionality
COMP9417 87
Curse of Dimensionality
COMP9417 88
Curse of Dimensionality
One approach:
COMP9417 89
Curse of Dimensionality
See Moore and Lee (1994) “Efficient Algorithms for Minimizing Cross Validation Error”
COMP9417 90
Instance-based (nearest-neighbour) learning
COMP9417 91
Dealing with noise
IB3 (Instance-Based Learning 3): store classification performance information with each instance &
only use in prediction if above a threshold
COMP9417 92
KNN Example
COMP9417 93
KNN Example
• Distance-based learning
COMP9417 95
Acknowledgements
• Material derived from slides for the book
“Elements of Statistical Learning (2nd Ed.)” by T. Hastie, R. Tibshirani & J. Friedman. Springer
(2009) [Link]
• Material derived from slides for the book
“Machine Learning: A Probabilistic Perspective” by P. Murphy MIT Press (2012)
[Link]
• Material derived from slides for the book “Machine Learning” by P. Flach Cambridge University
Press (2012) [Link]
• Material derived from slides for the book
“Bayesian Reasoning and Machine Learning” by D. Barber Cambridge University Press (2012)
[Link]
• Material derived from slides for the book “Machine Learning” by T. Mitchell McGraw-Hill (1997)
[Link] [Link]/~tom/[Link]
• Material derived from slides for the course “Machine Learning” by A. Srinivasan BITS Pilani, Goa,
India (2016)
COMP9417 96