逻辑回归(logistic regression)原理理解+matlab实现

逻辑回归(logistic regression)原理理解+matlab实现_第1张图片
逻辑回归(logistic regression)原理理解+matlab实现_第2张图片

使用梯度下降法迭代:

function theta =logisticReg()
%   梯度下降法寻找最合适的theta,使得代价函数J最小
options=optimset('GradObj','on','MaxIter',100);
inittheta=[0 0]';
theta=fminunc(@costFunc,inittheta,options);
end

%%
function [J,gradient] = costFunc(theta)
x = [0.0 0.1 0.7 1.0 1.1 1.3 1.4 1.7 2.1 2.2]';
y = [0 0 1 0 0 0 1 1 1 1]'; 
m=size(x,1);
tmp=theta(1)+theta(2)*x;        %theta'x
hypothesis=1./(1+exp(-tmp));  %logistic function
delta=log(hypothesis+0.01).*y+(1-y).*log(1-hypothesis+0.01);       %加上0.01是为了防止x为0
J=-sum(delta)/m;
gradient(1)=sum(hypothesis-y)/m;  %x0=1;
gradient(2)=sum((hypothesis-y).*x)/m;       %theta=theta-a*gradient;  gradient=-J'(theta)
end

原理主要参数sigai博客讲解。
原理参考:
http://blog.csdn.net/ariessurfer/article/details/41310525
http://blog.csdn.net/abcjennifer/article/details/7716281

matlab实现参考:
http://www.cnblogs.com/denny402/p/4032381.html

你可能感兴趣的:(机器学习算法)