【Machine Learning】【Andrew Ng】- 编程题(Week 9)

1、Estimate Gaussian Parameters
这里写图片描述
这里写图片描述

mu = 1/m*(sum(X))';
for i = 1:m
    sigma2 = sigma2+ 1/m*((X(i,:)')-mu).^2;
end

2、Select Threshold
这里写图片描述
这里写图片描述
• tp is the number of true negatives: the ground truth label says it’s an anomaly and our algorithm correctly classified it as an anomaly.
• fp is the number of false positives: the ground truth label says it’s not an anomaly, but our algorithm incorrectly classified it as an anomaly.
• fn is the number of false negatives: the ground truth label says it’s an anomaly, but our algorithm incorrectly classified it as not being anomalous.
第一步根据阈值先求出预测值,是0还是1
第二步画出performance矩阵,然后求F1

    cvPredictions = zeros(size(pval));
    for i=1:size(pval)
       if pval(i)<epsilon
           cvPredictions(i) = 1;
       end    
    end
    fp = sum((cvPredictions == 1)&(yval == 0));
    fn = sum((cvPredictions == 0)&(yval == 1));
    tp = sum((cvPredictions == 1)&(yval == 1)); 
    prec = tp/(tp+fp);
    rec = tp/(tp+fn);
    F1 = 2*prec*rec/(prec+rec);

3、Collaborative Filtering Cost
这里写图片描述

C0 = X*Theta'-Y;
C0 = C0.*R;
C = C0.^2;
J = 1/2*sum(sum(C));

4、Collaborative Filtering Gradient
这里写图片描述

C1 = C0*Theta;
X_grad = C1;
C2 = C0'*X;
Theta_grad = C2;

5、Regularized Cost
这里写图片描述

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

6、Gradient with regularization
这里写图片描述

C1 = C0*Theta;
X_grad = C1+lambda*X;
C2 = C0'*X;
Theta_grad = C2+lambda*Theta;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值