通过数据的高斯分布来判断新数据是否是异常,步骤:
- 根据公式计算高斯参数:mu和sigma
- 计算交叉验证数据的F1 score评估,来获取最优阈值epsilon
- 通过1、2获取的参数,可以预估新数据的数值;
原始数据:
数据分布:
1. 根据公式计算高斯参数:mu和sigma
estimateGaussian.m
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,1)';%对每个特征的训练数据求均值, sum(X,1)是矩阵X每列求和, mu(n,1)
sigma2 = 1/m.*(sum((X-repmat(mu', m, 1)).^2))';%repmat 将mu‘(1,n) 复制m行1列,这样跟X(m,n)可以做减法运算;
%另一种计算方式
%mu = sum(X)' / m;
%temp = X' - repmat(mu, 1, m);
%sigma2 = sum(temp.^2,2) / m;
% =============================================================
end
根据计算的高斯参数绘制概率等高线:
2. 计算交叉验证数据的F1 score评估,来获取最优阈值epsilon
selectThreshold.m
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
%方法1 矩阵形式
cvPrediction = pval < epsilon;
tp = sum((cvPrediction == 1) & (yval == 1));
fp = sum((cvPrediction == 1) & (yval == 0));
fn = sum((cvPrediction == 0) & (yval == 1));
%方法2
%m = size(yval, 1);
%tp = 0;
%fp = 0;
%fn = 0;
%for i=1:m
% if(yval(i,1) == 1 && pval(i,1) < epsilon)
% tp = tp + 1;
% end
% if(yval(i,1) == 0 && pval(i,1) < epsilon)
% fp = fp +1;
% end
% if(yval(i,1) == 1 && pval(i,1) > epsilon)
% 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
3. 通过1、2获取的参数,可以预估新数据的数值;
最后,通过编写的函数计算多维的数据: