Fork me on GitHub

This documentation is for scikit-learn version 0.17Other versions

If you use the software, please consider citing scikit-learn.

Non-linear SVMΒΆ

Perform binary classification using non-linear SVC with RBF kernel. The target to predict is a XOR of the inputs.

The color map illustrates the decision function learned by the SVC.


Python source code:


import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm

xx, yy = np.meshgrid(np.linspace(-3, 3, 500),
                     np.linspace(-3, 3, 500))
X = np.random.randn(300, 2)
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)

# fit the model
clf = svm.NuSVC(), Y)

# plot the decision function for each datapoint on the grid
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.imshow(Z, interpolation='nearest',
           extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto',
contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2,
plt.scatter(X[:, 0], X[:, 1], s=30, c=Y,
plt.axis([-3, 3, -3, 3])

Total running time of the example: 1.11 seconds ( 0 minutes 1.11 seconds)