Solving nonlinear problems using a kernel SVM
Another reason why SVMs enjoy high popularity among machine learning practitioners is that it can be easily kernelized to solve nonlinear classification problems. Before we discuss the main concept behind a kernel SVM, let's first create a sample dataset to see what such a nonlinear classification problem may look like.
Kernel methods for linearly inseparable data
Using the following code, we will create a simple dataset that has the form of an XOR gate using the logical_or
function from NumPy, where 100 samples will be assigned the class label 1
, and 100 samples will be assigned the class label -1
:
>>> import matplotlib.pyplot as plt >>> import numpy as np >>> np.random.seed(1) >>> X_xor = np.random.randn(200, 2) >>> y_xor = np.logical_xor(X_xor[:, 0] > 0, ... X_xor[:, 1] > 0) >>> y_xor = np.where(y_xor, 1, -1) >>> plt.scatter(X_xor[y_xor == 1, 0], ... ...