Search⌘ K

Visualize the Working of the Support Vector Machine

Explore how to visualize the support vector machine algorithm by creating interactive plots that demonstrate the effects of the C parameter and various kernel functions. Learn to handle linear and non-linear separable data using the kernel trick and understand the role of hyperparameters in the bias-variance trade-off for effective SVM modeling.

Now that we have enough understanding of the support vector machine algorithm, let's move and create interactive plots to see the effect of the CC parameter.

Imports

We need to import the required libraries.

Python 3.8
# required imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from ipywidgets import interact

Sample data

Let's create a dataset for binary class classification.

Python 3.8
# from sklearn.datasets.samples_generator import make_blobs # older versions
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=50, centers=2,random_state=121,cluster_std=0.90)
# try different values of cluster_std e.g. 0.60, 0.70, 0.80 ...etc and
# observe the margins in the interactive plots
plt.figure(figsize=(16,8))
ax = plt.gca()
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel('X1')
ax.set_ylabel('X2')
# plotting data as a scatter plot
plt.scatter(X[:, 0], X[:, 1], c=y, s=100, cmap=plt.cm.Set1, edgecolors='black', linewidth=2);

Now that we have the data, let’s move on and write some functions to create the interactive plots and understand the effect of the CC ...