ITPub博客

首页 > 数据库 > 数据库开发技术 > 异常检测方法No.003

异常检测方法No.003

原创 数据库开发技术 作者:quincyqiang 时间:2021-06-17 10:00:00 0 删除 编辑

前面的文章我们介绍完了LOF局部异常检测算法的若干定义、算法步骤和代码实现。今天我们来介绍另一个有趣异常检测算法one-class-SVM。


01

简介

one-class-SVM是指训练数据集中只有一类正的或者负的数据,而没有另外的一类。在这里,我们需要学习的实际上是训练数据的边界,但是这时我们不能使用“最大化边界”了,因为我们没有两类数据。所以,Scholkopf假设最好的边界要远离特征空间中的原点。


02

OneClass SVM 主要参数和方法
class sklearn.svm.OneClassSVM(kernel='rbf',degree=3, gamma='auto',coef0=0.0, tol=0.001, nu=0.5, shrinking=True, cache_size=200, verbose=False,max_iter=-1, random_state=None)


参数:

属性:方法:


03

sklearn实现:OneClasssSVM
import numpy as npimport matplotlib.pyplot as pltimport matplotlib.font_managerfrom sklearn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))# Generate train dataX = 0.3 * np.random.randn(100, 2)X_train = np.r_[X + 2, X - 2]X_test = np.r_[X + 2, X-2]# Generate some abnormal novel observationsX_outliers = np.random.uniform(low=0.1, high=4, size=(20, 2))# fit the modelclf = svm.OneClassSVM(nu=0.1, kernel='rbf', gamma=0.1)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].sizen_error_test = y_pred_test[y_pred_test == -1].sizen_error_outlier = y_pred_outliers[y_pred_outliers == 1].size # plot the line , the points, and the nearest vectors to the planeZ = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])Z = Z.reshape(xx.shape) 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, Z.max()], colors='palevioletred') s =40b1 = 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') plt.axis('tight')plt.xlim((-5, 5))plt.ylim((-5, 5))plt.legend([a.collections[0], b1, b2, c],           ["learned frontier", 'training observations',            "new regular observations", "new abnormal observations"],           loc="upper left",           prop=matplotlib.font_manager.FontProperties(size=11))plt.xlabel(    "error train: %d/200; errors novel regular: %d/40; errors novel abnormal:%d/40"%(        n_error_train, n_error_test, n_error_outlier)    )plt.show()



04

参考文献


https://www.cnblogs.com/wj-1314/p/10701708.html

https://zhuanlan.zhihu.com/p/32784067

https://blog.csdn.net/bbbeoy/article/details/79159652

https://blog.csdn.net/u013719780/article/details/53219997

https://www.cnblogs.com/damumu/p/7320334.html

扫描下方的二维码进入ChallengeHub粉丝群,也可以添加管理员微信拉您入群。
与爱好AI的朋友们共同成长
ChallengeHub粉丝群
管理员微信





原文作者:ChallengeHub;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-01-07

  • 博文量
    350
  • 访问量
    161006