简体   繁体   中英

One class SVM toy example not understood

I've been playing around with one class SVM. I think I understand the theory behind it (that it attempts to separate the data from the origin). I attempted to run a toy example which the algorithm should fit perfectly; however it seems like I'm missing something since the algorithm doesn't classify all the training examples as non anomalies. Code:

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

xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))

x = np.array([1, 3, 1, 3, 2])
y = np.array([1, 1, 3, 3, 2])
feature = np.vstack((x, y)).T
clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf.fit(feature)
y_pred_train = clf.predict(feature)
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) 
Z = Z.reshape(xx.shape)
print(y_pred_train)
print(feature)
plt.title("Novelty Detection")
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')

b1 = plt.scatter(feature[:, 0], feature[:, 1], c='white', s=40, 
edgecolors='k')

plt.show()

the prediction on the training set is [ 1 -1 1 1 1], which does not make sense.

Please advise.

One Class SVM is taking at least one outlier (anomaly) among your dataset. This is why you see one "wrong" label in the predicted labels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM