Week9_3Program_Anomaly Detection and Recommender Systems編程解析
1. Anomaly detection分析
1.1 Estimate gaussian parameters 0 / 15
計算公式:
X=(300x2) K=3 centroids=(3x2) idx=(300x1)
在 estimateGaussian.m 中添加
mu = mean(X);
sigma2 = var(X,opt=1);
octave中mean函數的作用:
mean (x) = SUM_i x(i) / N
octave中的var函數的作用:
var (x) = 1/(N-1) SUM_i (x(i) - mean(x))^2
1.2 Select threshold 0 / 15
計算公式:
上述求p的過程己經在 multivariateGaussian.m 中完成了, 直接用就行,程序中是pval
下面是 selectThreshold.m中用到的公式:
上圖是lecture 11中的課件截圖
* 將距離u1最近的找出來,取個平均值作爲新的u1 *
predictions = (pval < epsilon);
truePositives = sum((predictions == 1) & (yval == 1));
falsePositives = sum((predictions == 1) & (yval == 0));
falseNegatives = sum((predictions == 0) & (yval == 1));
precision = truePositives / (truePositives + falsePositives);
recall = truePositives / (truePositives + falseNegatives);
F1 = (2 * precision * recall) / (precision + recall);
2. Recommender Systems
2.1 Collaborative filtering cost 0 / 20
計算公式:
X(5x3) (4x3) Y(5x4)
在 cofiCostFunc.m 中添加, 實現沒有正則化項的costFunction
error = (X*Theta'-Y) .* R;
J = (1/2)*sum(sum(error .^ 2));
2.2 Collaborative filtering gradient 0 / 30
計算公式:
# 下面兩行是2.1中添加的,計算costFunction
error = (X*Theta'-Y) .* R;
J = (1/2)*sum(sum(error .^ 2));
# 下面兩行是2.2中的,梯度下降計算
X_grad = error * Theta ;
Theta_grad = error' * X ;
2.3 Regularized cost 0 / 10
計算公式:
error = (X*Theta'-Y) .* R;
J_noReg = (1/2)*sum(sum(error .^ 2));
X_grad = error * Theta ;
Theta_grad = error' * X ;
# 下面實現正則化的costFunction
costRegLeft = lambda/2 * sum(sum(Theta.^2));
costRegRight = lambda/2 * sum(sum(X.^2));
Reg = costRegLeft + costRegRight;
J = J_noReg + Reg;
2.4 Gradient with regularization 0 / 10
計算公式:
# 下面是計算costFuncton,分兩步先計算不帶cost的J,再計算reg項
error = (X*Theta'-Y) .* R;
J_noReg = (1/2)*sum(sum(error .^ 2));
costRegLeft = lambda/2 * sum(sum(Theta.^2));
costRegRight = lambda/2 * sum(sum(X.^2));
Reg = costRegLeft + costRegRight;
J = J_noReg + Reg;
# 下面是計算grad,分兩步先計算不帶reg的grad,再計算reg
X_grad_noReg = error * Theta ;
Theta_grad_noReg = error' * X ;
X_grad = X_grad_noReg + lambda * X;
Theta_grad = Theta_grad_noReg + lambda * Theta;
3. 總結
1 Estimate gaussian parameters 0 / 15
2 Select threshold 0 / 15
3 Collaborative filtering cost 0 / 20
4 Collaborative filtering gradient 0 / 30
5 Regularized cost 0 / 10
6 Gradient with regularization 0 / 10