Creating linear kernel SVM in Python
Prerequisite: SVM
Let’s create a Linear Kernel SVM using the sklearn library of Python and the Iris Dataset that can be found in the dataset library of Python.
Linear Kernel is used when the data is Linearly separable, that is, it can be separated using a single Line. It is one of the most common kernels to be used. It is mostly used when there are a Large number of Features in a particular Data Set. One of the examples where there are a lot of features, is Text Classification, as each alphabet is a new feature. So we mostly use Linear Kernel in Text Classification.
Note: Internet Connection must be stable while running the below code because it involves downloading data.
In the above image, there are two set of features “Blue” features and the “Yellow” Features. Since these can be easily separated or in other words, they are linearly separable, so the Linear Kernel can be used here.
Advantages of using Linear Kernel:
1. Training a SVM with a Linear Kernel is Faster than with any other Kernel.
2. When training a SVM with a Linear Kernel, only the optimisation of the C Regularisation parameter is required. On the other hand, when training with other kernels, there is a need to optimise the γ parameter which means that performing a grid search will usually take more time.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
iris = datasets.load_iris()
X = iris.data[:, : 2 ]
y = iris.target
C = 1.0
svc = svm.SVC(kernel = 'linear' , C = 1 ).fit(X, y)
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()
|
Output:
Here all the features are separated using simple lines, thus representing the Linear Kernel.
Last Updated :
20 Jun, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...