Chapter 1


In [1]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import svm, datasets

In [3]:
# we create 40 separable points
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = [0] * 20 + [1] * 20

fig = plt.figure(2)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.bwr);



In [4]:
clf = svm.SVC(kernel='linear', C=1e10)
clf.fit(X, Y)

# get the separating hyperplane
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf.intercept_[0]) / w[1]
b = clf.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = clf.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])

fig = plt.figure()
plt.plot(xx, yy, 'k-')
plt.plot(xx, yy_down, 'k--')
plt.plot(xx, yy_up, 'k--')
plt.axis('tight')
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80, facecolors='white')
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.bwr);



In [7]:
# we create 80 non separable points
X, Y = datasets.make_circles(n_samples=80, shuffle=True, noise=None, random_state=None, factor=0.5)
X = X * 2 + np.random.randn(80, 2) * 0.2

fig = plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.bwr);



In [8]:
# Add a new dimension x_3 = x_1^2 + x_2^2
r3 = lambda x: [x[0]**2 + x[1]**2]
d3 = array(apply_along_axis(r3, 1, X))
X = hstack((X, d3))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=Y, cmap=plt.cm.bwr);



In [9]:
clf = svm.SVC(kernel='linear', C=1e10)
clf.fit(X, Y)

# Get the separator plane mesh (hardcoded, it's just an approximate)
Mx, My = np.meshgrid(np.arange(-2, 2, 0.1), np.arange(-2, 2, 0.1))
Mz = np.ones(Mx.shape) * 2

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(Mx, My, Mz, color='#888888')
ax.scatter(X[Y==1][:, 0], X[Y==1][:, 1], X[Y==1][:, 2], c='r', cmap=plt.cm.Spectral)
ax.scatter(X[Y!=1][:, 0], X[Y!=1][:, 1], X[Y!=1][:, 2], c='b', cmap=plt.cm.Spectral);



Code for linearly separable example by Gaël Varoquaux under BSD 3 clause license, customized and extended by Carlos García Márquez.