编程作业(八)

作者: SmallRookie | 来源:发表于2017-10-26 20:22 被阅读55次

异常检测与推荐系统

异常检测

在本节练习中,你将实现一个异常检测算法用于检测服务器上的异常行为。特征变量为每台服务器的吞吐量(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;

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

相关文章

  • 编程作业(八)

    异常检测与推荐系统 异常检测 在本节练习中,你将实现一个异常检测算法用于检测服务器上的异常行为。特征变量为每台服务...

  • 第八章节正则化编程作业

    第八章节编程作业:Logistic Regression 需要自己编程的主要包括以下5个文件: plotData....

  • 编程作业

    题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少? 题目:企业发放的奖金根据利...

  • 编程作业

    由于现在没有电脑,变成作业完不成,事后我会补上!

  • 编程作业

    无标题文档 title{ } content{ } content-1{ }...

  • Network Adapter

    作业要求: 必做 1、编程获取网卡信息 选做 2、编程实现IPv4相关信息的设置  3、编程实现功能开关 本次作业...

  • DAY52

    网络编程作业终于搞定了@_@

  • Coursera 机器学习 编程作业答案

    Coursera Machine Learning 编程作业答案

  • (139)2022.1.21 星期五 晴转多云

    假期第八天 今天是新宝寒假编程课的最后一天,还没上够就结束了!新宝作业也完成了大半!周末再撵撵作业,下周开始带他预...

  • 两个月C语言的初学者,我利用easyx图形库所做的球球作战小游戏

    写简书是为了记录自己学习编程的历程,只是在这里贴一些简单的编程作业和想法。但发现即便只是关于编程的作业,感兴趣的人...

网友评论

    本文标题:编程作业(八)

    本文链接:https://www.haomeiwen.com/subject/jmbxpxtx.html