There are various classification algorithms existing in the field of computational statistics and more robust, compute-time friendly algorithms are being invented as the study of machine learning progresses. However, for an algorithm invented in the 90’s, SVMs still hold a respectable place in real world applications and even academics. SVMs have performed surprisingly well when feature vectors are too large to cripple other algorithms. How do they handle very large feature vector spaces ? And why are they still popular?
With supervised learning algorithms, at some point the performance is pretty similar for all. What matters more is: how clean your data is, what features you have selected and what cost function you have picked. The nearest algorithm that can be compared to SVM is logistic regression and we will continue our mathematical derivation from logistic regression itself, slowly transgressing the Sigmoidal hypothesis function into a large margin classifier.
Alternative view of Logistic Regression:
The hypothesis function of logistic regression is given by:
![](https://static.wixstatic.com/media/7103e6_29692c77440b4eaca94a675b9c91b0ae~mv2.png/v1/fill/w_512,h_226,al_c,q_85,enc_auto/7103e6_29692c77440b4eaca94a675b9c91b0ae~mv2.png)
Where z = inner product of feature vector Θ and input vector x.
If result y = 1, we want h to be close to 1, or z to be greater than 0.
Similarly if y = 0, we want h to be close to 0, or z to be less than 0.
Fig 1: Sigmoidal Function
![](https://static.wixstatic.com/media/7103e6_a897c632a85d48fda6724c8fbcd85d9e~mv2.png/v1/fill/w_980,h_653,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/7103e6_a897c632a85d48fda6724c8fbcd85d9e~mv2.png)
Cost function of Logistic regression:
![](https://static.wixstatic.com/media/7103e6_a1bb02b389a744f4a1fcb8957139c11d~mv2.png/v1/fill/w_512,h_40,al_c,q_85,enc_auto/7103e6_a1bb02b389a744f4a1fcb8957139c11d~mv2.png)
where y is actual output and h is hypothesis function result.
When y = 1, the cost function becomes: J = -log(h)
Fig 2: J vs z graph when y = 1
![](https://static.wixstatic.com/media/7103e6_ea1f8e6f3d5c421185220f5507d0378f~mv2.jpg/v1/fill/w_252,h_370,al_c,q_80,enc_auto/7103e6_ea1f8e6f3d5c421185220f5507d0378f~mv2.jpg)
This function shows cost function J against z when y = 1. To build an SVM out of it, we have to redefine the cost function to follow this trend but in a simpler manner, so that J = 0 for z >= 1, and J is non zero for z < 1. This little modification makes the cost function computationally faster. The same trend is followed for y = 0, such as J = 0 for z <= -1 and J is non-zero otherwise.
Now that the cost function is modified, let’s take a look at the hypothesis function. Ideally it should be a step function but due to the modified cost function, the hypothesis function becomes as such:
h = 1 for z >= 1
h = 0 for z <= -1
A margin remains between 1 and -1 and this is why the SVM is called a Large Margin Classifier. This margin can be modified according to our wish and serves a purpose of actually ignoring the outliers present inside the margin.
The final cost function takes a form as follows:
![](https://static.wixstatic.com/media/7103e6_3b47bdfee1314df78b88f3279ac392d1~mv2.png/v1/fill/w_512,h_56,al_c,q_85,enc_auto/7103e6_3b47bdfee1314df78b88f3279ac392d1~mv2.png)
The modified cost functions are Cost 1 and Cost 0 respectively and a constant C is added to amplify the cost function. The constant C is set to a moderate value and it helps the classifier to be more robust. If C is set too high, the decision boundary will try to include all points and leads to overfitting.
Use of Kernels:
Let’s see how SVM uses a Gaussian kernel to make the decision boundaries non-linear.
The input vector is modified with a Gaussian function to sort of change the input vector into something that the SVM can easily classify, so in that sense, the SVM itself does not form non linear decision boundaries but modifies the non-linear nature of the input into something that can be easily classified. Shown below is a Gaussian kernel in 2d space:
Fig 3: Gaussian Kernel
![](https://static.wixstatic.com/media/7103e6_a7bacf7b99474c6f97f5ab0de88663dd~mv2.png/v1/fill/w_236,h_170,al_c,q_85,enc_auto/7103e6_a7bacf7b99474c6f97f5ab0de88663dd~mv2.png)
Till now we have seen some basic parameters and how they affect the functionality of SVMs. Now let’s get into the code.
SVM LINEAR KERNEL
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
# import data
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
# we create an instance of SVM and fit out data.
C = 1.0 # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=1,gamma=0).fit(X, y)
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
plt.subplot(1, 1, 1)
Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.title('SVC with linear kernel')
plt.show()
![](https://static.wixstatic.com/media/7103e6_55a535509efd4a4c9cb61aebbe05adf4~mv2.png/v1/fill/w_512,h_394,al_c,q_85,enc_auto/7103e6_55a535509efd4a4c9cb61aebbe05adf4~mv2.png)
SVM RBF KERNEL
svc = svm.SVC(kernel='rbf', C=1,gamma=0).fit(X, y)
![](https://static.wixstatic.com/media/7103e6_0181f1440fcd4a09995489cb3f5aff77~mv2.png/v1/fill/w_512,h_409,al_c,q_85,enc_auto/7103e6_0181f1440fcd4a09995489cb3f5aff77~mv2.png)
Gamma: Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’. Higher the value of gamma, will try to exactly fit the as per training data set i.e. generalization error and cause overfitting problem.
![](https://static.wixstatic.com/media/7103e6_b436fe2820914dfdae32ebc721afd4bd~mv2.png/v1/fill/w_512,h_154,al_c,q_85,enc_auto/7103e6_b436fe2820914dfdae32ebc721afd4bd~mv2.png)
C: Penalty parameter C of the error term. It also controls the trade-off between smooth decision boundaries and classifying the training points correctly.
![](https://static.wixstatic.com/media/7103e6_97c1c8273db34aa08d51a12de06670d7~mv2.png/v1/fill/w_512,h_151,al_c,q_85,enc_auto/7103e6_97c1c8273db34aa08d51a12de06670d7~mv2.png)
Let’s go through what we have learnt in this process:
SVM helps us fit very high dimensional data, proving its advantage over Logistic Regression.
Adding non-linear kernels helps us to fit non-linear data.
Increasing the value of constant C leads to the model becoming more complex, helping it to fit more complex data points. However if C is too large, the model will train itself to include outliers too, leading to overfitting. So a moderate value of C is to be chosen.
Practice Problem…
Determine the kernel function for this image and let me know in the comments section.
![](https://static.wixstatic.com/media/7103e6_ef359dcc493b4c1f856588b303d6f7a4~mv2.png/v1/fill/w_422,h_385,al_c,q_85,enc_auto/7103e6_ef359dcc493b4c1f856588b303d6f7a4~mv2.png)
Edited by coolman-spaceman
Comments