Support Vector Machines
Last updated
Last updated
SVMs are also called kernel methods.
Given linearly separable data (with real-valued attributes), we must find a linear discriminant function that separates the positives and negatives (i.e. the classes) and is as far as possible from any example in the data.
Therefore, SVMs aim to maximize the distance of the decision boundary to the closest point in the data set. This distance is called the margin of the hyperplane, and is denoted by .
The line that achieves maximum margin is called the maximum margin hyperplane.
Consider the data below (positive class: +1, negative class: -1)
The hyperplane equation is g(x) = 0.
For our data, we have the following linear constraints:
The solution gives the weights of the maximum margin hyperplane.
This is, however, the theoretical computation. SVM softwares compute slightly differently.
Let I be the indices of the support vectors. The SVM software outputs g(x) as a function of the support vectors:
So, we have two representtions of the maximum margin hyperplane:
The above works when the data is linearly-separable. What if the data is not linearly separable?
Then, the above technique can be used, since the data (in terms of z) is linearly separable.
However, this cannot always be done.
The idea is to map the data to a new space where it becomes linearly separable. This new space is an infinite-dimensional space.
Then, we have:
Some commonly-used kernel functions:
Polynomial Kernel (of degree d):
The hope is that the data becomes linearly separable by a d-degree polynomial in the attributes.
d is a tunable parameter.
Gaussian Kernel (Radial Basis Function RBF Kernel):
Using a Gaussian kernel is analogous to computing a distance-weighted sum (similar to KNN). It is the most suitable kernel when the data forms a checker-board pattern. To use a Polynomial kernel for the same data, we would need to use high degree d.
The Gaussian kernel is the most commonly-used kernel.
Note: If the number of support vectors returned by the SVM software is not much smaller than the size of the training set, it is a clear sign of overfitting.
The previously discussed hyperplane refers to a hard-margin hyperplane. A soft-margin hyperplane allows examples to be in the margin region, as well as on the wrong side of the hyperplane. To accomodate for the examples that are on the wrong side of the hyperplane, we penalize by the distance of those examples.
For a soft-margin hyperplane, we do the following:
Let
The distance from point x to the hyperplane is = (note that there is no in the denominator).
We need to determine such that if and if .
, and so on.
For the maximum margin hyperplane, we have
Suppose we scale the coefficients of g, such that the new g(x)=1 (let's call it ) for the points closest to the hyperplane. To do so, we must divide the coefficients of g by a certain quantity. Which quantity to use? Maybe the margin ?
Now, replace g(x) by , we have new g(x) = for the points closest to the hyperplane. These points are called support vectors. Typically, we have at least 3 support vectors, some of which are on the + side and some are on the - side. The intuiton behind calling them support vectors is that they support the hyperplane in place, and the hyperplane will move if any of them move.
So, for the support vectors, and . For other examples:
Maximum margin hyperplane on support vectors x satisfies the cannonical form of the maximum margin hyperplane i.e. g(x) = . So, .
Therefore, we must maximize subject to the constraints:
(to simplify, we minimize ).
SVM softwares often don't directly output the weights. Instead, they output a dual representation of the hyperplane g(x)=0 where g(x)=:
where i.e. the example to be classified.
Suppose the support vectors are say respectively, it will output coefficients for each support vector i.e. respectively, and (say).
The software will, therefore, output g(x). The coefficients of in g(x) give the weights and the constant term will be .
The Primal Representation
The Dual Representation
( for all t where is not a support vector).
Say the data follows . We can make the data linearly separable using .
Let where is a mapping function.
Suppose we have an easy way to compute , for x,y in the original space, K is called a kernel function. Then, we have:
------------------ (1)
The kernel trick is to avoid computing in the new space, by using a kernel function.
Linear Kernel:
For d=2,
Consider
Let (this makes it easier to compute), then, =
In general, if the degree is d, the kernel function is
Another version without lower order terms is
where is the L2 norm i.e. basically the distance between x and y.
is a tunable parameter.
It can also be computed as where . Small means a narrow Gaussian, while small means a flat Gaussian.
In general, we predict + if and predict - otherwise. (from (1)).
For a hard-margin hyperplane, we minimize subject to:
, and Equivalently,
, subject to:
;
is the penalty paramater. Note that a large penalty may cause overfitting.