2010-04-23 13:52:16 +08:00
|
|
|
"""
|
2010-08-09 05:34:03 +08:00
|
|
|
==========================================
|
|
|
|
|
One-class SVM with non-linear kernel (RBF)
|
|
|
|
|
==========================================
|
2010-11-02 18:38:06 +08:00
|
|
|
|
|
|
|
|
One-class SVM is an unsupervised algorithm that
|
|
|
|
|
estimates outliers in a dataset.
|
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
|
|
|
|
|
|
2010-08-09 05:34:03 +08:00
|
|
|
xx, yy = np.meshgrid(np.linspace(-7, 7, 500), np.linspace(-7, 7, 500))
|
|
|
|
|
X = 0.3 * np.random.randn(100, 2)
|
|
|
|
|
X = np.r_[X + 2, X - 2]
|
|
|
|
|
|
|
|
|
|
# Add 10 % of outliers (leads to nu=0.1)
|
|
|
|
|
X = np.r_[X, np.random.uniform(low=-6, high=6, size=(20, 2))]
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# fit the model
|
2010-08-09 05:34:03 +08:00
|
|
|
clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
|
|
|
|
|
clf.fit(X)
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# plot the line, the points, and the nearest vectors to the plane
|
2010-11-18 17:36:05 +08:00
|
|
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
2010-04-23 13:52:16 +08:00
|
|
|
Z = Z.reshape(xx.shape)
|
2010-08-09 05:34:03 +08:00
|
|
|
y_pred = clf.predict(X)
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
pl.set_cmap(pl.cm.Paired)
|
2010-08-09 05:34:03 +08:00
|
|
|
pl.contourf(xx, yy, Z)
|
|
|
|
|
pl.scatter(X[y_pred>0,0], X[y_pred>0,1], c='white', label='inliers')
|
|
|
|
|
pl.scatter(X[y_pred<=0,0], X[y_pred<=0,1], c='black', label='outliers')
|
2010-04-23 13:52:16 +08:00
|
|
|
pl.axis('tight')
|
2010-08-09 05:34:03 +08:00
|
|
|
pl.legend()
|
2010-04-23 13:52:16 +08:00
|
|
|
pl.show()
|