前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OCSVM 学习笔记

OCSVM 学习笔记

作者头像
EmoryHuang
发布2022-10-31 15:45:34
1K0
发布2022-10-31 15:45:34
举报
文章被收录于专栏:EmoryHuang's Blog

OCSVM 学习笔记

前言

OCSVM(OneClass SVM) 算法是一种经典的异常检测算法,基本原理与 SVM 类似,与 SVM 关注的二分类问题不同的是,就像它的名字 OneClass SVM 那样,OCSVM 只有一个分类,这也正是异常检测所需要的,我们不关注那些异常的数据点。

如果你不了解 SVM 可以点击这里: 支持向量机(SVM)学习笔记

OCSVM 简介

Our strategy is to map the data into the feature space corresponding to the kernel, and to separate them from the origin with maximum margin.

OCSVM 的基本思想是: 将数据映射到与内核相对应的特征空间,在数据与原点间构建超平面,并且最大化超平面到零点的距离

类似的,我们可以得到最优化问题的目标函数和优化条件:

其中

为松弛变量,

用于调节松弛程度 ,F 为特征空间:

目标函数和优化条件之后,类似的,写出拉格朗日方程,得到对偶问题求解:

另外一方面,有分类函数 f(x)

算法示例

我们可以通过 sklearn 简单的实现 OCSVM。

代码语言:javascript
复制
>>> from sklearn.svm import OneClassSVM
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
>>> clf = OneClassSVM(gamma='auto').fit(X)
>>> clf.predict(X)
array([-1,  1,  1,  1, -1])
>>> clf.score_samples(X)
array([1.7798..., 2.0547..., 2.0556..., 2.0561..., 1.7332...])

下面是 sklearn 上的一个简单案例:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager
from sklearn import svm

xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))
# Generate train data
X = 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 observations
X_outliers = np.random.uniform(low=0.1, high=4, size=(20, 2))
# fit the model
clf = 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].size
n_error_test = y_pred_test[y_pred_test == -1].size
n_error_outlier = y_pred_outliers[y_pred_outliers == 1].size

# plot the line , the points, and the nearest vectors to the plane
Z = 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 =40
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')

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()

参考文献

  • B. Schölkopf, R. Williamson, A. Smola, J. Shawe-Taylor, and J. Platt, “Support vector method for novelty detection,” in Advances in Neural Information Processing Systems, vol. 12. Denver, CO, USA: MIT Press, Aug./Sep. 1999, pp. 582–588.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-10-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OCSVM 学习笔记
    • 前言
      • OCSVM 简介
        • 算法示例
          • 参考文献
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档