2010-04-23 13:52:16 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
==============
|
2010-04-23 13:52:16 +08:00
|
|
|
Non-linear SVM
|
2010-11-02 18:38:06 +08:00
|
|
|
==============
|
2010-04-23 13:52:16 +08:00
|
|
|
|
2010-11-02 18:38:06 +08:00
|
|
|
Perform binary classification using non-linear SVC
|
|
|
|
|
with RBF kernel. The target to predict is a XOR of the
|
|
|
|
|
inputs.
|
2010-08-20 21:45:14 +08:00
|
|
|
|
2010-04-23 13:52:16 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
print __doc__
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
|
|
|
|
from scikits.learn 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
|
2010-08-20 21:45:14 +08:00
|
|
|
clf = svm.NuSVC()
|
2010-04-23 13:52:16 +08:00
|
|
|
clf.fit(X, Y)
|
|
|
|
|
|
|
|
|
|
# plot the line, the points, and the nearest vectors to the plane
|
|
|
|
|
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
|
|
|
|
|
Z = Z.reshape(xx.shape)
|
|
|
|
|
|
|
|
|
|
pl.set_cmap(pl.cm.Paired)
|
|
|
|
|
pl.pcolormesh(xx, yy, Z)
|
|
|
|
|
pl.scatter(X[:,0], X[:,1], c=Y)
|
|
|
|
|
|
|
|
|
|
pl.axis('tight')
|
|
|
|
|
pl.show()
|
|
|
|
|
|