1.logistic regression回归解决二分类
matlab 代码
% logistic regression 二元分类,使用 batch descent regression算法
%数据
x = linspace(1,20,20)
y = fliplr([0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1]);
%初始值
theta0 = 0.1;
theta1 = 0.1;
step = 0.001; %步长
count = 1
while 1
%(derative_theta0 ,derative_theta1)是梯度
derative_theta0 = 0;
derative_theta1 = 0;
m = length(x);
for i=1 : length(x)
derative_theta0 = derative_theta0 + (y(i) - 1 ./ (1 + exp( - (theta0+theta1*x(i)) ) )) *1
derative_theta1 = derative_theta1 + (y(i) - 1 ./ (1 + exp( - (theta0+theta1*x(i)) ) )) * x(i)
end
%梯度下降或去新的 theta0和theta1
theta0 = theta0 + step*derative_theta0;
theta1 = theta1 + step*derative_theta1;
error = (step*derative_theta0)^2 + (step*derative_theta1)^2;
%退出条件
if error < 0.00000003
f = 1./( 1+ exp(-(theta0 + theta1*x)))
func = sprintf('%d times: theta1=%f theta0=%f\n',count,theta1,theta0);
plot(x,y,'r+',x,f,'linewidth',2);
legend(func)
break;
end
count = count + 1
end
2.分类情况
y =fliplr([0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1]) 时候的的分类情况
y = [0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1]时候的的分类情况