吴恩达机器学习-Week3-Logistic Regression-编程题

plotData.m

pos  = find(y==1);
neg = find(y==0);
plot(X(pos, 1), X(pos, 2),  'k+', 'LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2),  'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);

sigmoid.m

这题需要注意的是,矩阵每个元素都要做运算。一开始我以为矩阵只有1行或者1列,实际上z可以是m×n。

for m=1:size(z, 1)
    for n=1:size(z, 2)
        g(m, n) = 1 / (1 + exp(-z(m, n)));
    end;
end;

costFunction.m

J = -1 / m * (y' * log(sigmoid(X * theta)) + (1- y)' * log(1 - sigmoid(X * theta)));
grad = 1/ m * X' * (sigmoid(X * theta) - y);

predict.m

注意把p里面的元素换成0和1

p = sigmoid(X * theta);
p(p >= 0.5) = 1;
p(p < 0.5) = 0;

costFunctionReg.m

n = length(theta);
% 把theta(1)置为0,这样之后运算不能正则化theta(1)的时候,直接把theta_reg拿来用就好了
theta_reg = [0; theta(2:n)];
J = -1 / m * (y' * log(sigmoid(X * theta)) + (1 - y)' * log(1 - sigmoid(X * theta))) + lambda / (2*m)  * (theta_reg' * theta_reg);
grad = 1 / m * X' * (sigmoid(X * theta) - y) + lambda / m * theta_reg;

你可能感兴趣的:(吴恩达机器学习-Week3-Logistic Regression-编程题)