38 lines
739 B
Python
38 lines
739 B
Python
"""
|
|
==============
|
|
Non-linear SVM
|
|
==============
|
|
|
|
Perform binary classification using non-linear SVC
|
|
with RBF kernel. The target to predict is a XOR of the
|
|
inputs.
|
|
|
|
"""
|
|
print __doc__
|
|
|
|
import numpy as np
|
|
import pylab as pl
|
|
from sklearn import svm
|
|
|
|
xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))
|
|
np.random.seed(0)
|
|
X = np.random.randn(300, 2)
|
|
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
|
|
|
|
# fit the model
|
|
clf = svm.NuSVC()
|
|
clf.fit(X, 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)
|
|
|
|
pl.set_cmap(pl.cm.jet)
|
|
pl.pcolormesh(xx, yy, Z)
|
|
pl.scatter(X[:, 0], X[:, 1], c=Y)
|
|
pl.xticks(())
|
|
pl.yticks(())
|
|
|
|
pl.axis('tight')
|
|
pl.show()
|