Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
21 lines (21 sloc) 688 Bytes
function [cost, gradient] = cost_logistic(a, x, y)
cost = 0;
gradient = 0;
t = a(1)+a(2).*x;
sigma = 1./(1+exp(-t));
cost = sum(-y.*log(sigma)- (1-y).*log(1-sigma));
costFun = @ (a) sum(-y.*log((1./(1+exp(-(a(1)+a(2).*x)))))-(1-y).*log(1-(1./(1+exp(-(a(1)+a(2).*x))))));
gradient = (1/length(x))*sum((sigma-y).*t);
ai = [0 0];
% Set options for fminunc
options = optimset('GradObj','on','MaxIter',400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = fminunc(costFun, ai);
t = theta(1)+theta(2).*x;
sigma = 1./(1+exp(-t));
plot(x,y,'xb', x, sigma);
title('Regression')
xlabel('Temp (Degrees F)')
ylabel('Pass or Fail (1 or 0)')
end
You can’t perform that action at this time.