This lesson is being piloted (Beta version)

Support Vector Machine

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How to use Support Vector Machine in Machine Learning model

Objectives
  • Learn how to use SVM in ML model

12. Support Vector Machine

12.1. Applications of Support Vector Machine:

image

12.2. Explanation

image

image

image

image

12.3. SVM’s kernel

12.3.1. For linear separable data, it is quite straight forward to create a hyperplane to distinguish them

image

12.3.2. For linearly non-separable data, SVM makes use of kernel tricks to make it linearly separable.

image

The following kernel trick compared different kernel ‘linear’ , ’poly’ , ‘rbf’ , ‘sigmoid’:

image

12.4. Implementation

Here we use the regular iris dataset with Classification problem

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

import numpy as np
import pandas as pd
iris = load_iris()
X = iris.data
X = X[:,2:4]
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.6,random_state=123)

Fit Support Vector Classifier model

from sklearn.svm import SVC
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt
names = ["Linear SVM", "RBF SVM", "Poly SVM", "Sigmoid SVM"]
classifiers = [
    SVC(kernel="linear"),
    SVC(kernel="rbf"),
    SVC(kernel="poly"),
    SVC(kernel="sigmoid")]

i = 1
figure = plt.figure(figsize=(27, 5))
cm = plt.cm.jet

for name, clf in zip(names, classifiers):
    ax = plt.subplot(1,4, i)
    clf.fit(X_train, y_train)
    ax = plot_decision_regions(X=X_train, 
                      y=y_train,
                      clf=clf)
    ax.set_xlabel(iris.feature_names[2], size=14)
    ax.set_ylabel(iris.feature_names[3], size=14)
    ax.set_title(name, size=20)
    handles, labels = ax.get_legend_handles_labels()
    ax.legend(handles,iris.target_names)
    i+=1
plt.show()

image

In this model, C is the regularization parameter Default C=1. The strength of the regularization is inversely proportional to C. Must be strictly positive.

12.5. Tips on using SVM

12.6. Pros of SVM

12.7. Cons of SVM

Key Points

  • SVM