import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
SVM
SVM
# Linearly separable data in 2d
# Generate data
0)
np.random.seed(= np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
X = [0] * 20 + [1] * 20
y
# Plot data
0], X[:, 1], c=y, cmap=cm.Paired) plt.scatter(X[:,
# Fit SVM
from sklearn.svm import SVC
= SVC(kernel='linear')
clf
clf.fit(X, y)
# Plot decision boundary
= clf.coef_[0]
w = -w[0] / w[1]
a = np.linspace(-5, 5)
xx = a * xx - (clf.intercept_[0]) / w[1]
yy 'k-')
plt.plot(xx, yy,
# Plot support vectors
0], clf.support_vectors_[:, 1], s=80, facecolors='none')
plt.scatter(clf.support_vectors_[:,
# Plot margins
= clf.support_vectors_[0]
b = a * xx + (b[1] - a * b[0])
yy_down = clf.support_vectors_[-1]
b = a * xx + (b[1] - a * b[0])
yy_up 'k--')
plt.plot(xx, yy_down, 'k--')
plt.plot(xx, yy_up,
# Plot data
0], X[:, 1], c=y, cmap=cm.Paired) plt.scatter(X[:,
# Magnitude of w
np.linalg.norm(w)
1.111010607589106