机器学习第一话:初识逻辑回归

逻辑回归(Logistic regression)

什么是逻辑回归?

在我们以前的学习中,回归通常是指利用给定的样本信息 X ( x 1 , x 2 , . . . , x n ) X(x_1,x2,...,x_n) X(x1,x2,...,xn)建立一个回归表达式,例如:
y = β 0 x 1 + β 1 x 2 + . . . + β n x x y = \beta_0x1+\beta1x2+...+\beta_nx_x y=β0x1+β1x2+...+βnxx
最终我们得到是一个值 y ^ \hat y y^。对于预测某个样本是正例还是负例这种普通回归就不太适用,因为我们预测某个值是正例还是负例时,预测值 y ^ \hat y y^需要保证是在(0,1)范围内。
这时我们就需要使用逻辑回归。逻辑回归是一类经典的二分类算法,在使用机器学习算法时,我们一般使用简单的,若问题能得到很好的解决,那么就是用简单的,其次再考虑使用复杂的。

sigmoid函数

在正式介绍逻辑回归算法时,需要介绍一个辅助函数:sigmoid 函数。
公式: g ( z ) = 1 1 + e − z g(z) = \frac{1}{1+e^{-z}} g(z)=1+ez1.
其图像为:
sigmoid函数图像
函数值域为[0,1],可将任意值映射到[0,1]区间中,实现由值到概率的转换,即可以用来实现分类任务。
其导数为: d g ( z ) d z = g ( z ) × ( 1 − g ( z ) ) \frac{dg(z)}{dz} =g(z)\times (1-g(z)) dzdg(z)=g(z)×(1g(z))

逻辑回归参数求解

注:以下的 l o g ( x ) = l n ( x ) log(x) = ln(x) log(x)=ln(x)

似然函数

预测函数:
h θ ( X ) = g ( θ T X ) = 1 1 + e − θ T X h_\theta(X) =g(\theta^TX) = \frac{1}{1+e^{-\theta^TX}} hθ(X)=g(θTX)=1+eθTX1,
其中 θ T X = ∑ i = 0 n θ i x i = θ 0 + θ 1 x 1 + . . . + θ n x n \theta^TX = \sum\limits_{i = 0}^n\theta_ix_i =\theta_0 +\theta_1x_1+...+\theta_nx_n θTX=i=0nθixi=θ0+θ1x1+...+θnxn.

对于分类任务而言: h θ ( X ) h_\theta(X) hθ(X)就是样本为1的概率。那么:
P = P ( y = 1 ∣ X ; θ ) = h θ ( X ) P = P(y = 1|X;\theta) =h_\theta(X) P=P(y=1X;θ)=hθ(X)
1 − P = P ( y = 0 ∣ X ; θ ) = 1 − h θ ( X ) 1-P =P(y = 0|X;\theta) =1-h_\theta(X) 1P=P(y=0X;θ)=1hθ(X).
整合一下得到:
P ( y ∣ X ; θ ) = ( h θ ( X ) ) y ( 1 − h θ ( X ) ) 1 − y P(y|X;\theta)=(h_\theta(X))^y(1-h_\theta(X))^{1-y} P(yX;θ)=(hθ(X))y(1hθ(X))1y

根据极大似然估计思想,为了解得参数向量 θ \theta θ,需要使似然函数最大。其中,似然函数如下:
L ( θ ) = ∏ i = 1 m P ( y i ∣ X ; θ ) = ∏ i = 1 m ( h θ ( x i ) ) i y ( 1 − h θ ( x i ) ) 1 − y i L(\theta) =\prod\limits_{i=1}^mP(y_i|X;\theta)=\prod\limits_{i=1}^m(h_\theta(x_i))^y_i(1-h_\theta(x_i))^{1-y_i} L(θ)=i=1mP(yiX;θ)=i=1m(hθ(xi))iy(1hθ(xi))1yi.
对数似然函数如下:
l ( θ ) = l o g L ( θ ) = ∑ i = 1 m ( y i l o g h θ ( x i ) + ( 1 − y i ) l o g ( 1 − h θ ( x i ) ) ) l(\theta)=logL(\theta)=\sum\limits_{i=1}^m(y_ilogh_\theta(x_i)+(1-y_i)log(1-h_\theta(x_i))) l(θ)=logL(θ)=i=1m(yiloghθ(xi)+(1yi)log(1hθ(xi))).

参数更新

为了使用梯度下降法求解,引入 J ( θ ) = − 1 m l ( θ ) J(\theta)=-\frac{1}{m}l(\theta) J(θ)=m1l(θ).

θ j \theta_j θj求偏导:
δ J ( θ ) δ θ j = 1 m ∑ i = 1 m ( h θ ( x i ) − y i ) x i j \frac{\delta J(\theta)}{\delta\theta_j} =\frac{1}{m}\sum\limits_{i=1}^m(h_\theta(x_i)-y_i)x_{ij} δθjδJ(θ)=m1i=1m(hθ(xi)yi)xij
其中, x i j x_{ij} xij表示第i个样本的第j个特征。
那么参数更新过程为:
θ j = θ j − α 1 m ∑ i = 1 m ( h θ ( x i ) − y i ) x i j \theta_j=\theta_j-\alpha\frac{1}{m}\sum\limits_{i=1}^m(h_\theta(x_i)-y_i)x_{ij} θj=θjαm1i=1m(hθ(xi)yi)xij.

参数解释

逻辑回归相较于其他的机器学习方法,其优点在于其参数可以明确解释。
P P P表示样本为正例的概率,则 P 1 − P \frac{P}{1-P} 1PP为胜率,
已知 P = 1 1 + e − θ T X P = \frac{1}{1+e^{-\theta^TX}} P=1+eθTX1,那么:
P 1 − P = e θ 0 + θ 1 x 1 + . . . + θ n x n \frac{P}{1-P} =e^{\theta_0+\theta_1x_1+...+\theta_nx_n} 1PP=eθ0+θ1x1+...+θnxn,
x 1 x_1 x1变为 x 1 + 1 x_1+1 x1+1此时概率为 P ∗ P^* P,则有
P ∗ 1 − P ∗ = e θ 0 + θ 1 ( x 1 + 1 ) + . . . + θ n x n = e θ 0 + θ 1 x 1 + . . . + θ n x n × e θ 1 \begin{aligned} \frac{P^*}{1-P^*} &=e^{\theta_0+\theta_1(x_1+1)+...+\theta_nx_n}\\ &=e^{\theta_0+\theta_1x_1+...+\theta_nx_n}\times e^{\theta1} \end{aligned} 1PP=eθ0+θ1x1+1+...+θnxn=eθ0+θ1x1+...+θnxn×eθ1
即在其他 x x x保持不变时,若 x 1 x_1 x1增加一个单位,那么胜率就变为原来的 e θ 1 e^{\theta_1} eθ1倍。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值