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);
這題須要注意的是,矩陣每一個元素都要作運算。一開始我覺得矩陣只有1行或者1列,實際上z能夠是m×n。code
for m=1:size(z, 1) for n=1:size(z, 2) g(m, n) = 1 / (1 + exp(-z(m, n))); end; end;
J = -1 / m * (y' * log(sigmoid(X * theta)) + (1- y)' * log(1 - sigmoid(X * theta))); grad = 1/ m * X' * (sigmoid(X * theta) - y);
注意把p裏面的元素換成0和1
p = sigmoid(X * theta); p(p >= 0.5) = 1; p(p < 0.5) = 0;
io
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;