異常檢測
異常檢測(Anomaly Detection):異常檢測就是從數(shù)據(jù)集中檢測出異常樣本晌砾,是一種無監(jiān)督學習咙好。
引例
飛機制造商在飛機引擎從生產線上流出時温学,會考慮進行異常檢測,以防止不合格引擎對整機造成的巨大影響孽查,而為了進行異常檢測琐簇,通常就需要采集一些特征,比如會采集如下特征:
-
=引擎運轉時產生的熱量
-
=引擎的振蕩頻率
對于一系列的數(shù)據(jù)集(特征向量集合):,
, 這些數(shù)據(jù)都是正常樣本沃于,我們將其繪制到二維平面上:
如果一個新的測試樣本居于樣本布密度較大的地方如:
那么我們有很大的把握認為這個測試樣本是正常的。
反之如果一個新的測試樣本遠離分布集中的地方如:
那么我們也有很大的把握認為這個測試樣本是正常的海诲。
小結:
如果我們擁有一個測試集,我們根據(jù)已知的數(shù)據(jù)集建立模型
繁莹,該模型可以將正常樣本與異常樣本分離。
建立模型
高斯分布(正態(tài)分布)
正態(tài)分布可以表示成,表示
服從均值為
,方差為
的正態(tài)分布特幔。
參數(shù)估計:
若有 咨演,
證明可以中最大似然估計。
異常檢測算法
訓練集: 蚯斯,
建立模型:
參數(shù)擬合:
計算
判斷是否小于
薄风,若小于
則為異常饵较。
異常檢測算法的評估
- 對數(shù)據(jù)按6:2:2比例進行分配,分別為訓練集,交叉驗證集遭赂,測試集循诉,訓練集中全是無標簽數(shù)據(jù),異常數(shù)據(jù)在交叉驗證集與測試集中按比例進行分配
- 通過訓練集對參數(shù)進行擬合
- 對交叉驗證集和測試集中的數(shù)據(jù)進行測試
- 由于異常樣本的數(shù)量非常的少撇他,導致預測十分偏斜茄猫,可以通過考察準確率,召回率困肩,F(xiàn)1值來評估模型的效果划纽。
- 通過交叉驗證集來調節(jié)參數(shù)
異常檢測與監(jiān)督學習
因為我們可能已經知道了訓練數(shù)據(jù)是否為異常數(shù)據(jù),那么就難免有個疑惑我們?yōu)槭裁床挥帽O(jiān)督學習的算法比如logistics regression來做呢僻弹?
下面我們來比較一下異常檢測與監(jiān)督學習
項目 | 異常檢測 | 邏輯回歸 |
---|---|---|
樣本 | 異常樣本數(shù)量少(0~20)阿浓,大量負樣本 | 正負樣本數(shù)量都很多 |
應用 | 欺詐檢測,工業(yè)制造蹋绽,數(shù)據(jù)中心的監(jiān)測機器 | 垃圾郵件分類芭毙,天氣預報,癌癥判斷 |
注:大量的正樣本可以讓算法學習到正樣本的特征卸耘,并且肯能出現(xiàn)的正樣本與訓練集中的正樣本相似退敦,而異常可能是從未出現(xiàn)過的異常蚣抗。
數(shù)據(jù)處理
通常我們先畫出特征值的柱狀圖侈百,看其是否接近與高斯分布,若不是我們可以對特征值進行相關的處理翰铡,使其接近于高斯分布钝域,例如取對數(shù),取冪等等锭魔。特征值的分布越接近高斯分布則算法的效果越好例证。
多元高斯分布
我們不再單獨考慮每個特征值的高斯分布而是考慮特征向量的高斯分布
算法流程
參數(shù)擬合
剩下的流程同高斯分布相同。
高斯分布與多元高斯分布比較
高斯分布 | 多元高斯分布 |
---|---|
需要手動創(chuàng)建新的特征去捕獲不正常變量值的組合 | 自動捕獲不同特征變量之間的相關性 |
運算亮小迷捧,適應n很大的情況织咧,即使m很小也可以運行的很好 | 計算量大,m必須大于n,通常當m>=10時才考慮 |
注:如果發(fā)現(xiàn)是不可逆的一般有兩種情況
- m< n
- 有冗余變量(變量間存在線性相關的關系)
MatlabCode
參數(shù)擬合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 correctly
mu = 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;
end
end
end