zoukankan      html  css  js  c++  java
  • 机器学习/逻辑回归(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: ',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。 这里写图片描述
  • 相关阅读:
    React篇-子组件调用父组件方法,并传值
    RN-ios模拟器上调出中文输入法
    mac-破解2018 webstorm
    React篇-滚动条下移的触发在react的生命周期分析
    javascript篇-typeof,instanceof,constructor,toString判断数据类型的用法和区别
    javascript篇-slice(),splice(),split(),substring(),substr()的用法以及区别
    javascript篇-console.log()打印object却显示为字符串[object object]
    Linux的几种关机命令
    深入浅出Oracle:DBA入门、进阶与诊断案例 PDF 下载
    SQL state [72000]; error code [1461]; ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 ; nested exception is java.sql.BatchUpdateException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
  • 原文地址:https://www.cnblogs.com/ldt-/p/10242208.html
Copyright © 2011-2022 走看看