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
|
|
|
|
2013-07-22 21:48:44 +08:00
|
|
|
An example using a one-class SVM for novelty detection.
|
|
|
|
|
|
2011-12-20 00:15:58 +08:00
|
|
|
:ref:`One-class SVM <svm_outlier_detection>` is an unsupervised
|
|
|
|
|
algorithm that learns a decision function for novelty detection:
|
|
|
|
|
classifying new data as similar or different to the training set.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2010-04-23 13:52:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-11-07 07:21:24 +08:00
|
|
|
import matplotlib.font_manager
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import svm
|
2010-04-23 13:52:16 +08:00
|
|
|
|
2011-11-07 07:21:24 +08:00
|
|
|
xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))
|
|
|
|
|
# Generate train data
|
2010-08-09 05:34:03 +08:00
|
|
|
X = 0.3 * np.random.randn(100, 2)
|
2011-11-07 07:21:24 +08:00
|
|
|
X_train = np.r_[X + 2, X - 2]
|
|
|
|
|
# Generate some regular novel observations
|
|
|
|
|
X = 0.3 * np.random.randn(20, 2)
|
|
|
|
|
X_test = np.r_[X + 2, X - 2]
|
|
|
|
|
# Generate some abnormal novel observations
|
|
|
|
|
X_outliers = np.random.uniform(low=-4, high=4, 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)
|
2011-11-07 07:21:24 +08:00
|
|
|
clf.fit(X_train)
|
|
|
|
|
y_pred_train = clf.predict(X_train)
|
|
|
|
|
y_pred_test = clf.predict(X_test)
|
|
|
|
|
y_pred_outliers = clf.predict(X_outliers)
|
|
|
|
|
n_error_train = y_pred_train[y_pred_train == -1].size
|
|
|
|
|
n_error_test = y_pred_test[y_pred_test == -1].size
|
|
|
|
|
n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size
|
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)
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title("Novelty Detection")
|
2015-10-22 19:59:52 +08:00
|
|
|
plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu)
|
|
|
|
|
a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="darkred")
|
|
|
|
|
plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred")
|
2014-05-15 04:31:03 +08:00
|
|
|
|
2015-10-22 19:59:52 +08:00
|
|
|
s = 40
|
2017-03-05 00:22:00 +08:00
|
|
|
b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
|
|
|
|
b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
|
|
|
|
|
c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis("tight")
|
|
|
|
|
plt.xlim((-5, 5))
|
|
|
|
|
plt.ylim((-5, 5))
|
|
|
|
|
plt.legend(
|
|
|
|
|
[a.collections[0], b1, b2, c],
|
2014-05-15 10:35:13 +08:00
|
|
|
[
|
|
|
|
|
"learned frontier",
|
|
|
|
|
"training observations",
|
|
|
|
|
"new regular observations",
|
|
|
|
|
"new abnormal observations",
|
|
|
|
|
],
|
|
|
|
|
loc="upper left",
|
|
|
|
|
prop=matplotlib.font_manager.FontProperties(size=11),
|
|
|
|
|
)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel(
|
2014-01-08 14:50:11 +08:00
|
|
|
"error train: %d/200 ; errors novel regular: %d/40 ; errors novel abnormal: %d/40"
|
2011-11-07 07:21:24 +08:00
|
|
|
% (n_error_train, n_error_test, n_error_outliers)
|
2021-10-07 16:13:00 +08:00
|
|
|
)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|