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;