编程作业(八)

异常检测与推荐系统

异常检测

在本节练习中,你将实现一个异常检测算法用于检测服务器上的异常行为。特征变量为每台服务器的吞吐量(mb/s)和延迟(ms)。当服务器运行时,你收集到了307个样本(即m=307),当然这些数据都是无标签数据。现在,你怀疑在这些数据中存在少量的异常数据,这些异常数据记录了服务器的异常操作。因此,你需要实现一个异常检测算法。

你将使用高斯模型用于检测数据集中的异常数据。与此同时,你将以可视化的2D数据集开始。

2D数据集

可视化数据集的代码为:

%% ================== Part 1: Load Example Dataset  ===================
%  We start this exercise by using a small dataset that is easy to
%  visualize.
%
%  Our example case consists of 2 network server statistics across
%  several machines: the latency and throughput of each machine.
%  This exercise will help us find possibly faulty (or very fast) machines.
%

fprintf('Visualizing example dataset for outlier detection.\n\n');

%  The following command loads the dataset. You should now have the
%  variables X, Xval, yval in your environment
load('ex8data1.mat');

%  Visualize the example dataset
plot(X(:, 1), X(:, 2), 'bx');
axis([0 30 0 30]);
xlabel('Latency (ms)');
ylabel('Throughput (mb/s)');

fprintf('Program paused. Press enter to continue.\n');
pause

任务一 高斯分布

高斯分布的数学表达式为:

计算出参数μ和σ^2^的值,使得高斯模型能够较好地拟合数据集。

你的任务为:在estimateGaussian.m文件中补充相关代码,使其能够计算出参数μ和 σ2 的值。

注:在Octave或者Matlab中,在使用var()函数计算方差σ2的值时,其默认除以(m-1),而不是除以m。

参考代码为:

mu = (mean(X))';
sigma2 = (var(X))' * (m -1) / m;

运行该部分代码,可得到如下结果:

任务二 选择ε值

现在你已经计算出了合理的高斯分布的参数,那么你可以基于交叉验证集选择阈值ε,从而确定哪些数据为异常数据。因此在本部分,你将通过计算出基于交叉验证集的F1值来选择合理的阈值ε。

F1值的数学表达式:

在计算F1值前,你需要先计算出查准率(Precision)和召回率(Recall),其计算公式为:

若对上述概念不清楚者,可查阅相关资料,也可查阅本人之前的文章——机器学习系统设计(二),谢谢!

综上,selectThreshold.m文件中的参考代码如下:

cvPredictions = pval < epsilon;
tp = sum((cvPredictions == 1) & (yval == 1));
fp = sum((cvPredictions == 1) & (yval == 0));
fn = sum((cvPredictions == 0) & (yval == 1));

prec = tp / (tp + fp + 1e-10);
rec = tp / (tp + fn + 1e-10);
F1 = 2 * prec * rec / (prec + rec + 1e-10);

运行本部分代码,可得到如下结果:

Best epsilon found using cross-validation: 8.990853e-005
Best F1 on Cross Validation Set:  0.875000
   (you should see a value epsilon of about 8.99e-05)
   (you should see a Best F1 value of  0.875000)

高维度的数据集

在此最后一部分,你将运行异常检测算法检测高维度的数据集。该部分代码如下:

%% ================== Part 4: Multidimensional Outliers ===================
%  We will now use the code from the previous part and apply it to a 
%  harder problem in which more features describe each datapoint and only 
%  some features indicate whether a point is an outlier.
%

%  Loads the second dataset. You should now have the
%  variables X, Xval, yval in your environment
load('ex8data2.mat');

%  Apply the same steps to the larger dataset
[mu sigma2] = estimateGaussian(X);

%  Training set 
p = multivariateGaussian(X, mu, sigma2);

%  Cross-validation set
pval = multivariateGaussian(Xval, mu, sigma2);

%  Find the best threshold
[epsilon F1] = selectThreshold(yval, pval);

fprintf('Best epsilon found using cross-validation: %e\n', epsilon);
fprintf('Best F1 on Cross Validation Set:  %f\n', F1);
fprintf('   (you should see a value epsilon of about 1.38e-18)\n');
fprintf('   (you should see a Best F1 value of 0.615385)\n');
fprintf('# Outliers found: %d\n\n', sum(p < epsilon));

运行结果如下:

Best epsilon found using cross-validation: 1.377229e-018
Best F1 on Cross Validation Set:  0.615385
   (you should see a value epsilon of about 1.38e-18)
   (you should see a Best F1 value of 0.615385)
# Outliers found: 117
推荐系统

在本节练习中,你将实现一个协同过滤算法,并将其应用于电影评分的数据集,其中评分的等级为1~5。该数据集中拥有943名用户(即nu = 943)和1682部电影(即nm = 1682)。

任务一 协同过滤算法

代价函数

协同过滤算法的代价函数为:

正则化后为:

协同过滤梯度

协同过滤算法的梯度为:

正则化后为:

因此,cofiCostFunc.m文件的参考代码为:

J_temp = (X * Theta' - Y) .^ 2;
J = sum(sum(J_temp .* R)) / 2 + lambda / 2 .* sum(sum(Theta .^ 2)) + lambda / 2 .* sum(sum(X .^ 2));

X_grad = ((X * Theta' - Y) .* R) * Theta + lambda .* X;
Theta_grad = ((X * Theta' - Y) .* R)' * X + lambda .* Theta;

注:本次练习需要提交部分已结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值