http://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html

In [249]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
In [250]:
def generates_points(n_sep, n_rand):
    # we create n_sep separable points + n_rand random
    n_sep_by_class = int(n_sep/2)
    np.random.seed(0)
    X = np.r_[np.random.randn(n_sep_by_class+n_rand/2, 2) - [2, 2], np.random.randn(n_sep_by_class+n_rand/2, 2) + [2, 2]]
    Y = [0] * n_sep_by_class +list(np.random.binomial(1, 0.5, size=n_rand)) + [1] * n_sep_by_class
    return X, Y
In [255]:
def plot(X, Y, C=1):
# fit the model
    clf = svm.SVC(kernel='linear', C=C)
    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]

    # plot the parallels to the separating hyperplane that pass through the
    # support vectors
    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])

    # plot the line, the points, and the nearest vectors to the plane
    plt.plot(xx, yy, 'k-')
    plt.plot(xx, yy_down, 'k--')
    plt.plot(xx, yy_up, 'k--')

    plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
                s=80, facecolors='none')
    plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)

    plt.axis('tight')
    plt.title("C=%s"%C)
    plt.show()
In [261]:
X, Y = generates_points(100, 0)
plot(X, Y)

Varying C parameter

In [262]:
X, Y = generates_points(20, 0)
for C in [0.1, 1, 2, 10]:
    plot(X, Y, C)

Non linearly separable space

In [263]:
X, Y = generates_points(20, 4)
for C in [0.1,1, 2, 10]:
    plot(X, Y, C)

Non linear kernels

In [243]:
def plot_poly(X, Y, C,kernel="poly", degree=2):
# fit the model
    h = .02 
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
    clf = svm.SVC(kernel=kernel, C=C, degree=degree)
    clf.fit(X, Y)

    Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    
    plt.contour(xx,yy, Z, levels=[0], linewidths=2, colors='red')
    plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
                s=80, facecolors='none')
    plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)

    plt.axis('tight')
    plt.title("C=%s"%C)
    plt.show()
In [264]:
np.random.seed(0)
X = np.random.randn(100, 2)
Y = [int(e[0]*e[0]+e[1]*e[1] > 1) for e in X]
for C in [0.1,1, 2, 10]:
    plot_poly(X, Y, C, kernel="poly")

Polynomial kernel with a too large degree

In [265]:
np.random.seed(0)
X = np.random.randn(100, 2)
Y = [int(e[0]*e[0]+e[1]*e[1] > 1) for e in X]
for C in [0.1,10]:
    plot_poly(X, Y, C, degree=11)

RBF Kernel

Separable but not linearly separable data
In [236]:
np.random.seed(0)
X = np.random.randn(100, 2)
Y = [int(e[0]>e[1]**3) for e in X]
for C in [0.1,1, 2, 10]:
    plot_poly(X, Y, C, kernel="rbf")
In []: