理解参考:https://blog.csdn.net/liuchengzimozigreat/article/details/84566650

以下实例

import numpy as np
import matplotlib.pyplot as plt def function(x):
return np.sum(x**2)#return x[0]**2 + x[1]**2 def _numerical_gradient_no_batch(f,x):
h = 1e-4
grad = np.zeros_like(x)
for idx in range(x.size):
tmp_va1 = x[idx]
x[idx] = float(tmp_va1) + h
fxh1 =f(x) x[idx] = tmp_va1 -h
fxh2 = f(x)
grad[idx] = (fxh1- fxh2)/(2*h) x[idx] = tmp_va1
print("grad:"+str(grad))
return grad def numerical_gradient(f,X):
if X.ndim == 1:
return _numerical_gradient_no_batch(f,X)
else:
grad = np.zeros_like(X)
for idx,x in enumerate(X):
grad[idx] = _numerical_gradient_no_batch(f,x)
return grad def function_2(x):
if x.ndim == 1:
return np.sum(x **2)
else:
return np.sum(x**2,axis=1) def tangent_line(f,x):
d = numerical_gradient(f,x)
print(d)
y = f(x) - d * x
return lambda t : d * t + y print(_numerical_gradient_no_batch(function_2,np.array([3.0,4.0])))
print(numerical_gradient(function_2,np.array([3.0,4.0])))
print(numerical_gradient(function_2,np.array([[3.0,4.0],[0.0,2.0],[3.0,0.0]])))
if __name__ =='__main__':
x0= np.arange(-2,2.5,0.25)
x1=np.arange(-2,2.5,0.25)
X,Y= np.meshgrid(x0,x1)
X = X.flatten()
Y = Y.flatten() grad = numerical_gradient(function_2,np.array([X,Y])) plt.figure()
plt.quiver(X,Y,-grad[0],-grad[1],angles="xy",color="#666666")
plt.xlim([-2,2])
plt.ylim([-2,2])
plt.xlabel('x0')
plt.ylabel('x1')
plt.grid()
plt.legend()
plt.draw()
plt.show()

python matplotlib quiver——画箭头、风场的更多相关文章

  1. Python matplotlib pylab 画张图

    from pylab import * w1 = 1 w2 = 25 fs = 18 y = np.arange(-2,2,0.001) x = w1*y*log(y)-1.0/w2*exp(-(w2 ...

  2. Python - matplotlib 数据可视化

    在许多实际问题中,经常要对给出的数据进行可视化,便于观察. 今天专门针对Python中的数据可视化模块--matplotlib这块内容系统的整理,方便查找使用. 本文来自于对<利用python进 ...

  3. 转:使用 python Matplotlib 库 绘图 及 相关问题

     使用 python Matplotlib 库绘图      转:http://blog.csdn.net/daniel_ustc/article/details/9714163 Matplotlib ...

  4. 安装python Matplotlib 库

    转:使用 python Matplotlib 库 绘图 及 相关问题  使用 python Matplotlib 库绘图      转:http://blog.csdn.net/daniel_ustc ...

  5. 在matlab 画箭头

    [转载]在matlab 画箭头 原文地址:在matlab 画箭头作者:纯情小郎君 完整见链接http://www.mathworks.com/matlabcentral/fx_files/14056/ ...

  6. python matplotlib 中文显示参数设置

    python matplotlib 中文显示参数设置 方法一:每次编写代码时进行参数设置 #coding:utf-8import matplotlib.pyplot as pltplt.rcParam ...

  7. python matplotlib plot 数据中的中文无法正常显示的解决办法

    转发自:http://blog.csdn.net/laoyaotask/article/details/22117745?utm_source=tuicool python matplotlib pl ...

  8. python matplotlib画图产生的Type 3 fonts字体没有嵌入问题

    ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...

  9. 菱形实现气泡Bubble,菱形画箭头,菱形画三角形

    菱形实现气泡Bubble,菱形画箭头,菱形画三角形 >>>>>>>>>>>>>>>>>>&g ...

随机推荐

  1. ASP.NET Core 2 学习笔记(十四)Filters

    Filter是延续ASP.NET MVC的产物,同样保留了五种的Filter,分别是Authorization Filter.Resource Filter.Action Filter.Excepti ...

  2. python学习之老男孩python全栈第九期_数据库day004知识点总结 —— MySQL数据库day4

    复习: 1. MySQL:文件管理的软件 2. 三部分: - 服务端 - SQL语句 - 客户端 3. 客户端: - MySQL - navicat 4. 授权操作: - 用户操作 - 授权操作 5. ...

  3. 用pymysql操作数据库

    import pymysql # 打开数据库连接 connection = pymysql.connect(host='127.0.0.1', user='root', passwd=', db='s ...

  4. K先生

    每天多一点点的努力,不为别的,只为了日后能够多一些选择,选择云卷云舒的小日子,选择自己说了算的生活,选择自己喜欢的人. 加油,记K先生之名!

  5. 弹窗中使用jquery ui的autocomplete自动完成插件无效果 实际是被遮挡了

    在普通页面上使用jquery ui的autocomplete自动完成插件时正常显示提供选择的下拉框,但是放到弹窗中的时候就无法显示这个选择的下拉框,其它效果正常: 估计是被弹出窗遮挡了,网络搜索了jq ...

  6. 利用css3制作毛玻璃的效果

    忙里偷闲,最近又在看许多比较酷炫的效果.现在基于jquery的插件比较多,但是很多插件的兼容性不是太好,所以原生的才是王道.在日常当中,毛玻璃已经不常见了,那是一个很久远年代的东西了.诺,下面就是毛玻 ...

  7. ifconfig命令无法找到,提示bash: ifconfig: command not found

    问题就是题目那样,具体解决方法截图如下: 分析问题 1.whereis ifconfig 看一下这个命令在哪个目录下 2.echo $PATH 看一下该目录是否在路经下,注意lunux下是完全区分大小 ...

  8. css动画笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 解决提交项目到GitHub,报错Please tell me who you are的问题

    描述: 在第一次提交项目到GitHub时,出现了以下问题 看了一下错误原因:Run git config --global user.email "you@example.com" ...

  10. frame shiro 授权及原理简述

    shiro 授权模式 shiro采用的是rbac授权模式rbac,基于角色的权限管理,谁扮演什么角色,被允许做什么事情. shiro 授权流程 shiro 授权方式 1.编程式 通过写if/else授 ...