学习笔记-机器学习(2)-logistic回归

xiaoxiao2021-02-28  39

简介:logistics回归算法是一种分类方法,广泛用于医学等领域。

应用:1.对电子邮箱中的垃圾邮件和有用邮件分类;

          2.在线交易的真伪

          3.医学方面:判断肿瘤是良性还是恶性。

基本思想:1.寻找合适的假设函数,即分类函数,用以预测输入数据的判断结果;

                    2.构造代价函数,即损失函数,用以表示预测的输出结果与训练数据的实际类别之间的偏差;

                    3.最小化代价函数,从而获取最优的模型参数。

具体过程:

假设函数;

代价函数:J(θ)=1/m ∑Cost(h(x),y)

如果结果是越接近1,需要的代价就越多,即假设结果偏离正确的“0”越远。

简化代价函数:

梯度下降法求J(θ)的最小值

使用梯度下降法求J(θ)的最小值,θ的更新过程

  

其中:α学习步长。

即:

MATLAB模拟:

%%方法一、利用matlab自带的函数glmfit() function theta=logisticRegression() % logistic regression的参数theta,可以用matlab自带函数glmfit求出 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]'; theta = glmfit(x, [y ones(10,1)], 'binomial', 'link', 'logit') end %%方法二:使用梯度下降法迭代 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

theta =    -3.4932 

   2.9402

ans =    -3.4932     2.9402

参考:https://www.cnblogs.com/denny402/p/4032381.html

          andrew ng机器学习视频

转载请注明原文地址: https://www.6miu.com/read-2619432.html

最新回复(0)