  # Building an SVM Linear Kernel in Python

NumPy | Python Methods and Functions

Let`s build a SVM linear core using the sklearn Python library and the Iris dataset found in the Python dataset library.

Linear Kernel is used when the data is linearly separable, that is, it can be separated with a single line. This is one of the most common kernels. It is mainly used when there are a large number of functions in a given dataset. One example where there are many features is text classification , as each alphabet is a new feature. Therefore, we mainly use the linear kernel in text classification.

Note . Internet connection must be stable while running the code below as it is related to data loading.

In the image above, there are two sets of features: Blue and Yellow . Since they can be easily separated, or in other words, they are linearly separable, so a linear kernel can be used here.

Advantages of using a linear kernel:

1. Training SVM with a linear kernel is faster than with any other kernel.

2. When training SVM with a linear kernel, only optimization of the C Regularization parameter is required. On the other hand, when teaching other kernels, it is necessary to optimize the γ, parameter, which means that the search in the grid usually takes longer.

 ` # Importing Libraries ` ` import ` ` numpy as np ` ` import ` ` matplotlib.pyplot as plt ` ` from ` ` sklearn ` ` import ` ` svm, datasets `   ` # Import some data from the iris dataset ` ` iris ` ` = ` ` datasets.load_iris () `   ` # Take only the first two data functions. ` ` # To avoid splitting, you can use Two-Dim Dataset `   ` X ` ` = ` ` iris.data [:,: ` ` 2 ` `] ` ` y ` ` = ` ` iris.target `   ` # C - SVM regularization parameter ` ` C ` ` = ` ` 1.0 `    ` # Create an SVM instance and strip the data. ` ` # The data is not scaled so that support vectors can be built `` svc = svm.SVC (kernel = `linear` , C = 1 ). fit (X , y)   # create grid for construction 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))    # Data plot for proper visual representation plt.s ubplot ( 1 , 1 , 1 )    # Predict the result by providing model data 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` )   # Display the graph plt.show () `

Exit:

Here all functions are separated using simple lines, thus representing a linear kernel.