SVM Kernel

SVM Kernel
Author

Nipun Batra

Published

April 20, 2023

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
X = np.array([-2, -1, 0, 1, 2, 3, 4]).reshape(-1, 1)
y = np.array([-1, -1, 1, 1, 1, -1, -1])
plt.scatter(X.flatten(), np.zeros_like(X), c=y, cmap=cm.Paired)
<matplotlib.collections.PathCollection at 0x7f8e0b18f490>

def phi(x):
    return np.hstack([x, x**2])
phi(X)
array([[-2,  4],
       [-1,  1],
       [ 0,  0],
       [ 1,  1],
       [ 2,  4],
       [ 3,  9],
       [ 4, 16]])
plt.scatter(phi(X)[:, 0], phi(X)[:, 1], c = y,cmap = cm.Paired)
<matplotlib.collections.PathCollection at 0x7f8beb7f5fa0>

from sklearn.svm import SVC
c = SVC(C=1, kernel='linear')
c.fit(phi(X), y)
SVC(C=1, kernel='linear')
plt.scatter(phi(X)[:, 0], phi(X)[:, 1], c = y, zorder=10, cmap =cm.Paired, edgecolors='k', alpha=1, s=200)
x_min = phi(X)[:, 0].min()-1
x_max = phi(X)[:, 0].max()+1
y_min = phi(X)[:, 1].min()-1
y_max = phi(X)[:, 1].max()+1

XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
Z = c.decision_function(np.c_[XX.ravel(), YY.ravel()])

# Put the result into a color plot
Z = Z.reshape(XX.shape)
plt.pcolormesh(XX, YY, Z > 0, cmap=plt.cm.Paired, alpha=0.6)
plt.contour(XX, YY, Z, colors=['k', 'k', 'k'],
            linestyles=['--', '-', '--'], levels=[-1, 0, 1])
plt.xlabel(r"$x_1 = X$")
plt.ylabel(r"$x_2 = X^2$")
plt.title(r"Decision surface: {:0.1f}*x_1 + {:0.1f}*x_2 + {:0.1f} = 0".format(c.coef_[0, 0], c.coef_[0, 1], c.intercept_[0]))
Text(0.5, 1.0, 'Decision surface: 1.3*x_1 + -0.7*x_2 + 1.0 = 0')

# Fit a SVM with polynomial kernel

c = SVC(C=1, kernel='poly', degree=2)

c.fit(X, y)
SVC(C=1, degree=2, kernel='poly')