python应用Axes3D绘图(批量梯度下降算法)


Posted in Python onMarch 25, 2020

本文实例为大家分享了python批量梯度下降算法的具体代码,供大家参考,具体内容如下

问题:

将拥有两个自变量的二阶函数绘制到空间坐标系中,并通过批量梯度下降算法找到并绘制其极值点

大体思路:

首先,根据题意确定目标函数:f(w1,w2) = w1^2 + w2^2 + 2 w1 w2 + 500
然后,针对w1,w2分别求偏导,编写主方法求极值点
而后,创建三维坐标系绘制函数图像以及其极值点即可

具体代码实现以及成像结果如下:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D

#f(w1,w2) = w1^2 + w2^2 + 2*w1*w2 + 500
def targetFunction(W): #目标函数
 w1,w2 = W
 return w1 ** 2 + w2**2 + 2*w1*w2+500

def gradientFunction(W): #梯度函数:分别对w1,w2求偏导
 w1,w2 = W
 w1_grad = 2*w1+2*w2
 w2_grad = 2*w2 + 2*w1
 return np.array([w1_grad,w2_grad])

def batch_gradient_distance(targetFunc,gradientFunc,init_W,learning_rate = 0.01,tolerance = 0.0000001): #核心算法
 W = init_W
 target_value = targetFunc(W)
 counts = 0 #用于计算次数
 while counts<5000:
 gradient = gradientFunc(W)
 next_W = W-gradient*learning_rate
 next_target_value = targetFunc(next_W)
 if abs(next_target_value-target_value) <tolerance:
 print("此结果经过了", counts, "次循环")
 return next_W
 else:
 W,target_value = next_W,next_target_value
 counts += 1
 else:
 print("没有取到极值点")


if __name__ == '__main__':
 np.random.seed(0) #保证每次运行随机出来的结果一致
 init_W = np.array([np.random.random(),np.random.random()]) #随机初始的w1,w2
 w1,w2 = batch_gradient_distance(targetFunction,gradientFunction,init_W)
 print(w1,w2)
 #画图
 x1=np.arange(-10,11,1) #为了绘制函数的原图像
 x2=np.arange(-10,11,1)

 x1, x2 = np.meshgrid(x1, x2) # meshgrid :3D坐标系

 z=x1**2 + x2**2 + 2*x1*x2+500

 fig = plt.figure()
 ax = Axes3D(fig)
 ax.plot_surface(x1, x2, z) #绘制3D坐标系中的函数图像
 ax.scatter(w1,w2, targetFunction([w1,w2]), s=50, c='red') #绘制已经找到的极值点
 ax.legend() #使坐标系为网格状

 plt.show() #显示

函数以及其极值点成像如下(红点为极值点):

python应用Axes3D绘图(批量梯度下降算法)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python在Windows8下获取本机ip地址的方法
Mar 14 Python
python实现根据月份和日期得到星座的方法
Mar 27 Python
Python中的index()方法使用教程
May 18 Python
听歌识曲--用python实现一个音乐检索器的功能
Nov 15 Python
浅谈Python中的私有变量
Feb 28 Python
python利用微信公众号实现报警功能
Jun 10 Python
Python编程flask使用页面模版的方法
Dec 28 Python
Python 脚本实现淘宝准点秒杀功能
Nov 13 Python
Python:合并两个numpy矩阵的实现
Dec 02 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
Mar 09 Python
python递归函数求n的阶乘,优缺点及递归次数设置方式
Apr 02 Python
Java多线程实现四种方式原理详解
Jun 02 Python
2020新版本pycharm+anaconda+opencv+pyqt环境配置学习笔记,亲测可用
Mar 24 #Python
python实现梯度下降和逻辑回归
Mar 24 #Python
详解Python 实现 ZeroMQ 的三种基本工作模式
Mar 24 #Python
python使用梯度下降算法实现一个多线性回归
Mar 24 #Python
PyQt5+python3+pycharm开发环境配置教程
Mar 24 #Python
python实现最速下降法
Mar 24 #Python
python实现梯度法 python最速下降法
Mar 24 #Python
You might like
php之字符串变相相减的代码
2007/03/19 PHP
Excel数据导入Mysql数据库的实现代码
2008/06/05 PHP
PHP定时执行计划任务的多种方法小结
2011/12/19 PHP
网页的分页下标生成代码(PHP后端方法)
2016/02/03 PHP
利用PHP将图片转换成base64编码的实现方法
2016/09/13 PHP
JScript 脚本实现文件下载 一般用于下载木马
2009/10/29 Javascript
Jquery 滑入滑出效果实现代码
2010/03/27 Javascript
javscript对象原型的一些看法
2010/09/19 Javascript
js 调用父窗口的具体实现代码
2013/07/15 Javascript
JavaScript中实现最高效的数组乱序方法
2014/10/11 Javascript
做web开发 先学JavaScript
2014/12/12 Javascript
JS折半插入排序算法实例
2015/12/02 Javascript
js鼠标移动时禁止选中文字
2017/02/19 Javascript
微信小程序页面滑动屏幕加载数据效果
2020/11/16 Javascript
详解vuex状态管理模式
2018/11/01 Javascript
js设置鼠标悬停改变背景色实现详解
2019/06/26 Javascript
Vue 实现前端权限控制的示例代码
2019/07/09 Javascript
vue实现标签云效果的方法详解
2019/08/28 Javascript
如何使用原生Js实现随机点名详解
2021/01/06 Javascript
[46:42]DOTA2-DPC中国联赛正赛 Aster vs Magma BO3 第二场 3月5日
2021/03/11 DOTA
python自动化测试之从命令行运行测试用例with verbosity
2014/09/28 Python
在Python中使用matplotlib模块绘制数据图的示例
2015/05/04 Python
python与C互相调用的方法详解
2017/07/14 Python
python使用mysql的两种使用方式
2018/03/07 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
2019/07/18 Python
Python 使用 docopt 解析json参数文件过程讲解
2019/08/13 Python
Python any()函数的使用方法
2019/10/28 Python
python爬虫开发之Request模块从安装到详细使用方法与实例全解
2020/03/09 Python
全球领先的各类汽车配件零售商:Advance Auto Parts
2016/08/26 全球购物
澳洲国民品牌乡村路折扣店:Country Road & Trenery Outlet
2018/04/19 全球购物
司法局火灾防控方案
2014/06/05 职场文书
装饰技术负责人岗位职责
2015/04/13 职场文书
小学生运动会广播
2015/08/19 职场文书
JVM上高性能数据格式库包Apache Arrow入门和架构详解(Gkatziouras)
2021/05/26 Servers
Redis Cluster集群动态扩容的实现
2021/07/15 Redis
Python first-order-model实现让照片动起来
2022/06/25 Python