写在前面
Logistic回归涉及到高等数学,线性代数,概率论,优化问题。本文尽量以最简单易懂的叙述方式,以少讲公式原理,多讲形象化案例为原则,给读者讲懂Logistic回归。如对数学公式过敏,引发不适,后果自负。
Logistic回归原理与推导
Logistic回归中虽然有回归的字样,但该算法是一个分类算法,如图所示,有两类数据(红点和绿点)分布如下,如果需要对两类数据进行分类,我们可以通过一条直线进行划分(w0 * x0 + w1 * x1+w2 * x2)。当新的样本(x1,x2)需要预测时,带入直线函数中,函数值大于0,则为绿色样本(正样本),否则为红样本(负样本)。
推广到高维空间中,我们需要得到一个超平面(在二维是直线,在三维是平面,在n维是n-1的超平面)切分我们的样本数据,实际上也就是求该超平面的W参数,这很类似于回归,所以取名为Logistic回归。
![3629157-dc884c8bc6a2d8d3.jpg](https://i-blog.csdnimg.cn/blog_migrate/73f15267c96e7d9e6e74db49fa1f7372.webp?x-image-process=image/format,png)
![3629157-e4c884bb53fd499a.jpg](https://i-blog.csdnimg.cn/blog_migrate/ca7f7dd1ca72cf7e230642ecb27696df.webp?x-image-process=image/format,png)
sigmoid函数
当然,我们不直接使用z函数,我们需要把z值转换到区间[0-1]之间,转换的z值就是判断新样本属于正样本的概率大小。
我们使用sigmoid函数完成这个转换过程,公式如下。通过观察sigmoid函数图,如图所示,当z值大于0时,σ值大于0.5,当z值小于0时,σ值小于于0.5。利用sigmoid函数,使得Logistic回归本质上是一个基于条件概率的判别模型。
![3629157-cb9e236574da3b71.jpg](https://i-blog.csdnimg.cn/blog_migrate/a9174139aec83b4173581ab230473219.webp?x-image-process=image/format,png)
![3629157-b8a528ba22625503.png](https://i-blog.csdnimg.cn/blog_migrate/b28d20432220ce484d50daac9636c591.webp?x-image-process=image/format,png)
目标函数
其实,我们现在就是求W,如何求W呢,我们先看下图,我们都能看出第二个图的直线切分的最好,换句话说,能让这些样本点离直线越远越好,这样对于新样本的到来,也具有很好的划分,那如何用公式表示并计算这个目标函数呢?
![3629157-502f7422805a06a5.png](https://i-blog.csdnimg.cn/blog_migrate/5a88aafadc8c232c8bb66883337e1c27.webp?x-image-process=image/format,png)
我们把sigmoid公式应用到z函数中:
![3629157-8972821d0e78a774.jpg](https://i-blog.csdnimg.cn/blog_migrate/69c230e4574ea6b83a3ef33ac7225d74.webp?x-image-process=image/format,png)
通过条件概率可推出下面公式,对公式进行整合为一个,见下。
![3629157-6ee8ace0cfd13ece.jpg](https://i-blog.csdnimg.cn/blog_migrate/a5e4237ad46f06f97e8c439e19c68a3b.webp?x-image-process=image/format,png)
![3629157-3b8631e9e94aa83f.jpg](https://i-blog.csdnimg.cn/blog_migrate/37a8bbdcfcaccc97003962836cf3911e.webp?x-image-process=image/format,png)
假定样本与样本之间相互独立,那么整个样本集生成的概率即为所有样本生成概率的乘积:
![3629157-e82b5179c0e40fe7.jpg](https://i-blog.csdnimg.cn/blog_migrate/1123670f724f42e0a481637b59f81cd5.webp?x-image-process=image/format,png)
这个公式过于复杂,不太容易求导,这里通过log转换:
![3629157-b078f1e1060b7564.jpg](https://i-blog.csdnimg.cn/blog_migrate/b896591cac1658ccc45bf236817d4214.webp?x-image-process=image/format,png)
这时就需要这个目标函数的值最大,以此求出θ。
梯度上升法
在介绍梯度上升法之前,我们看一个中学知识:求下面函数在x等于多少时,取最大值。
![3629157-e7d36036abfb3c78.png](https://i-blog.csdnimg.cn/blog_migrate/775bdb58fa45bc36d766ad97e9c0d265.webp?x-image-process=image/format,png)
函数图:
![3629157-0f49900553bcd396.png](https://i-blog.csdnimg.cn/blog_migrate/1820da84ecbdf2e2006cfe9653b92cc3.webp?x-image-process=image/format,png)
解:求f(x)的导数:2x,令其为0,求得x=0时,取最大值为0。但在函数复杂时,求出导数也很难计算函数的极值,这时就需要使用梯度上升法,通过迭代,一步步逼近极值,公式如下,我们顺着导数的方向(梯度)一步步逼近。
![3629157-5b0d7578c50bfb47.jpg](https://i-blog.csdnimg.cn/blog_migrate/7f7a167d2ec0d5b95fcfe5b5dccffd71.webp?x-image-process=image/format,png)
利用梯度算法计算该函数的x值:
def f(x_old):
return -2*x_old
def cal():
x_old = 0
x_new = -6
eps = 0.01
presision = 0.00001
while abs(x_new-x_old)>presision:
x_old=x_new
x_new=x_old+eps*f(x_old)
return x_new
-0.0004892181072978443
目标函数求解
这里,我们对函数求偏导,得到迭代公式如下:
![3629157-8982a56269e0d52e.png](https://i-blog.csdnimg.cn/blog_migrate/05ec3794789a8603403bc636fb932df1.webp?x-image-process=image/format,png)
Logistic回归实践
数据情况
读入数据,并绘图显示:
def loadDataSet():
dataMat = [];labelMat = []
fr = open('数据/Logistic/TestSet.txt')
for line in fr.readlines():
lineArr = line.strip().split()
dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
labelMat.append(int(lineArr[2]))
return dataMat, labelMat
![3629157-15431532b3e72627.png](https://i-blog.csdnimg.cn/blog_migrate/5f02e030ea205d6a86a9d923f931d915.webp?x-image-process=image/format,png)
训练算法
利用梯度迭代公式,计算W:
def sigmoid(inX):
return 1.0/(1 + np.exp(-inX))
def gradAscent(dataMatIn, labelMatIn):
dataMatrix = np.mat(dataMatIn)
labelMat = np.mat(labelMatIn).transpose()
m,n = np.shape(dataMatrix)
alpha = 0.001
maxCycles = 500
weights = np.ones((n,1))
for k in range(maxCycles):
h = sigmoid(dataMatrix * weights)
error = labelMat - h
weights = weights + alpha * dataMatrix.transpose() * error
return weights
通过计算的weights绘图,查看分类结果:
![3629157-d9778f1cb1d77758.png](https://i-blog.csdnimg.cn/blog_migrate/335b30d35932cd09d3db046458e1e80e.webp?x-image-process=image/format,png)
算法优缺点
- 优点:易于理解和计算
- 缺点:精度不高
写在最后
最近在运营自己的原创公众号,以后文章会在公众号首发,希望各位读者多多关注支持。
万水千山总是情,点波关注行不行。
![3629157-8c29e186e3310144.png](https://i-blog.csdnimg.cn/blog_migrate/f8b123ab204709013cda09c9a7647b16.webp?x-image-process=image/format,png)