# Code source: Gael Varoqueux # Modified for Documentation merge by Jaques Grobler import numpy as np import pylab as pl from sklearn import svm Y = [-1] * 4 + [1] * 5 X = np.array([ [-2, 4], [4, 1], [2, -2], [1, 1], [1, 6], [2, 4], [6, 2], [7, 7], [8, 8], ]) # fit the model clf = svm.SVC(kernel='linear', C=0.1) clf.fit(X, Y) # get the separating hyperplane w = clf.coef_[0] a = -w[0] / w[1] b = -(clf.intercept_[0]) / w[1] xx = np.linspace(-10, 10) yy = a * xx + b # plot the parallels to the separating hyperplane that pass through the # support vectors margin = 1 / np.sqrt(np.sum(clf.coef_ ** 2)) yy_down = yy + a * margin yy_up = yy - a * margin pl.plot(xx, yy, 'k-') pl.plot(xx, yy_down, 'k--') pl.plot(xx, yy_up, 'k--') pl.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80, facecolors='red', zorder=10) pl.scatter(X[:, 0], X[:, 1], c=Y, facecolors='black', zorder=10) pl.show()