python逻辑回归代码d()_机器学习/逻辑回归(logistic regression)/--附python代码

个人分类: 机器学习

本文为吴恩达《机器学习》课程的读书笔记,并用python实现。

前一篇讲了线性回归,这一篇讲逻辑回归,有了上一篇的基础,这一篇的内容会显得比较简单。

逻辑回归(logistic regression)虽然叫回归,但他做的事实际上是分类。这里我们讨论二元分类,即只分两类,y属于{0,1}。

选择如下的假设函数:

这里写图片描述

其中:

这里写图片描述

上式称为逻辑函数或S型函数,图像如下图:

这里写图片描述

可以看到,当z趋向正无穷,g(z)趋向1,当z趋向负无穷g(z)趋向0,即g(z)取值[0,1]。

同样,令 这里写图片描述 ,

现在我们要根据训练集,获取上面模型的最好参数 值。同样,可以通过最大似然函数的方法来求解。

假设:

这里写图片描述

合并上面两个式子:

这里写图片描述

假设m个训练样本是独立的,则似然函数:

这里写图片描述

同样,我们求其对数值以方便求解:

这里写图片描述

我们的目的是最大似然函数,即max l ,可以用梯度上升法:

这里写图片描述

下面我们先对g(z)进行函数求导(后面会用到):

这里写图片描述

则可以求得一个样本时的导数(第二步用到 ):

这里写图片描述

则增量梯度法(上一篇线性回归有介绍)有:

这里写图片描述

m个样本的批处理梯度法有:

这里写图片描述

python 代码:

##author:lijiayan

##data:2016/10/27

from numpy import *

import matplotlib.pyplot as plt

def loadData(filename):

data = loadtxt(filename)

x = data[:,0:2]

y = data[:,2:3]

return x,y

#the sigmoid function

def sigmoid(x):

return 1.0 / (1 + exp(-x))

#the cost function

def costfunction(y,h):

y = array(y)

h = array(h)

J = sum(y*log(h))+sum((1-y)*log(1-h))

return J

# the batch gradient descent algrithm

def gradescent(x,y):

m,n = shape(x) #m: number of training example; n: number of features

x = c_[ones(m),x] #add x0

x = mat(x) # to matrix

y = mat(y)

a = 0.002 # learning rate

maxcycle = 2000

theta = ones((n+1,1)) #initial theta

J = []

for i in range(maxcycle):

h = sigmoid(x*theta)

theta = theta + a * x.transpose()*(y-h)

cost = costfunction(y,h)

J.append(cost)

plt.plot(J)

plt.show()

return theta,cost

#the stochastic gradient descent (m should be large,if you want the result is good)

def stocGraddescent(x,y):

m,n = shape(x) #m: number of training example; n: number of features

x = c_[ones(m),x] #add x0

x = mat(x) # to matrix

y = mat(y)

a = 0.01 # learning rate

theta = ones((n+1,1)) #initial theta

J = []

for i in range(m):

h = sigmoid(x[i]*theta)

theta = theta + a * x[i].transpose()*(y[i]-h)

cost = costfunction(y,h)

J.append(cost)

plt.plot(J)

plt.show()

return theta,cost

#plot the decision boundary

def plotbestfit(x,y,theta):

plt.plot(x[:,0:1][where(y==1)],x[:,1:2][where(y==1)],'ro')

plt.plot(x[:,0:1][where(y!=1)],x[:,1:2][where(y!=1)],'bx')

x1= arange(-4,4,0.1)

x2 =(-float(theta[0])-float(theta[1])*x1) /float(theta[2])

plt.plot(x1,x2)

plt.xlabel('x1')

plt.ylabel(('x2'))

plt.show()

def classifyVector(inX,theta):

prob = sigmoid(sum(inX*theta))

print 'the probobility is:',prob

if prob > 0.5:

return 1.0

else:

return 0.0

if __name__=='__main__':

x,y = loadData("testSet.txt")

theta,cost = gradescent(x,y)

print 'theta:\n',theta

print 'J:',cost

X = [1,2,9]

print 'the new input:',X

h = classifyVector(X,theta)

print 'the predict y:',h

plotbestfit(x,y,theta)

这个是logL(似然函数对数值)的曲线图,有点cost function的意思,只不过cost function取最小值,这个是取最大值,平稳了不震荡、不发散,就说明算法正常运行:

这里写图片描述

这个是两个类的分类示意图:

这里写图片描述

这是最后的运算结果,给出了theta值,logL的最终值(最大值),以及新来一个输入X,模型给出的预测值。注意,输入是两个特征x1,x2,这边X=[1,2,9]是三个特征,其中有一个是x0=1。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值