1. 程式人生 > >【吳恩達】機器學習第16章異常檢測以及ex8部分程式設計練習

【吳恩達】機器學習第16章異常檢測以及ex8部分程式設計練習

1.異常檢測

1.1思路簡述

首先是一些沒有標籤的資料進行p(x)建模,就是擬合數據得到一個符合資料規律的p(x),然後根據一個特定的閾值來判斷,是否異常。

1.2具體步驟(假設p(x)符合高斯)

在octave中,我們可以使用hist視覺化直方圖來看資料是否是高斯分佈。如果不是,可以使用log(x)變換得到近似高斯分佈的資料。

step1:首先是選取特徵。選取一些不大不小的重要特徵,也可以用建立一些新的特徵去捕捉異常。

step2:計算每種特徵對應的均值u_{j}、以及方差\sigma^2_{j}.

step3:根據高斯分佈p(x)的表示式,計算概率值。

step4:與閾值\varepsilon比較,看是否發生異常,小於,則異常。

1.3演算法評估

step1:我們將資料集分成三部分:假設我們資料集中有少量的異常樣本,大量正常樣本。

我們將正常樣本按照6:2:2分給訓練集、交叉驗證集、測試集。然後將異常樣本1:1分給交叉驗證集、測試集。

step2:用訓練集的樣本來擬合p(x) 也就是選定用什麼概率函式來擬合數據分佈。

step3:在交叉驗證集上確定閾值\varepsilon:比如我們選取p(x)上最大的概率,以及最小的概率。選取合適的步長,進行迭代。從最小的概率開始,計算F1分數,記錄最大的F1以及其對應的閾值。(具體見程式設計作業)。這裡選取F1分數的原因是因為有很多正常樣本少數異常樣本,存在資料傾斜,所以不適合正確比率作為指標。

2.多元高斯的異常檢測

步驟與原來的模型基本一致。

考慮了兩個特徵聯合的影響,有時候一個在原模型中兩個特徵在自己的區域都是正常的,但由於之間存在關聯,所以聯合來看,他們是異常的,這種情況在原模型中是看不出來的。比如上圖的X.

二者對比來看,原模型需要手動建立新特徵,而新模型不用。原模型的計算成本比較低,而新模型要算協方差的逆,計算成本高。原模型可以在小樣本的情況下使用,但是新模型不適合在小樣本的時候使用,樣本數m>>特徵數n的時候,使用新模型比較合適。當m<n時,協方差矩陣就會變成不可逆的。當存在冗餘特徵時,也會不可逆。從幾何圖形來看,原模型是關於特徵對稱的,而新模型則不是。協方差\sum

非對角線元素的改變會改變影象的方向。

notes:監督學習與異常檢測。監督學習適合大量樣本,(大量正樣本,大量負樣本)。異常檢測適合少量正樣本,大量負樣本。並且負樣本種類多種多樣。p(x)越小,越可能異常。

3.程式設計作業

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.
%
mu=(1/m)*sum(X);%i-th表示的是第i個特徵  求解每個特徵的均值
sigma2=(1/m)*sum(((X-mu).^2));%求每個特徵的方差

% =============================================================

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
	tp=0;
	fp=0;
	fn=0;
    predictions=(pval<epsilon);
	for i=1:size(predictions),
	    if  predictions(i)==1 & yval(i)==1,
		     tp=tp+1;
		end
		if  predictions(i)==1 & yval(i)==0,
		     fp=fp+1;
		end
		if  predictions(i)==0 & yval(i)==1,
		     fn=fn+1;
		end
	end
	prec=tp/(tp+fp);
	rec=tp/(tp+fn);
	F1=2*prec*rec/(prec+rec);

    % =============================================================

    if F1 > bestF1,
       bestF1 = F1;
       bestEpsilon = epsilon;
    end
end

end