基于高斯分布的异常检测算法一文中,详细给出了异常检测算法的原理及其公式,本文为该算法的Octave仿真。实例为,根据训练样例(一组网络服务器)的吞吐量(Throughput)和延迟时间(Latency)数据,标记出异常的服务器。

可视化的数据集如下:

我们根据数据集X,计算其二维高斯分布的数学期望mu与方差sigma2:

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); mu = zeros(n, 1);
sigma2 = zeros(n, 1); mu = sum(X,1)'/m;
%note:mu and sigma are both n-demension.
for(i=1:m)
e=(X(i,:)'-mu);
sigma2 += e.^2;
endfor sigma2 = sigma2/m end

计算概率密度:

function p = multivariateGaussian(X, mu, Sigma2)
%MULTIVARIATEGAUSSIAN Computes the probability density function of the
%multivariate gaussian distribution.
% p = MULTIVARIATEGAUSSIAN(X, mu, Sigma2) Computes the probability
% density function of the examples X under the multivariate gaussian
% distribution with parameters mu and Sigma2. If Sigma2 is a matrix, it is
% treated as the covariance matrix. If Sigma2 is a vector, it is treated
% as the \sigma^2 values of the variances in each dimension (a diagonal
% covariance matrix)
% k = length(mu); if (size(Sigma2, 2) == 1) || (size(Sigma2, 1) == 1)
Sigma2 = diag(Sigma2);
end X = bsxfun(@minus, X, mu(:)');
p = (2 * pi) ^ (- k / 2) * det(Sigma2) ^ (-0.5) * ...
exp(-0.5 * sum(bsxfun(@times, X * pinv(Sigma2), X), 2)); end

可视化后:

根据预留的一部分已知是否异常的训练样例(CV集),来选择阈值:

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) pred = (pval<epsilon); p_e_1 = (pred==1);
y_e_1 = (yval==1);
p1 = 0;
m = size(p_e_1,1);
for(i=1:m)
if((p_e_1(i)==1)&&(p_e_1(i)==y_e_1(i)))
p1++;
endif
endfor
p_12 = sum(pred);
p_13 = sum(y_e_1); p=p1/p_12;
r=p1/p_13; F1 = 2*p*r/(p+r); if F1 > bestF1
bestF1 = F1;
bestEpsilon = epsilon;
end
end end

最终的标记结果:

异常检测算法的Octave仿真的更多相关文章

  1. 如何开发一个异常检测系统:使用什么特征变量(features)来构建异常检测算法

    如何构建与选择异常检测算法中的features 如果我的feature像图1所示的那样的正态分布图的话,我们可以很高兴地将它送入异常检测系统中去构建算法. 如果我的feature像图2那样不是正态分布 ...

  2. 异常检测算法--Isolation Forest

    南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...

  3. 机器学习:异常检测算法Seasonal Hybrid ESD及R语言实现

    Twritters的异常检测算法(Anomaly Detection)做的比较好,Seasonal Hybrid ESD算法是先用STL把序列分解,考察残差项.假定这一项符合正态分布,然后就可以用Ge ...

  4. 异常检测算法:Isolation Forest

    iForest (Isolation Forest)是由Liu et al. [1] 提出来的基于二叉树的ensemble异常检测算法,具有效果好.训练快(线性复杂度)等特点. 1. 前言 iFore ...

  5. 【机器学习】异常检测算法(I)

    在给定的数据集,我们假设数据是正常的 ,现在需要知道新给的数据Xtest中不属于该组数据的几率p(X). 异常检测主要用来识别欺骗,例如通过之前的数据来识别新一次的数据是否存在异常,比如根据一个用户以 ...

  6. kaggle信用卡欺诈看异常检测算法——无监督的方法包括: 基于统计的技术,如BACON *离群检测 多变量异常值检测 基于聚类的技术;监督方法: 神经网络 SVM 逻辑回归

    使用google翻译自:https://software.seek.intel.com/dealing-with-outliers 数据分析中的一项具有挑战性但非常重要的任务是处理异常值.我们通常将异 ...

  7. 异常检测(Anomaly detection): 异常检测算法(应用高斯分布)

    估计P(x)的分布--密度估计 我们有m个样本,每个样本有n个特征值,每个特征都分别服从不同的高斯分布,上图中的公式是在假设每个特征都独立的情况下,实际无论每个特征是否独立,这个公式的效果都不错.连乘 ...

  8. 异常检测算法Robust Random Cut Forest(RRCF)关键定理引理证明

    摘要:RRCF是亚马逊发表的一篇异常检测算法,是对周志华孤立森林的改进.但是相比孤立森林,具有更为扎实的理论基础.文章的理论论证相对较为晦涩,且没给出详细的证明过程.本文不对该算法进行详尽的描述,仅对 ...

  9. 时间序列异常检测算法S-H-ESD

    1. 基于统计的异常检测 Grubbs' Test Grubbs' Test为一种假设检验的方法,常被用来检验服从正太分布的单变量数据集(univariate data set)\(Y\) 中的单个异 ...

随机推荐

  1. HDU 3228 题解(最小生成树)(Kruskal)(内有详细注释)

    Problem Description A group of explorers has found a solitary island. They land on the island and ex ...

  2. TensorFlow 安装及使用

    安装 (1)安装包安装:pip install tensorflow==1.14 -i https://pypi.douban.com/simple virtualenv -p /usr/bin/py ...

  3. alert(1) to win 15

  4. bzoj2325 [ZJOI2011]道馆之战 树链剖分+DP+类线段树最大字段和

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2325 题解 可以参考线段树动态维护最大子段和的做法. 对于线段树上每个节点 \(o\),维护 ...

  5. adb server version (31) doesn’t match this client (36); killing…

    版权声明:蜜蜂采花酿蜂蜜,奶牛吃草产牛奶. https://blog.csdn.net/codehxy/article/details/52175186 案例1 报错信息如下 C:\Users\lin ...

  6. zabbix agentd错误日志解决办法

    公司新上了一台服务器,我安装了zabbix_agents软件包,并复制了zabbix server端的zabbix_agentd.conf到/etc/zabbix里面并修改了相关的参数,并启动了zab ...

  7. Spring---异步消息

    1.异步消息 1.1.目的:   为了  系统与系统  之间的通信: 1.2.概念: 异步消息  :消息的   发送者  无需 等待消息  接收者的处理及返回,甚至 无需 关心消息是否发送成功: 1. ...

  8. Test 6.29 T3 小学生

    问题描述 "不错,不错!那么,准备好迎接下一道题了么?"一道白光闪过,CJK 眼前出现了 1e100 个小学生."他们中,有一些人轨了我的机子.现在,我需要你在 1S 之 ...

  9. UVA 11178 Morley's Theorem (计算几何)

    题目链接 lrj训练指南 P259 //==================================================================== Point getP( ...

  10. 卷积神经网络(Text--cnn)(知识点整理)

    参考:http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/ 独热编码(one- ...