欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

吴恩达机器学习课程ex2:Logistic Regression

程序员文章站 2022-07-13 10:11:45
...
# plotData.m
function plotData(X, y)
figure; hold on;

pos = find(y==1);
neg = find(y==0);
plot(X(pos, 1), X(pos,2), 'b+', 'LineWidth',2,'MarkerSize',7);
plot(X(neg, 1), X(neg,2), 'ro', 'LineWidth',1,'MarkerSize',7,'MarkerFaceColor','r');
hold off;
end
# sigmoid.m
function g = sigmoid(z)

g = zeros(size(z));
g = 1./(1+exp(-z));
end
# costFunction.m
m = length(y);
J = 0;
grad = zeros(size(theta));

h_fuc = sigmoid(X * theta);
J = (-1/m) * ((y' * log(h_fuc)) + (ones(m,1) - y)' * log(ones(m,1) - h_fuc)); 
grad = (1/m) * X' * (h_fuc - y);
end
# costFunctionReg.m
m = length(y);
J = 0;
grad = zeros(size(theta));

h_fuc = sigmoid(X * theta);
J = (-1/m) * ((y' * log(h_fuc)) + (ones(m,1) - y)' * log(ones(m,1) - h_fuc))+ (theta(2:end)' * theta(2:end)) * lambda / (2*m);
grad = (1/m) * X' * (h_fuc - y);

for i=2:size(theta,1)
  grad(i) = grad(i) + lambda / m * theta(i);
endfor

end
# predict.m
function p = predict(theta, X)
m = size(X, 1);
p = zeros(m, 1);

prob = sigmoid(X * theta);
threshold = ones(m,1) .* 0.5;

for i = 1:m
  if(prob(i) >= threshold(i))
    p(i) = 1;
    endif
endfor

end