numpy中的二阶梯度 [英] Second order gradient in numpy

查看:59
本文介绍了numpy中的二阶梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用数值计算 numpy 中数组的二阶梯度.

I am trying to calculate the 2nd-order gradient numerically of an array in numpy.

a = np.sin(np.arange(0, 10, .01))
da = np.gradient(a)
dda = np.gradient(da)

这就是我想出来的.应该这样做吗?

This is what I come up. Is the the way it should be done?

我问这个,因为在 numpy 中没有选项说 np.gradient(a, order=2).我担心这种用法是否错误,这就是为什么 numpy 没有实现这一点.

I am asking this, because in numpy there isn't an option saying np.gradient(a, order=2). I am concerned about whether this usage is wrong, and that is why numpy does not have this implemented.

PS1:我确实意识到有 np.diff(a, 2).但这只是单方面的估计,所以我很好奇为什么 np.gradient 没有类似的关键字.

PS1: I do realize that there is np.diff(a, 2). But this is only single-sided estimation, so I was curious why np.gradient does not have a similar keyword.

PS2:np.sin() 是一个玩具数据 - 真实数据没有分析形式.

PS2: The np.sin() is a toy data - the real data does not have an analytic form.

谢谢!

推荐答案

数值梯度计算没有通用的正确答案.在计算样本数据的梯度之前,您必须对生成该数据的基础函数做出一些假设.您可以在技术上使用 np.diff 进行梯度计算.使用 np.gradient 是一种合理的方法.我看不出你所做的有什么根本性的错误——它是一维函数的二阶导数的一种特殊近似.

There's no universal right answer for numerical gradient calculation. Before you can calculate the gradient about sample data, you have to make some assumption about the underlying function that generated that data. You can technically use np.diff for gradient calculation. Using np.gradient is a reasonable approach. I don't see anything fundamentally wrong with what you are doing---it's one particular approximation of the 2nd derivative of a 1-D function.

这篇关于numpy中的二阶梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆