Lesson 18 - Support Vector Machines

Additional Resources

  • Hands-On Machine Learning, Ch 5
In [1]:
import numpy as np
import matplotlib.pyplot as plt
 
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.datasets import make_blobs, make_gaussian_quantiles, make_moons, make_circles, make_classification

from ClassificationPlotter import plot_regions
from ipywidgets import *

Example 1

In [2]:
np.random.seed(1)
X1, y1 = make_blobs(n_samples=100, centers=2, cluster_std=2)
X1[0,:] = [-4, -5]
X1[1,:] = [-6, 7]

plt.figure(figsize = [8,6])
plt.scatter(X1[:, 0], X1[:, 1],  c=y1, s=80, edgecolor='k', cmap='rainbow')
plt.show()
In [3]:
%run -i Snippets/snippet10.py

Scikit-Learn Syntax

In [4]:
mod_01 = SVC(kernel='linear', C=0.01)
mod_01.fit(X1, y1)
print('Training Accuracy:', mod_01.score(X1, y1))
Training Accuracy: 0.98

Example 2

In [5]:
np.random.seed(3158)
X2_temp, y2 = make_circles(n_samples=100, noise=0.2, factor=0.05)
X2 = np.array([2 * X2_temp[:,0] + 6 * X2_temp[:,1], 
               2 * X2_temp[:,0] - 3 * X2_temp[:,1]]).T

plt.figure(figsize = [8,6])
plt.scatter(X2[:,0], X2[:,1], c=y2, s=50, edgecolor='k', cmap='rainbow')
plt.show()
In [6]:
%run -i Snippets/snippet11.py

Scikit-Learn Syntax

In [7]:
mod_02 = SVC(kernel='poly', degree=2, C=20, gamma='auto')
mod_02.fit(X2, y2)
print('Training Accuracy:', mod_02.score(X2, y2))
Training Accuracy: 1.0

Example 3

In [8]:
sd = np.random.choice(range(9999))
print(sd)
np.random.seed(159)
X3, y3 = make_moons(n_samples=100, noise=0.1)

plt.figure(figsize = [8,6])
plt.scatter(X3[:, 0], X3[:, 1], marker='o', c=y3, s=50, edgecolor='k', cmap='rainbow')
plt.show()
8421
In [9]:
%run -i Snippets/snippet12.py

Scikit-Learn Syntax

In [10]:
mod_03 = SVC(kernel='poly', degree=7, C=100, gamma='auto')
mod_03.fit(X3, y3)
print('Training Accuracy:', mod_03.score(X3, y3))
Training Accuracy: 0.92