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
這題需要注意的是,矩陣每個(gè)元素都要做運(yùn)算。一開始我以為矩陣只有1行或者1列,實(shí)際上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里面的元素?fù)Q成0和1
p = sigmoid(X * theta);
p(p >= 0.5) = 1;
p(p < 0.5) = 0;
costFunctionReg.m
n = length(theta);
% 把theta(1)置為0,這樣之后運(yùn)算不能正則化theta(1)的時(shí)候斑胜,直接把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;