首先我们要知道逻辑斯蒂回归是干什么用的,它可以用回归的方式找到分类边界曲线
1、sigmoid压缩函数:
g ( z ) = 1 1 + e − z g(z)= {1\over {1+e^{-z} }} g(z)=1+e−z1
在该函数中,z大于0,该函数值大于0.5,z<0,该函数值小于0.5,根据z和0.5的关系,可以实现分类。
函数范围(0-1)
例1:
h θ ( x ) = g ( θ 0 + θ 1 x 1 + θ 2 x 2 ) h_\theta(x) =g(\theta_0 +\theta1x1 +\theta2x2) hθ(x)=g(θ0+θ1x1+θ2x2)
g ( z ) = 1 1 + e − z g(z)= {1\over {1+e^{-z} }} g(z)=1+e−z1
取
θ 0 = − 3 , θ 1 = 1 , θ 2 = 1 \theta_0=-3, \theta1=1, \theta2=1 θ0=−3,θ1=1,θ2=1
得到分类函数:
− 3 + x 1 + x 2 = 0 -3 +x1 +x2=0 −3+x1+x2=0
例二:
h θ ( x ) = g ( θ 0 + θ 1 x 1 + θ 2 x 2 + θ 3 x 1 2 + θ 4 x 2 2 ) h_\theta(x) =g(\theta_0 +\theta1x_1 +\theta2x_2+\theta3x_1^2+\theta4x_2^2) hθ(x)=g(θ0+θ1x1+θ2x2+θ3x12+θ4x22)
取
θ 0 = − 1 , θ 1 = 0 , θ 2 = 0 , θ 3 = 1 , θ 4 = 1 \theta0=-1, \theta1=0,\theta2=0, \theta3=1,\theta4=1 θ0=−1,θ1=0,θ2=0,θ3=1,θ4=1
得到分类曲线:
x 1 2 + x 2 2 − 1 = 0 x_1^2+x_2^2-1=0 x12+x22−1=0
2、损失函数
如果还是用平方损失函数,容易陷入局部极小值,且体现不出来分类正确或错误的奖励或惩罚,所以要构建一个新的损失函数:
C o s t ( h θ ( x ) , y ) = − l o g ( h θ ( x ) ) , y = 1 − l o g ( 1 − h θ ( x ) ) , y = 0 Cost(h_\theta(x),y)=-log(h_\theta(x)) ,y=1 \\ -log(1-h_\theta(x)),y=0 Cost(hθ(x),y)=−log(hθ(x)),y=1−log(1−hθ(x)),y=0
画出图像:
转换成一个公式:
C o s t ( h θ ( x ) , y ) = − 1 m ∑ i = 1 m [ y i l o g ( h θ ( x i ) ) + ( 1 − y i ) l o g ( 1 − h θ ( x i ) ) ] Cost(h_\theta(x),y)=-\frac{1}{m}\sum_{i=1}^{m}[y_ilog(h_\theta(x_i))+(1-y_i)log(1-h_\theta(x_i))] Cost(hθ(x),y)=−m1i=1∑m[yilog(hθ(xi))+(1−yi)log(1−hθ(xi))]
防止过拟合,加入正则化项:
J ( θ ) = − 1 m ∑ i = 1 m [ y i l o g ( h θ ( x i ) ) + ( 1 − y i ) l o g ( 1 − h θ ( x i ) ) ] + λ 2 m ∑ j = 1 n θ j 2 J(\theta)=-\frac{1}{m}\sum_{i=1}^{m}[y_ilog(h_\theta(x_i))+(1-y_i)log(1-h_\theta(x_i))]+\frac{\lambda}{2m}\sum_{j=1}^{n}\theta_j^2 J(θ)=−m1i=1∑m[yilog(hθ(xi))+(1−yi)log(1−hθ(xi))]+2mλj=1∑nθj2