逻辑回归(logisitc regression)

1.逻辑分布

定义:连续变量X服从逻辑分布,分布函数表示为:

F(x)=11+e(θTx+b)(1) F ( x ) = 1 1 + e − ( θ T x + b ) ( 1 )
<script type="math/tex; mode=display" id="MathJax-Element-1">F(x)=\frac{1}{1+e^{-(θ^Tx+b)}}(1)</script>
分布函数图形为S形曲线,即为Sigmoid Logistic Function,表示为下图:
这里写图片描述
在实际应用中,F(X)函数表示为
F(x)=11+eθTx(2) F ( x ) = 1 1 + e − θ T x ( 2 )
<script type="math/tex; mode=display" id="MathJax-Element-2">F(x)=\frac{1}{1+e^{-θ^Tx}} (2)</script>

2.逻辑回归模型概念

a.假设有模型 P(Y=1|x)=F(x)=11+eθTx P ( Y = 1 | x ) = F ( x ) = 1 1 + e − θ T x <script type="math/tex" id="MathJax-Element-3">P(Y=1|x)=F(x)=\frac{1}{1+e^{-θ^Tx}}</script>在已知输入x的情况下,判断此输入为1类的概率是多少。
b.而在此概率模型中,若想求得概率P,只有参数 θT θ T <script type="math/tex" id="MathJax-Element-4">θ^T</script>不知道。
c.如何求得参数 θT θ T <script type="math/tex" id="MathJax-Element-5">θ^T</script>,就需要估计参数值。参数估计方法则采用在模型已知,参数未知的情况下的极大似然估计。
d.若采用极大似然估计方法来估计参数,那么就需要给出似然函数。在整个模型训练中,似然函数如何表示?问题转化成如何表达极大似然估计函数

3.极大似然估计

(1)训练样本
假设我们有5个训练样本,样本集为
{(x1,y1=1),(x2,y2=0),(x3,y3=1),(x4,y4=0),(x5,y5=1)} { ( x 1 , y 1 = 1 ) , ( x 2 , y 2 = 0 ) , ( x 3 , y 3 = 1 ) , ( x 4 , y 4 = 0 ) , ( x 5 , y 5 = 1 ) } <script type="math/tex" id="MathJax-Element-6">\{(x_1,y_1=1),(x_2,y_2=0),(x_3,y_3=1),(x_4,y_4=0),(x_5,y_5=1)\}</script>
则要满足5个样本的总的分布概率,有:
P=P(Y=1|x=x1)P(Y=0|x=x2)P(Y=1|x=x3)P(Y=0|x=x4)P(Y=1|x=x5) P = P ( Y = 1 | x = x 1 ) P ( Y = 0 | x = x 2 ) P ( Y = 1 | x = x 3 ) P ( Y = 0 | x = x 4 ) P ( Y = 1 | x = x 5 ) <script type="math/tex" id="MathJax-Element-7">P=P(Y=1|x=x_1)P(Y=0|x=x_2)P(Y=1|x=x_3)P(Y=0|x=x_4)P(Y=1|x=x_5)</script>
要计算P的值,从而确定极大似然函数。
(2)极大似然函数
a.极大似然估计求解步骤:

  • 写出似然函数;
  • 对似然函数求log
  • 对log函数求导数
  • 令导数等于0,求参数

b.设逻辑回归模型:

P(Y=1|x)=hθ(x) P ( Y = 1 | x ) = h θ ( x )
<script type="math/tex; mode=display" id="MathJax-Element-8">P(Y=1|x)=h_θ(x)</script>
P(Y=0|x)=1hθ(x) P ( Y = 0 | x ) = 1 − h θ ( x )
<script type="math/tex; mode=display" id="MathJax-Element-9">P(Y=0|x)=1-h_θ(x)</script>
则似然函数表示为:
L(θ)=i=1m[(hθ(xi)]yi[1hθ(xi)]1yi(3) L ( θ ) = ∏ i = 1 m [ ( h θ ( x i ) ] y i [ 1 − h θ ( x i ) ] 1 − y i ( 3 )
<script type="math/tex; mode=display" id="MathJax-Element-10">L(θ)=\prod_{i=1}^m[(h_θ(x_i)]^{y_i}[1-h_θ(x_i)]^{1-y_i}(3)</script>
对数似然函数为:
l(θ)=log(L(θ))=i=1m(yilog(hθ(xi)+(1yi)log(1hθ(xi)))(4) l ( θ ) = log ⁡ ( L ( θ ) ) = ∑ i = 1 m ( y i log ⁡ ( h θ ( x i ) + ( 1 − y i ) log ⁡ ( 1 − h θ ( x i ) ) ) ( 4 )
<script type="math/tex; mode=display" id="MathJax-Element-11">l(θ)=\log(L(θ))=\sum_{i=1}^m(y_i\log(h_θ(x_i)+(1-y_i)\log(1-h_θ(x_i)))(4)</script>
即要求得 l(θ) l ( θ ) <script type="math/tex" id="MathJax-Element-12">l(θ)</script>取得最大值时的θ的值。 问题转换成对极大似然函数的最优化问题。

4.代价函数与损失函数

代价函数 cost() c o s t ( ) <script type="math/tex" id="MathJax-Element-13">cost()</script>可以用对数似然函数公式(4)表示。
代价函数是用来寻找最优解的目标函数。
令代价函数 cost() c o s t ( ) <script type="math/tex" id="MathJax-Element-14">cost()</script>和损失函数 J(θ) J ( θ ) <script type="math/tex" id="MathJax-Element-15">J(θ)</script>表示为:
这里写图片描述
可以看出损失函数:

J(θ)=1ml(θ) J ( θ ) = − 1 m l ( θ )
<script type="math/tex; mode=display" id="MathJax-Element-16">J(θ)=-\frac{1}{m}l(θ)</script>
问题转换为使得损失函数最小时的参数θ的值。求损失函数极小值,采用梯度下降方法。
为了推导公式简单,在梯度下降求解之前,我们先介绍对数几率(log odds)或lofit函数

5.对数几率(log odds)

逻辑回归模型如下:

P(Y=1|x)=eθTx1+eθTx5 P ( Y = 1 | x ) = e θ T x 1 + e θ T x ( 5 )
<script type="math/tex; mode=display" id="MathJax-Element-17">P(Y=1|x)=\frac{e^{θ^Tx}}{1+e^{θ^Tx}}(5)</script>
P(Y=0|x)=11+eθTx6 P ( Y = 0 | x ) = 1 1 + e θ T x ( 6 )
<script type="math/tex; mode=display" id="MathJax-Element-18">P(Y=0|x)=\frac{1}{1+e^{θ^Tx}}(6)</script>
几率定义:一个事件的几率(odds)是指该事件发生的概率p与该事件不发生的概率1-p的比值 p1p p 1 − p <script type="math/tex" id="MathJax-Element-19">\frac{p}{1-p}</script>.
对数几率即对几率求对数 logit(p)=logp1p l o g i t ( p ) = log ⁡ p 1 − p <script type="math/tex" id="MathJax-Element-20">logit(p)=\log\frac{p}{1-p}</script>.
由公式(5)、(6)得
logP(Y=1|x)1P(Y=1|x)=θTx l o g P ( Y = 1 | x ) 1 − P ( Y = 1 | x ) = θ T x
<script type="math/tex; mode=display" id="MathJax-Element-21">log\frac{P(Y=1|x)}{1-P(Y=1|x)}=θ^Tx</script>
对数几率将在下面的推导中用到

6.梯度下降

由第4部分,得到损失函数 J(θ) J ( θ ) <script type="math/tex" id="MathJax-Element-22">J(θ)</script>

J(θ)=1mi=1m[yilog(hθ(xi)+(1yi)log(1hθ(xi))] J ( θ ) = − 1 m ∑ i = 1 m [ y i log ⁡ ( h θ ( x i ) + ( 1 − y i ) log ⁡ ( 1 − h θ ( x i ) ) ]
<script type="math/tex; mode=display" id="MathJax-Element-23">J(θ)=-\frac{1}{m}\sum_{i=1}^m[y_i\log(h_θ(x_i)+(1-y_i)\log(1-h_θ(x_i))]</script>
=1mi=1m[yiloghθ(xi)1hθ(xi)+log(1hθ(xi))] = − 1 m ∑ i = 1 m [ y i l o g h θ ( x i ) 1 − h θ ( x i ) + l o g ( 1 − h θ ( x i ) ) ]
<script type="math/tex; mode=display" id="MathJax-Element-24">=-\frac{1}{m}\sum_{i=1}^m[y_ilog\frac{h_θ(x_i)}{1-h_θ(x_i)}+log(1-h_θ(x_i))]</script>
=1mi=1m[yi(θTxi)log(1+eθTxi)] = − 1 m ∑ i = 1 m [ y i ( θ T x i ) − l o g ( 1 + e θ T x i ) ]
<script type="math/tex; mode=display" id="MathJax-Element-25">=-\frac{1}{m}\sum_{i=1}^m[y_i(θ^Tx_i)-log(1+e^{θ^Tx_i})] </script>
梯度函数:
J(θ)θ=1mi=1m[yixi11+eθxieθxi xi] ∂ J ( θ ) ∂ θ = − 1 m ∑ i = 1 m [ y i x i − 1 1 + e θ x i e θ x i   x i ]
<script type="math/tex; mode=display" id="MathJax-Element-26">\frac{∂J(θ)}{∂θ}=-\frac{1}{m}\sum_{i=1}^m[y_ix_i-\frac{1}{1+e^{θx_i}}e^{θx_i}~x_i]</script>
=1mi=1m(yihθ(xi))xi = − 1 m ∑ i = 1 m ( y i − h θ ( x i ) ) x i
<script type="math/tex; mode=display" id="MathJax-Element-27">=-\frac{1}{m}\sum_{i=1}^m(y_i-h_θ(x_i))x_i</script>
=1mi=1m(hθ(xi)yi)xi = 1 m ∑ i = 1 m ( h θ ( x i ) − y i ) x i
<script type="math/tex; mode=display" id="MathJax-Element-28">=\frac{1}{m}\sum_{i=1}^m(h_θ(x_i)-y_i)x_i</script>
梯度迭代:
Repeat{
θj:=θjαJ(θ)θj θ j := θ j − α ∂ J ( θ ) ∂ θ j
<script type="math/tex; mode=display" id="MathJax-Element-29">θ_j:=θ_j-\alpha\frac{∂J(θ)}{∂θ_j}</script>
}
最终找到极小值,从而确定 θT θ T <script type="math/tex" id="MathJax-Element-30">θ^T</script>

7.LR如何处理过拟合问题?

1.什么是过拟合?
就是训练出的样本可以很好的适应所有的训练样本,但是不能对测试样本很好的预测,这就是过拟合。
2.解决过拟合的方法有两个:
(1)降维,使用PCA降维,使使得模型 θ θ <script type="math/tex" id="MathJax-Element-48">\theta</script>个数减少,次数也降低,避免了过拟合
(2)正则化。增加正则化项
对于LR添加L1正则项或者L2正则项。

L1正则化会导致参数值变为0,但是L2却只会使得参数值减小,这是因为L1的导数是固定的,参数值每次的改变量是固定的,而L2会由于自己变小改变量也变小。

8.多分类问题

多分类逻辑回归的模型为:

P(Y=k|x)=eθx1+K1k=1eθx,k=1,2,...,K1. P ( Y = k | x ) = e θ x 1 + ∑ k = 1 K − 1 e θ x , k = 1 , 2 , . . . , K − 1.
<script type="math/tex; mode=display" id="MathJax-Element-33">P(Y=k|x)=\frac{e^{θx}}{1+\sum_{k=1}^{K-1}e^{θx}},k=1,2,...,K-1.</script>

9.总结

  1. 梯度下降法实现简单,但是梯度下降求得最优解不一定是全局最优解,有可能是局部最优解,并且收敛速度不一定是最快的。
  2. 逻辑回归源于线性回归
    线性函数=逻辑回归模型中Y=1的对数几率。

参考:
http://blog.csdn.net/chibangyuxun/article/details/53148005逻辑回归原理及过程
http://blog.csdn.net/raintungl/article/details/78188182中3.1logistic回归的极大似然估计
http://blog.csdn.net/walilk/article/details/50978864的导数与梯度下降

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐