Classification Techniques Overview
Classification Techniques Overview
CMT307 Session 4
Yuhua Li
liy180@[Link]
CMT307 1
Contents
• k-nearest neighbours
• Support Vector Machines
• Decision trees
• Hyperparameter Optimisation
CMT307 2
Classification: Definition
• Given a collection of records (training set )
◦ Each record contains a set of attributes, one of the attributes is the class.
• Find a model for class attribute as a function of the values of other attributes.
• Goal: previously unseen records should be assigned a class as accurately as
possible.
◦ A test set is used to determine the accuracy of the model. Usually, the given data set is
divided into training and test sets, with training set used to build the model and test set
used to evaluate it.
CMT307 3
Classification Example
Refund
4 Marital Taxable
cal cal us Status Income Cheat
i i o
gor gor i nu
ate ate ont ass No Single 75K ?
c c c cl
Tid Refund Marital Taxable Yes Married 50K ?
Status Income Cheat
No Married 150K ?
1 Yes Single 125K No Yes Divorced 90K ?
2 No Married 100K No No Single 40K ?
3 No Single 70K No
No Married 80K ?
4 Yes Married 120K No
10
CMT307
Contents
• k-nearest neighbours
• Support Vector Machines
• Decision trees
• Hyperparameter Optimisation
CMT307 5
k-nearest neighbours
• A type of instance-based learning, or lazy learning
• A nonparametric method
• Can be used for both classification and regression, though
commonly used for classification.
• Learning is very fast (finding k or no training)
◦ Store training examples
CMT307 6
k-NN Classification
• Training Algorithm
◦ Store training data points
k=11
• Set the value for k
k=3
• Prediction Algorithm k=1
◦ To classify a new input vector x:
§ Calculate the distance from x to each of the data points in
training set
§ Find the k training data points which are nearest to x
§ Examine the k-nearest training data points to x and
assign x to the most frequently occurring class
CMT307 7
k -NN Classification
• Advantages
◦ Training is very fast (no training required)
◦ Learn complex target functions
◦ Simple and intuitive, easy to program
◦ Classification accuracy can be very good; can outperform more complex models
• Disadvantages
◦ Slow at query time
◦ Sensitive to noise and irrelevant attributes
CMT307 8
k-NN Issues
• Distance measure
◦ Most common: Euclidean distance
& %)
(
(
K=1
𝑑 𝐱 ! , 𝐱" = % 𝑥!,# − 𝑥",#
#$%
• Choosing k
◦ Increasing k reduces variance, increases bias
K=15
• Feature scaling is needed if features are not
commensurate
CMT307 9
Contents
• K nearest neighbours
• Support Vector Machines
• Hyperparameter Optimisation
• Decision trees
CMT307 10
Support Vector Machines f(x) = 1.5 x1 - x2 - 0.1
x 𝑦1 f(x) < 0
f .1=0
x2 -0
x 1
-
1.5
𝑓(𝑥,𝑤,𝑏)=𝑠𝑖𝑔𝑛(𝑤 * 𝑥+𝑏)
= 0 f(x) > 0
1.5𝑥! − 𝑥" − 0.1 = 0 f (x)
w=(1.5, -1), b=−0.1
CMT307 11
ρ
CMT307 12
Support Vector Machines
• As the 2-d example shown on previous slide, the two classes can be separated
by different lines.
• The points nearest to the separation/decision line (or plane in 3-d or hyperplane
in higher dimension) define the margin(i.e., gap) between classes.
• Those points are called support vectors.
• Support vector machine aims to maximise the margin.
CMT307 13
Sec. 15.1
• Functional margin of dataset is twice the minimum functional margin for any
point
◦ The factor of 2 comes from measuring the whole width of the margin
CMT307 14
Sec. 15.1
• This implies:
𝒘% (𝐱 * −𝐱 + ) = 2
2
𝝆 = 𝐱 * −𝐱 + " =
‖𝒘‖" wT x + b = 0
CMT307 15
Sec. 15.1
CMT307 16
Sec. 15.1
f(x) = ΣαiyixiTx + b
• Notice that it relies on an inner product between the test point x and the support vectors xi
◦ We will return to this later.
• Also keep in mind that solving the optimization problem involved computing the inner products
xiTxj between all pairs of training points.
CMT307 17
Sec. 15.2.1
CMT307 18
Sec. 15.2.1
CMT307 19
Regularisation
• Support vector machines
◦ Training SVM involves regularization by default, its strength of regularization is determined by
the penalty parameter C>0.
+
1 *
min 𝑤 𝑤 + 𝐶 % 𝜉!
2
!$%
◦ Larger values of C: less regularization
§ Fit the training data as well as possible
§ Each individual data point is important to classify correctly
◦ Smaller values of C: more regularization
§ More tolerant of errors on individual data points
CMT307 20
Sec. 15.1
1
0
-1
CMT307 22
Sec. 15.2.3
Non-linear SVMs
• Datasets that are linearly separable (with some noise) work out great:
0 x
0 x
0 x
CMT307 24
Sec. 15.2.3
Φ: x → φ(x)
CMT307 25
Non-linear SVMs: Feature spaces
• SVM uses a kernel function (which is a similarity measure between data points)
to map data from input (feature) space to a higher-dimensional feature space
• Why use kernels?
◦ Make non-separable problem separable.
◦ Map data into better representational space
• Common kernels
◦ Linear 𝐾 x ! , x" = x !* x"
◦ Polynomial 𝐾 x ! , x" = (1 + x !* x" ),
$
x ! "x # $
◦ Radial basis function 𝐾 x ! , x" = 𝑒
-
$%$ =𝑒 -. x ! -x #
xi - xj
CMT307 27
SVM Reading
• Aurélien Géron. Hands-On Machine Learning with Scikit-Learn, Keras, and
TensorFlow, 2nd Edition, 2019.
Chapter 5, Appendix C for a rudimentary introduction.
CMT307 28
Contents
• k-nearest neighbours
• Support Vector Machines
• Decision trees
• Hyperparameter Optimisation
CMT307 29
Example of a Decision Tree
cal cal us
i i o
or or nu
t eg
t eg
nt i
ass Splitting Attributes
ca ca co cl
Tid Refund Marital Taxable
Status Income Cheat
Training Data
CMT307 30
Decision Tree
• A root node that has no incoming edges and zero or more outgoing edges
• An internal node is a test on an attribute
• A branch represents an outcome of the test
• A leaf or terminal node represents a class label
• At each node, one attribute is chosen to split training examples into distinct
classes as much as possible
CMT307 31
Example of Decision Tree
cal cal us
i i o
or or nu
t eg
t eg
nt i
ass Single,
ca ca co cl MarSt
Married Divorced
Tid Refund Marital Taxable
Status Income Cheat
NO Refund
1 Yes Single 125K No No
Yes
2 No Married 100K No
3 No Single 70K No NO TaxInc
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
CMT307 32
Apply Model to Test Data
Test Data
Start from the root of tree. Refund Marital Taxable
Status Income Cheat
No Married 80K ?
Refund 10
Yes No
NO MarSt
Single, Divorced Married
TaxInc NO
< 80K > 80K
NO YES
CMT307 33
Apply Model to Test Data
Test Data
Refund Marital Taxable
Status Income Cheat
No Married 80K ?
Refund 10
Yes No
NO MarSt
Single, Divorced Married
TaxInc NO
< 80K > 80K
NO YES
CMT307 34
Apply Model to Test Data
Test Data
Refund Marital Taxable
Status Income Cheat
No Married 80K ?
Refund 10
Yes No
NO MarSt
Single, Divorced Married
TaxInc NO
< 80K > 80K
NO YES
CMT307 35
Apply Model to Test Data
Test Data
Refund Marital Taxable
Status Income Cheat
No Married 80K ?
Refund 10
Yes No
NO MarSt
Single, Divorced Married
TaxInc NO
< 80K > 80K
NO YES
CMT307 36
Apply Model to Test Data
Test Data
Refund Marital Taxable
Status Income Cheat
No Married 80K ?
Refund 10
Yes No
NO MarSt
Single, Divorced Married
TaxInc NO
< 80K > 80K
NO YES
CMT307 37
Apply Model to Test Data
Test Data
Refund Marital Taxable
Status Income Cheat
No Married 80K ?
Refund 10
Yes No
NO MarSt
Single, Divorced Married Assign Cheat to “No”
TaxInc NO
< 80K > 80K
NO YES
CMT307 38
Decision Tree Algorithms
• Many Algorithms:
◦ Hunt’s (one of the earliest)
◦ CART (Classification And Regression Tree)
◦ ID3 (Iterative Dichotomiser 3)
◦ C4.5 (successor of ID3)
◦ SLIQ (Supervised Learning In Quest), SPRINT
CMT307 39
Choosing the Splitting Attribute
• At each node, available attributes are evaluated on the basis of separating the
classes of the training examples. A Goodness function is used for this purpose.
• Typical goodness functions:
◦ information gain (Entropy ) used in (ID3/C4.5)
◦ GINI index (IBM Intelligent Miner)
CMT307 40
How to Specify Test Condition?
• Depends on attribute types
◦ Nominal: takes values from an unordered set. Marital status, Gender, Colour, etc.
◦ Continuous: takes values from ordered set. Age, Salary, etc.
CMT307 41
Splitting Based on Nominal Attributes
• Multi-way split: Use as many partitions as distinct values.
CarType
Family Luxury
Sports
CarType CarType
{Sports,
{Family} OR {Family,
{Sports}
Luxury} Luxury}
CMT307 42
Splitting Based on Continuous Attributes
Taxable Taxable
Income Income?
> 80K?
< 10K > 80K
Yes No
CMT307 43
How to determine the Best Split
• Greedy approach:
◦ Nodes with homogeneous class distribution are preferred
C0: 5 C0: 9
C1: 5 C1: 1
Non-homogeneous, Homogeneous,
High degree of impurity Low degree of impurity
CMT307 44
A criterion for attribute selection
• Which is the best attribute?
◦ The one which will result in the smallest tree
◦ Heuristic: choose the attribute that produces the “purest” nodes
CMT307 45
Computing information
• Given a probability distribution, the information required to predict an event is
the distribution’s entropy (Shannon entropy)
CMT307 47
Splitting Criteria based on Information Gain
• Entropy at a given node t:
Entropy (t ) = - å p( j | t ) log2 p( j | t )
j
CMT307 48
Examples for computing Entropy
Entropy(t ) = -å p( j | t ) log p( j | t )
j 2
CMT307 49
Splitting Based on Information Gain
• Information Gain:
0
𝑛$
𝐺𝐴𝐼𝑁,-.$/ = 𝐸𝑛𝑡𝑟𝑜𝑝𝑦(𝑝) − E 𝐸𝑛𝑡𝑟𝑜𝑝𝑦(𝑖)
𝑛
$&!
Parent Node, p is split into k partitions;
ni is number of records in partition i
CMT307 50
Splitting based on Gini Index
• If a data set t contains examples from c classes, gini index, gini(t) is defined as
2
𝑔𝑖𝑛𝑖 𝑡 = 1 − E 𝑝1" Example:
1&! a node with 3 classes, each having samples [0, 49, 5]
gini(T) = 1 – (0/54)2 – (49/54)2 – (5/54)2 ≈ 0.168.
where pj is the relative frequency of class j in t.
• If a data set t of n points is split into two subsets t1 and t2 with sizes n1 and n2 respectively, the gini
index of the split data contains examples from c classes, the gini index gini(t) is defined as
𝑛! 𝑛"
𝑔𝑖𝑛𝑖,-.$/ 𝑡 = 𝑔𝑖𝑛𝑖 𝑡! + 𝑔𝑖𝑛𝑖 𝑡"
𝑛 𝑛
• The attribute provides the smallest ginisplit(t) is chosen to split the node (need to enumerate all
possible splitting points for each attribute).
51
Weather Data: Play or not Play?
• Training Examples
Day Outlook Temp. Humidity Wind Play Tennis
D1 Sunny Hot High Weak No
D2 Sunny Hot High Strong No
D3 Overcast Hot High Weak Yes
D4 Rain Mild High Weak Yes
D5 Rain Cool Normal Weak Yes
D6 Rain Cool Normal Strong No
D7 Overcast Cool Normal Weak Yes
D8 Sunny Mild High Weak No
D9 Sunny Cool Normal Weak Yes
D10 Rain Mild Normal Strong Yes
D11 Sunny Mild Normal Strong Yes
D12 Overcast Mild High Strong Yes
D13 Overcast Hot Normal Weak Yes
D14 Rain Mild High Strong No
CMT307 52
Decision Tree for PlayTennis
• Attributes and their values:
◦ Outlook: Sunny, Overcast, Rain
◦ Humidity: High, Normal
◦ Wind: Strong, Weak
◦ Temperature: Hot, Mild, Cool
CMT307 53
Decision Tree for PlayTennis from ID3 Algorithm
Outlook
No Yes No Yes
CMT307 54
Converting a Tree to Rules
Outlook
No Yes No Yes
CMT307 55
Regression Trees
($) "
MSE3456 = ∑$∈3456 𝑦13456 −𝑦
L !
𝑦13456 = ) ∑$∈3456 𝑦 ($)
#$%&
CMT307 56
Remarks
• Pros
◦ Interpretable decision process: white box model
◦ Decision trees requires little data preparation: no need of
feature scaling or centering at all
• Cons
◦ Splits are perpendicular to an axis, so sensitive to training
set rotation
§ Solution: use PCA to get a better orientation of the training
data
◦ Very sensitive to small variations in the training data,
leading to different trees.
§ Solution: use Random Forest to limit instability
CMT307 57
Reading
• Aurélien Géron. Hands-On Machine Learning with Scikit-Learn, Keras, and
TensorFlow, 2nd Edition, 2019.
Chapter 6.
CMT307 58
Contents
• k-nearest neighbours
• Support Vector Machines
• Decision trees
• Hyperparameter Optimisation
CMT307 59
Hyperparameter Optimisation
• Model parameters vs. hyperparameters
SVM Example:
𝑓 𝒙$ = 𝑠𝑖𝑔𝑛(𝒘% 𝒙$ + 𝑏)
)
1 %
min 𝑤 𝑤 + 𝐶 E 𝜉$
2
$&!
• Machine learning models have hyperparameters that you must set in order to
customize the model to a given dataset,
◦ hyperparameter may take on integer-valued, real-valued, or categorical, e.g.,
§ k of k-NN: integer
§ C and r of SVM with RBF kernel: real
§ Kernel of SVM: categorical {‘linear’, ‘poly’, ‘rbf’, …}
CMT307 60
Hyperparameter Optimisation
• Machine learning programs usually provide default values for hyperparameters,
e.g.,
◦ k=5 in KNeighborsClassifier of sklearn
◦ C=1 and γ= 1 / (n_features * [Link]()) in SVC of sklearn
CMT307 61
Methods of hyperparameter optimization
• Grid search
• Random search
• Bayesian optimization
◦ Optuna
◦ Hyperopt See [Link]
◦ Scikit Optimize optimization-techniques-machine-learning/
CMT307 62
Grid search
• The most basic hyperparameter optimisation
method
CMT307 63
Random search
• Define a search space as a bounded domain of
hyperparameter values and randomly sample
points in that domain
• Works better than grid search when some
hyperparameters are much more important
than others
• More efficient in terms of exploration. For B
!
evaluations, grid search only explores 𝐵 "
CMT307 64
Summary
• k-NN is a simple but can produce good results for many complex problems
• SVM is an important and popular machine learning method with many
successful applications
• DT is a white box method with good interpretability
• All k-NN, SVM and DT can be used for both classification and regression
CMT307 65