Logistic回归的牛顿法及DFP、BFGS拟牛顿法求解
牛顿法
1 # coding:utf-8 2 import matplotlib.pyplot as plt 3 import numpy as np 4 5 def dataN(length):#生成数据 6 x = np.ones(shape = (length,3)) 7 y = np.zeros(length) 8 for i in np.arange(0,length/100,0.02): 9 x[100*i][0]=1 10 x[100*i][1]=i 11 x[100*i][2]=i + 1 + np.random.uniform(0,1.2) 12 y[100*i]=1 13 x[100*i+1][0]=1 14 x[100*i+1][1]=i+0.01 15 x[100*i+1][2]=i+0.01 + np.random.uniform(0,1.2) 16 y[100*i+1]=0 17 return x,y 18 19 def sigmoid(x): #simoid 函数 20 return 1.0/(1+np.exp(-x)) 21 22 def DFP(x,y, iter):#DFP拟牛顿法 23 n = len(x[0]) 24 theta=np.ones((n,1)) 25 y=np.mat(y).T 26 Gk=np.eye(n,n) 27 grad_last = np.dot(x.T,sigmoid(np.dot(x,theta))-y) 28 cost=[] 29 for it in range(iter): 30 pk = -1 * Gk.dot(grad_last) 31 rate=alphA(x,y,theta,pk) 32 theta = theta + rate * pk 33 grad= np.dot(x.T,sigmoid(np.dot(x,theta))-y) 34 delta_k = rate * pk 35 y_k = (grad - grad_last) 36 Pk = delta_k.dot(delta_k.T) / (delta_k.T.dot(y_k)) 37 Qk= Gk.dot(y_k).dot(y_k.T).dot(Gk) / (y_k.T.dot(Gk).dot(y_k)) * (-1) 38 Gk += Pk + Qk 39 grad_last = grad 40 cost.append(np.sum(grad_last)) 41 return theta,cost 42 43 def BFGS(x,y, iter):#BFGS拟牛顿法 44 n = len(x[0]) 45 theta=np.ones((n,1)) 46 y=np.mat(y).T 47 Bk=np.eye(n,n) 48 grad_last = np.dot(x.T,sigmoid(np.dot(x,theta))-y) 49 cost=[] 50 for it in range(iter): 51 pk = -1 * np.linalg.solve(Bk, grad_last) 52 rate=alphA(x,y,theta,pk) 53 theta = theta + rate * pk 54 grad= np.dot(x.T,sigmoid(np.dot(x,theta))-y) 55 delta_k = rate * pk 56 y_k = (grad - grad_last) 57 Pk = y_k.dot(y_k.T) / (y_k.T.dot(delta_k)) 58 Qk= Bk.dot(delta_k).dot(delta_k.T).dot(Bk) / (delta_k.T.dot(Bk).dot(delta_k)) * (-1) 59 Bk += Pk + Qk 60 grad_last = grad 61 cost.append(np.sum(grad_last)) 62 return theta,cost 63 64 def alphA(x,y,theta,pk): #选取前20次迭代cost最小的alpha 65 c=float("inf") 66 t=theta 67 for k in range(1,200): 68 a=1.0/k**2 69 theta = t + a * pk 70 f= np.sum(np.dot(x.T,sigmoid(np.dot(x,theta))-y)) 71 if abs(f)>c: 72 break 73 c=abs(f) 74 alpha=a 75 return alpha 76 77 def newtonMethod(x,y, iter):#牛顿法 78 m = len(x) 79 n = len(x[0]) 80 theta = np.zeros(n) 81 cost=[] 82 for it in range(iter): 83 gradientSum = np.zeros(n) 84 hessianMatSum = np.zeros(shape = (n,n)) 85 for i in range(m): 86 hypothesis = sigmoid(np.dot(x[i], theta)) 87 loss =hypothesis-y[i] 88 gradient = loss*x[i] 89 gradientSum = gradientSum+gradient 90 hessian=[b*x[i]*(1-hypothesis)*hypothesis for b in x[i]] 91 hessianMatSum = np.add(hessianMatSum,hessian) 92 hessianMatInv = np.mat(hessianMatSum).I 93 for k in range(n): 94 theta[k] -= np.dot(hessianMatInv[k], gradientSum) 95 cost.append(np.sum(gradientSum)) 96 return theta,cost 97 98 def tesT(theta, x, y):#准确率 99 length=len(x) 100 count=0 101 for i in xrange(length): 102 predict = sigmoid(x[i, :] * np.reshape(theta,(3,1)))[0] > 0.5 103 if predict == bool(y[i]): 104 count+= 1 105 accuracy = float(count)/length 106 return accuracy 107 108 def showP(x,y,theta,cost,iter):#作图 109 plt.figure(1) 110 plt.plot(range(iter),cost) 111 plt.figure(2) 112 color=['or','ob'] 113 for i in xrange(length): 114 plt.plot(x[i, 1], x[i, 2],color[int(y[i])]) 115 plt.plot([0,length/100],[-theta[0],-theta[0]-theta[1]*length/100]/theta[2]) 116 plt.show() 117 length=200 118 iter=5 119 x,y=dataN(length) 120 121 theta,cost=BFGS(x,y,iter) 122 print theta #[[-18.93768161][-16.52178427][ 16.95779981]] 123 print tesT(theta, np.mat(x), y) #0.935 124 showP(x,y,theta.getA(),cost,iter) 125 126 theta,cost=DFP(x,y,iter) 127 print theta #[[-18.51841028][-16.17880599][ 16.59649161]] 128 print tesT(theta, np.mat(x), y) #0.935 129 showP(x,y,theta.getA(),cost,iter) 130 131 theta,cost=newtonMethod(x,y,iter) 132 print theta #[-14.49650536 -12.78692552 13.05843361] 133 print tesT(theta, np.mat(x), y) #0.935 134 showP(x,y,theta,cost,iter)
转载于:https://www.cnblogs.com/qw12/p/5656765.html
Logistic回归的牛顿法及DFP、BFGS拟牛顿法求解相关推荐
- Logistic回归与建模
文章目录 1 Logistic回归模型 1.1 模型的概念 1.2 模型的建立 1.3 参数的估计 1.4 模型的求解 1.5 模型的预测 1.6 拟合优度检验 1.7 计算预测正确率 1 Logis ...
- c语言dfp算法程序,拟牛顿法中的DFP算法和BFGS算法
注明:程序中调用的函数jintuifa.m golddiv.m我在之前的笔记中已贴出 DFP算法和BFGS算法不同在于H矩阵的修正公式不同 DFP算法 %拟牛顿法中DFP算法求解f = x1*x1+2 ...
- 06.Logistic回归与最大熵模型(学习笔记)
06.Logistic回归与最大熵模型 参考: 袁春老师<大数据机器学习公开课>:https://www.xuetangx.com/course/THU08091001026/103331 ...
- logistic回归 如何_第七章:利用Python实现Logistic回归分类模型
免责声明:本文是通过网络收集并结合自身学习等途径合法获取,仅作为学习交流使用,其版权归出版社或者原创作者所有,并不对涉及的版权问题负责.若原创作者或者出版社认为侵权,请联系及时联系,我将立即删除文章, ...
- 机器学习实战(五)——Logistic 回归
文章目录 Logistic 回归 5.2 基于最优化方法的最佳回归系数确定 5.2.1 梯度上升法 5.3 python实战 5.3.1 查看数据集分布情况 5.3.2 训练 5.3.3 绘制决策边界 ...
- logistic回归模型python_【机器学习速成宝典】模型篇03逻辑斯谛回归【Logistic回归】(Python版)...
目录 一元线性回归.多元线性回归.Logistic回归.广义线性回归.非线性回归的关系 什么是极大似然估计 逻辑斯谛回归(Logistic回归) 多类分类Logistic回归 Python代码(skl ...
- Python3《机器学习实战》学习笔记(七):Logistic回归实战篇之预测病马死亡率
转载请注明作者和出处: http://blog.csdn.net/c406495762 机器学习知乎专栏:https://zhuanlan.zhihu.com/ml-jack CSDN博客专栏:htt ...
- 对数线性模型(Logistic回归算法)
1.Logistic分布: logistic分布定义:设X是连续随机变量,X服从logistic分布,即为X具有下列分布函数和密度函数: 其中,mu为位置参数,r>0为形状参数: logisti ...
- Logistic回归损失函数推导
Logistic回归损失函数推导 前言 Logistic回归损失函数的极大似然推导:西瓜书公式3.27怎么推来的? Logistic回归损失函数的极大似然推导:西瓜书公式3.27怎么推来的? Logi ...
最新文章
- 编程中的一个易错点:判断某个点是否超出棋盘边界
- mysql 1594_【MySQL】复制1594错误(从库relaylog损坏)
- centos7 网络服务(二)Unbound实现dns高速缓存
- 在Eclipse中查看Javadoc文档
- iOS 获取self类型
- catia齿轮宏程序_Catia宏程序
- Mac终端shell类型bash和zsh切换
- 第十五期:详解Java集合框架,让你全面掌握!
- 11G数据库导入10G的操作实践
- 【物理女神】谁是中国第一位物理学女博士?
- mysql数据库同步xtrab_热备份的实现方式
- gdiplus画直线
- 谷歌浏览器32位安装包_谷歌团队新作!只需下载3M安装包,就能让你的手机浏览器跟踪眼球运动...
- 佛系宿华和他的“信任电商”伪命题
- Python学员信息管理系统
- §1.1自然数 上•序数理论
- 大学的c语言课程难度,大学挂科率最高的4门课程,学霸也担心挂科,有你学过的课程吗?...
- 对Aurora8b10b的简要理解
- 10个自学编程的学习网站和论坛,都是常去逛的干货网站社区
- 【23考研】计算机408数据结构代码题强化阶段划重点(王道书)
热门文章
- 递归过程中语句执行顺序
- 图像分类_01图像分类简介:挑战+近邻分类器+CIFAR-10数据集概述
- 怎么让车辆gps定位失效_如何更有效地检测车辆gps定位器?
- 论文阅读 - Video Swin Transformer
- LeetCode 2008. 出租车的最大盈利(DP)
- 使用GRU单元的RNN模型生成唐诗
- LeetCode 1256. 加密数字(bitset)
- LeetCode 865. 具有所有最深结点的最小子树(递归)
- python语言画心_python语言还是java如何用python画爱心
- 基坑监测日报模板_基坑监测有多重要?实录基坑坍塌过程,不亲身经历,不知道现场有多恐怖!...