
1 异常检测
对于一些已有的数据,我们希望通过对数据的区分,从中找出一些异常的情况。
这样做的目的是什么呢?
例如对于飞机来说,我们可能需要迅速定位可能出现异常的引擎;
例如对于用户账号来说,我们可能需要发现账号是否被盗;
例如对于电商来说,我们可能需要识别刷单的行为。
从已有数据中找到异常的数据,在日常生活中应用非常广泛。
还记得之前看到一篇文章,教育贷款客户的人际网络做成关系图,正常客户和骗贷客户是有着明显差异的,有兴趣可以点击文章链接查看。
那么机器是如何识别出来异常情况呢?
用前面所说的飞机引擎为例,我们有关于温度和震动的数据:

然后我们可以给这些数据圈定一个范围:

如果数据落点在范围之外,那么就判断这是异常的数据,例如左上角箭头标记的数据:

这就是异常检测 (Anomaly detection)。
不同的圈子代表着不同的敏感度,圈子越小,误判的几率就越大。
例如我们认定绿圈之外的都是异常值,那么检测出来的异常就比较多,而如果我们认定黄圈之外的才是异常值,那么检测出来的异常就比较少。
2 公式与计算
异常检测的过程,其实是衡量数据密度的过程。数据分布集中的数据被认为是正常的,分布偏离中心的数据被认为是异常的。
对于这样衡量数据的密度情况,可以使用高斯分布(正态分布)来计算各项数据的概率。
假设一个随机变量 X 服从高斯分布,那么可以记为:

其中:


密度概率的函数为:

其曲线大概是这样的:

平均值 μ 决定了其中心位置,标准差 σ 决定了其宽度。
我们看一下 μ 和 σ 的变化会怎么影响图像的:

如果有两个特征 x1 和 x2 的数据:

那么对于 x1 和 x2 ,我们大概可以得到这样的高斯分布:

两者一起的密度概率 p(x) ,我们可以这样衡量:

对应到三维图示如下,图中落点的高度就是概率:

那么扩展开来,对于 n 维的数据集 X :

我们需要对每一个特征计算密度概率,并通过累积汇总:

最终的结果就是判断是否为异常的概率。
3 如何使用
异常检测的数据一般来说属于偏斜类,也就是正常的数据很多,异常的数据很少。
还是用之前的飞机引擎为例,假如我们有 10000 组正常的数据,20 组异常的数据。
这时候我们可以建立异常检测模型,将他们划分为 3 种数据集:
训练集:6000 组正常数据
验证集:2000 组正常数据,10 组异常数据
测试集:2000 组正常数据,10 组异常数据
训练集用来训练模型 p(x) 。
验证集用来选择合适的错误临界点 ε (我们判断 p(x) < ε 时为异常数据),并通过计算查准率、召回率和F1Score 衡量算法效果:



测试集用来检验最终的结果。
文章转载自公众号:止一之路
网友评论