Understanding Support Vector Machines
Understanding Support Vector Machines
f(x) = σ (wTx + b)
Linear Separators
Geometric interpretation
For given data we can draw different hyperplanes
𝝅𝟏 That separates two classes
𝝅𝟐
Q. Which one to select?
Support Vector Machine
Geometric interpretation
For given data we can draw different hyperplanes
𝝅𝟏 That separates two classes
𝝅𝟏
Q. Which one to select?
𝝅𝟏
Slight change will result in misclassification of points
Support Vector Machine
Geometric interpretation
For given data we can draw different hyperplanes
That separates two classes
𝝅𝟏
Q. Which one to select?
𝝅𝟏
Slight change will result in misclassification of points
Misclassified Points
Support Vector Machine
Geometric interpretation
For given data we can draw different hyperplanes
𝝅𝟐 That separates two classes
𝝅𝟐
Q. Which one to select?
𝝅𝟐
Slight change will not affect much
Support Vector Machine
Geometric interpretation
For given data we can draw different hyperplanes
𝝅𝟐 That separates two classes
𝝅𝟐
Slight change will not affect much
NO misclassified Points
Support Vector Machine
Geometric interpretation
For given data we can draw different hyperplanes
That separates two classes
Geometric interpretation
For given data we can draw different hyperplanes
𝝅𝟏 That separates two classes
𝝅𝟐
Q. Which one to select?
𝝅+ 𝝅
Margin Maximizing hyperplane
Take any plane 𝝅
𝒅
Support Vector Machine
𝝅 𝝅−
𝝅+
Margin Maximizing hyperplane
𝒅
Support Vector Machine
𝝅 𝝅−
𝝅+
If hyperplane 𝝅 is closer to the data points then
Case 1 marginal distance is small
𝒅
Support Vector Machine
Case 2 𝝅+
If hyperplane 𝝅 is farther to the data points then
𝝅 marginal distance is large
𝝅−
𝒅
Support Vector Machine
Case 2 𝝅+
𝝅 𝝅− 𝝅
𝝅+
Case 1 𝝅−
𝒅
Support Vector Machine
Case 2 𝝅+
Problem: Find a hyperplane 𝝅 such that marginal
𝝅 distance is maximum
𝝅− 𝝅 is margin maximizing separating plane
𝒅
Support Vector Machine
𝝅+
𝝅 𝝅+: 𝑾𝑻𝒙 + 𝒃 = +𝒌
𝝅− 𝝅 ∶ 𝑾𝑻𝒙 + 𝒃 = 𝟎
𝝅−: 𝑾𝑻𝒙 + 𝒃 = −𝒌
𝑾 𝑻𝑷 + 𝒃 + 𝒌 𝑾 𝑻𝑷 + 𝒃 − 𝒌
𝑨𝑩 = −
𝑩 𝑾 𝑾
𝒅
𝒌 − (−𝒌) 𝟐𝒌
𝑨𝑩 = =
𝑾 𝑾
Support Vector Machine
𝝅+ Distance between two parallel lines?
𝝅 𝝅+: 𝑾𝑻𝒙 + 𝒃 = +𝟏
𝑷 𝝅 ∶ 𝑾𝑻𝒙 + 𝒃 = 𝟎
𝝅−
𝝅−: 𝑾𝑻𝒙 + 𝒃 = −𝟏
𝑨
𝑨𝑩 = 𝑷𝑩 − 𝑷𝑨
𝟏 − (−𝟏) 𝟐
𝑨𝑩 = =
𝑾 𝑾
𝑩
𝒅
For simplicity lets say k=1
Support Vector Machine
𝝅+ Distance between two parallel lines?
𝝅 𝟐𝒌
𝑷 𝑨𝑩 = 𝒅 =
𝑾
𝝅−
𝐎𝐩𝐭𝐢𝐦𝐢𝐳𝐚𝐭𝐢𝐨𝐧 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: Find w, b such that
𝑨 Marginal distance will be maximum
2𝑘 2
𝑊 ∗ , 𝑏∗ = max = 𝑘 × max
𝑤, 𝑾 𝑤, 𝑾
𝑩 𝑏 𝑏
𝒅
Support Vectors
33
Classification Margin
• Datasets that are linearly separable with some noise work out great:
0 x
0 x
0 x
Non-linear Separator: Feature spaces
• General idea: the original feature space can always be mapped to
some higher-dimensional feature space where the training set is
separable:
Φ: x → φ(x)
SVM Main Idea
Non-Linear SVM
• When the data is not linearly separable.
• Which means when the data points cannot be separated
into 2 classes by using a straight line (if 2D).
• We use kernel tricks to classify them.
Few Definitions
Support Vectors
• These are the points that are closest to the hyperplane.
• A separating line will be defined with the help of these data
points.
Margin
• it is the distance between the hyperplane and the observations
closest to the hyperplane (support vectors).
• In SVM large margin is considered a good margin.
• There are two types of margins hard margin and soft
margin.
SVM Working?
There can be infinite points on the boundary to measure the distance from X,
therefore the perpendicular vector w to the hyperplane is considered to
measure the distance.
Margin in Support Vector Machine
• Since x2 and x1 are support vectors and they lie on the hyperplane,
hence they will follow yi* (w.x+b)=1 so we can write it as:
Optimization function and its constraints
• Putting equations (2) and (3) in equation (1) we get:
Signed distance
4 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓
3
2
2 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓
1 3 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = 𝟎. 𝟓
𝟐
4 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓
Support Vector Machine
𝝅+ Lets allow our model to make few mistakes
𝝅 Find plane/hyperplane such that all positive points
Are above 𝝅+and all negative points are below𝝅−
𝝅−
Here 4 points (1, 2, 3, 4) are not in appropriate position
Signed distance
4 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓 = 𝟏 − 𝟏. 𝟓
3
2
2 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓 = 𝟏 − 𝟐. 𝟓
1 3 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = 𝟎. 𝟓 = 𝟏 − 𝟎. 𝟓
𝟐
4 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓 = 𝟏 − 𝟐. 𝟓
Support Vector Machine
𝝅+ Lets allow our model to make few mistakes
𝝅 Find plane/hyperplane such that all positive points
Are above 𝝅+and all negative points are below𝝅−
𝝅−
Here 4 points (1, 2, 3, 4) are not in appropriate position
Signed distance
4 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓 = 𝟏 − 𝟏. 𝟓 = 𝟏 − 𝜻𝒊
3
2
2 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓 = 𝟏 − 𝟐. 𝟓 = 𝟏 − 𝜻𝒊
1 3 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = 𝟎. 𝟓 = 𝟏 − 𝟎. 𝟓 = 𝟏 − 𝜻𝒊
𝟐
4 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓 = 𝟏 − 𝟐. 𝟓 = 𝟏 − 𝜻𝒊
Support Vector Machine
𝝅+ Signed distance
𝝅 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓 = 𝟏 − 𝟏. 𝟓 = 𝟏 − 𝜻𝒊
𝝅− 2 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓 = 𝟏 − 𝟐. 𝟓 = 𝟏 − 𝜻𝒊
3 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = 𝟎. 𝟓 = 𝟏 − 𝟎. 𝟓 = 𝟏 − 𝜻𝒊
4 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟏. 𝟓 = 𝟏 − 𝟐. 𝟓 = 𝟏 − 𝜻𝒊
4
3 Lets focus one point at a time
2
𝟐 1
Support Vector Machine
𝝅+ Signed distance
𝝅 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓 = 𝟏 − 𝟏. 𝟓 = 𝟏 − 𝜻𝒊
𝝅−
Positive point 𝑦𝑖 = +1
𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑑𝑖 = − 0.5 as it is in opposite direction of w
1
Support Vector Machine
𝝅+ Signed distance
𝝅 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓 = 𝟏 − 𝟏. 𝟓 = 𝟏 − 𝜻𝒊
𝝅−
Positive point 𝑦𝑖 = +1
𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑑𝑖 = − 0.5 as it is in opposite direction of w
1
Support Vector Machine
𝝅+ Signed distance
𝝅 1 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = −𝟎. 𝟓 = 𝟏 − 𝟏. 𝟓 = 𝟏 − 𝜻𝒊
𝝅−
Positive point 𝑦𝑖 = +1
𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑑𝑖 = − 0.5 as it is in opposite direction of w
𝜻𝒊
How far point is from correct plane in incorrect direction
𝜻𝒊
How far point is from correct plane in incorrect direction
2
2 Is 2.5 distance away from correct plane 𝝅+in opposite direction
Support Vector Machine
𝝅+ Signed distance
𝝅 3 𝒚𝒊 𝑾𝑻𝒙𝒊 + 𝒃 = +𝟎. 𝟓 = 𝟏 − 𝟎. 𝟓 = 𝟏 − 𝜻𝒊
𝝅−
Positive point 𝑦𝑖 = +1
𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑑𝑖 = +0.5 as it is in opposite direction of w
𝜻𝒊
3 How far point is from correct plane in incorrect direction
𝜻𝒊
4
How far point is from correct plane in incorrect direction
𝜻𝒊 = 𝟎 𝝅+
𝜻𝒊 = 𝟎 If points are in appropriate region then 𝜻𝒊 = 𝟎
𝝅
If point is correctly classified then 𝜻𝒊 = 𝟎
𝝅−
𝜻𝒊 = 𝟎
𝜻𝒊 = 𝟎
𝜻𝒊 = 𝟎
𝜻𝒊 = 𝟎
Support Vector Machine
𝝅+
If points are Not in appropriate region then 𝜻𝒊 ≠ 𝟎
𝝅
If point is misclassified then 𝜻𝒊 ≠ 𝟎
𝝅−
𝜻𝒊 = 𝟏. 𝟓
Support Vector Machine
𝝅+ 𝜁𝑖 = 0 𝑖𝑓 𝑦𝑖 (𝑊𝑇 𝑥𝑖 + 𝑏) ≥ 1
𝝅
Point is correctly classified
𝝅−
𝜻𝒊 = 𝟎
𝜁𝑖 > 0 𝑖𝑓 𝑦𝑖 𝑊 𝑇 𝑥 𝑖 + 𝑏 < 1
𝜻𝒊 = 𝟐. 𝟓 Point is misclassified
𝜻𝒊 = 𝟎
Support Vector Machine
𝝅+ Original Formulation: Convert from maximization
𝝅 to minimization
2
𝝅− 𝑤 ∗ , 𝑏∗ = max
𝜻𝒊 = 𝟎 𝑤, 𝑊
𝑏
𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡, 𝑦𝑖 𝑊 𝑇 𝑥 𝑖 + 𝑏 ≥ 1
𝜻𝒊 = 𝟐. 𝟓
𝑊
𝑤 ∗ , 𝑏∗ = min
𝑤, 2
𝑏
𝜻𝒊 = 𝟎 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡, 𝑦𝑖 𝑊 𝑇 𝑥 𝑖 + 𝑏 ≥ 1
Support Vector Machine
𝝅+ New optimization Problem
𝝅
𝑛
𝑊 1
𝝅− 𝑤 ∗ , 𝑏∗ = min + 𝐶 ∑ 𝜁𝑖
𝜻𝒊 = 𝟎 𝑤, 2 𝑛
𝑏 𝑖
=1
𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡,
𝑦𝑖 𝑊 𝑇 𝑥 𝑖 + 𝑏 ≥ 1 − 𝜁𝑖 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑖′𝑠
𝜻𝒊 = 𝟐. 𝟓
𝜁𝑖 ≥ 0
𝜻𝒊 = 𝟎
Support Vector Machine
𝝅+ New optimization Problem Marginal distance
𝝅
𝑛
𝑊 1
𝝅− 𝑤 ∗ , 𝑏∗ = min + 𝐶 ∑ 𝜁𝑖
𝜻𝒊 = 𝟎 𝑤, 2 𝑛
𝑏 𝑖
=1
𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡,
𝑦𝑖 𝑊 𝑇 𝑥 𝑖 + 𝑏 ≥ 1 − 𝜁𝑖 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑖′𝑠
𝜻𝒊 = 𝟐. 𝟓
𝜁𝑖 ≥ 0
𝜻𝒊 = 𝟎
Support Vector Machine
𝝅+ New optimization Problem Allowed Error
𝝅
𝑛
𝑊 1
𝝅− 𝑤 ∗ , 𝑏∗ = min + 𝐶 ∑ 𝜁𝑖
𝜻𝒊 = 𝟎 𝑤, 2 𝑛
𝑏 𝑖
=1
𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡,
𝑦𝑖 𝑊 𝑇 𝑥 𝑖 + 𝑏 ≥ 1 − 𝜁𝑖 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑖′𝑠
𝜻𝒊 = 𝟐. 𝟓
𝜁𝑖 ≥ 0
𝜻𝒊 = 𝟎 Here C is a hyperparameter
Soft Margin SVM
• In real-life applications we don’t find any dataset which is linearly
separable, what we’ll find is either an almost linearly separable
dataset or a non-linearly separable dataset.
• Lets modify the above equation in such a way that it allows few
misclassifications that means it allows few points to be wrongly
classified.
• For all the correctly classified points, zeta will be equal to 0 and for
all the incorrectly classified points the zeta is the distance of that
particular point from its correct hyperplane.
• which is a better model, the one where the margin is maximum and
has 2 misclassified points or the one where the margin is very less,
and all the points are correctly classified?
Soft Margin SVM
• Datasets that are linearly separable with some noise works out great:
0 x
0 x
0 x
Non-linear Separator: Feature spaces
• General idea: the original feature space can always be mapped to
some higher-dimensional feature space where the training set is
separable:
Φ: x → φ(x)
How to make a plane curved
• Fitting hyperplanes as a
separators is mathematically easy.
It requires a linear operation.
• These scalar products are the only part of the computation that
depends on the dimensionality of the high-dimensional space.
• The kernel trick is just a magic way of doing scalar products faster
than is usually possible.
– It relies on choosing a way of mapping to the high-dimensional
feature space that allows fast scalar products.
The kernel trick
• For many mappings from a
low-D space to a high-D Low-D
space, there is a simple b
x
operation on two vectors in xa
the low-D space that can be
used to compute the scalar
product of their two images
in the high-D space.
High-D
K ( x a , x b ) ( x a ) . ( x b )
( xa )
Letting the doing the scalar ( xb )
kernel do product in the
the work obvious way
The kernel trick
• Suppose we have a dataset as below:
• we cannot draw a single line or say hyperplane which can classify the points
correctly.
• So, converting this lower dimension space to a higher dimension space
using some quadratic functions, allow us to find a decision boundary that
clearly divides the data points.
• These functions which help us do this are called Kernels and which kernel
to use is purely determined by hyper-parameter tuning.
The kernel trick
Different Kernel functions
1. Polynomial kernel
Following is the formula for the polynomial kernel:
It simply takes input data points and map them to a value of 0 and 1,
so that they can be separated by a simple straight line.
Different Kernel functions
3. Radial Basis Function (RBF) kernel
• It creates the non-linear combinations of features to lift samples
points into a higher-dimensional feature space.
• Now, a linear decision boundary can be used to separate these
points.
• It is the most used kernel in SVM classifications, the following
formula explains it mathematically:
where,
1. ‘σ’ is the variance, a hyperparameter in this case
2. ||X₁ – X₂|| is the Euclidean Distance between two points X₁
and X₂
Different Kernel functions
3. RBF kernel
• Example
Different Kernel functions
4. Bessel function kernel
5. Anova Kernel
• SVM’s are very good if you have no idea about what structure to
impose on the task.
Disadvantages of SVM
1. Choosing a good kernel is not easy.
2. It doesn’t show good results on a big dataset.
3. Hyper-parameter tuning is difficult.