异常检测
来源:互联网 发布:网络销售贵金属好做吗 编辑:程序博客网 时间:2024/04/30 03:45
异常检测
异常检测(Anomaly Detection):异常检测就是从数据集中检测出异常样本,是一种无监督学习。
引例
飞机制造商在飞机引擎从生产线上流出时,会考虑进行异常检测,以防止不合格引擎对整机造成的巨大影响,而为了进行异常检测,通常就需要采集一些特征,比如会采集如下特征:
x1 =引擎运转时产生的热量x2 =引擎的振荡频率
对于一系列的数据集(特征向量集合):
如果一个新的测试样本居于样本布密度较大的地方如:
那么我们有很大的把握认为这个测试样本是正常的。
反之如果一个新的测试样本远离分布集中的地方如:
那么我们也有很大的把握认为这个测试样本是正常的。
小结:
如果我们拥有一个测试集
建立模型
高斯分布(正态分布)
正态分布可以表示成
参数估计:
若有
证明可以中最大似然估计。
异常检测算法
训练集:
建立模型:
参数拟合:
计算
判断
异常检测算法的评估
- 对数据按6:2:2比例进行分配,分别为训练集,交叉验证集,测试集,训练集中全是无标签数据,异常数据在交叉验证集与测试集中按比例进行分配
- 通过训练集对参数进行拟合
- 对交叉验证集和测试集中的数据进行测试
- 由于异常样本的数量非常的少,导致预测十分偏斜,可以通过考察准确率,召回率,F1值来评估模型的效果。
- 通过交叉验证集来调节参数
ϵ
异常检测与监督学习
因为我们可能已经知道了训练数据是否为异常数据,那么就难免有个疑惑我们为什么不用监督学习的算法比如logistics regression来做呢?
下面我们来比较一下异常检测与监督学习
注:大量的正样本可以让算法学习到正样本的特征,并且肯能出现的正样本与训练集中的正样本相似,而异常可能是从未出现过的异常。
数据处理
通常我们先画出特征值的柱状图,看其是否接近与高斯分布,若不是我们可以对特征值进行相关的处理,使其接近于高斯分布,例如取对数,取幂等等。特征值的分布越接近高斯分布则算法的效果越好。
多元高斯分布
我们不再单独考虑每个特征值的高斯分布而是考虑特征向量
算法流程
参数拟合
剩下的流程同高斯分布相同。
高斯分布与多元高斯分布比较
注:如果发现
- m< n
- 有冗余变量(变量间存在线性相关的关系)
MatlabCode
参数拟合Code
function [mu sigma2] = estimateGaussian(X)%ESTIMATEGAUSSIAN This function estimates the parameters of a %Gaussian distribution using the data in X% [mu sigma2] = estimateGaussian(X), % The input X is the dataset with each n-dimensional data point in one row% The output is an n-dimensional vector mu, the mean of the data set% and the variances sigma^2, an n x 1 vector% % Useful variables[m, n] = size(X);% You should return these values correctlymu = zeros(n, 1);sigma2 = zeros(n, 1);% ====================== YOUR CODE HERE ======================% Instructions: Compute the mean of the data and the variances% In particular, mu(i) should contain the mean of% the data for the i-th feature and sigma2(i)% should contain variance of the i-th feature.%for i=1:n mu(i)=sum(X(:,i))/m;end;for i=1:n sigma2(i)=sum((X(:,i)-mu(i)).^2)/m;end; % =============================================================end
更新
function [bestEpsilon bestF1] = selectThreshold(yval, pval)%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting%outliers% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best% threshold to use for selecting outliers based on the results from a% validation set (pval) and the ground truth (yval).%bestEpsilon = 0;bestF1 = 0;F1 = 0;stepsize = (max(pval) - min(pval)) / 1000;for epsilon = min(pval):stepsize:max(pval) % ====================== YOUR CODE HERE ====================== % Instructions: Compute the F1 score of choosing epsilon as the % threshold and place the value in F1. The code at the % end of the loop will compare the F1 score for this % choice of epsilon and set it to be the best epsilon if % it is better than the current choice of epsilon. % % Note: You can use predictions = (pval < epsilon) to get a binary vector % of 0's and 1's of the outlier predictions predicted = (pval<epsilon); truepostive = sum((predicted==1)&(yval==1)); falsepostive = sum((predicted==1)&(yval==0)); falsenegative = sum((predicted==0)&(yval==1)); pre = truepostive/(truepostive+falsepostive); rec = truepostive/(truepostive+falsenegative); F1 = 2*pre*rec/(pre+rec); % ============================================================= if F1 > bestF1 bestF1 = F1; bestEpsilon = epsilon; endendend
1 0
- 异常检测
- 异常检测
- 异常检测
- 异常检测
- 异常检测
- 异常检测
- 异常检测
- 异常检测
- VxWorks异常任务检测
- TCP异常断开检测
- 异常细胞检测
- 检测异常细胞
- 网络异常检测
- 异常数据检测
- java中异常检测
- 异常声音检测总结
- 异常人脸检测
- 异常检测 Tips
- ORACLE--COUNT()函数使用
- 网页设计基础
- 抓取远程图片到本地,小实例
- n & (n-1) 的妙用
- BZOJ 3289: Mato的文件管理|分块|树状数组
- 异常检测
- UI_炫彩方框,旋转跑马灯,炫彩画板
- Ubuntu使用技巧——如何创建图标快捷方式
- Java集合---HashMap源码剖析
- SpringMVC使用了@ResponseBody报406错误的问题(1)
- IOS项目集成ShareSDK分享登录功能
- Android性能优化常用方法
- Hive学习笔记(三) 创建数据库与表
- uva 10739 String to Palindrome