当前位置: 代码迷 >> 综合 >> Coursera-MachineLearning-Week9编程题目整理
  详细解决方案

Coursera-MachineLearning-Week9编程题目整理

热度:116   发布时间:2023-11-11 06:58:12.0

estimateGaussian.m

mu = 1/m * sum(X);  %求解平均值
sigma2 = 1/m * sum((X - repmat(mu, m, 1)).^2);  %求解方差的平均值

selectThreshold.m

    %进行预测predictions = (pval < epsilon);%计算混淆矩阵fp = sum((predictions == 1) & (yval == 0));fn = sum((predictions == 0) & (yval == 1));tp = sum((predictions == 1) & (yval == 1));%计算查准率和召回率prec = tp/(tp+fp);rec = tp/(tp+fn);%计算F1值F1 = 2 * prec * rec / (prec + rec);

cofiCostFunc.m

temp = (X*Theta').*R;
%计算代价函数
J = sum( sum( (temp - Y.*R).^2) )/2.0 + (lambda/2) * ( sum(sum( X.^2 )) + sum(sum( Theta.^2 )) ); 
%计算正则化后的梯度
X_grad = (temp - Y.*R) * Theta + lambda * X;
Theta_grad = (temp - Y.*R)' * X + lambda * Theta;