函数计算numpy.gradient

时间:2018-03-29 13:23:45

标签: python numpy gradient

我有一个高斯,我使用numpy.gradient函数计算其渐变。我想在渐变中做一些细微的修改,然后获得稍微修改过的高斯。

Gaussian and its gradient

python中有没有可以计算梯度倒数的函数?

我已经检查了此链接Inverse of n-dimensional numpy.gradient但找不到解决方案。

1 个答案:

答案 0 :(得分:1)

1D中,以下代码段会反转np.gradient

>>> A = scipy.stats.norm().pdf(np.linspace(-1, 1, 19))
>>> A
array([0.24197072, 0.26874286, 0.29481487, 0.31944801, 0.34189229,
       0.36142383, 0.37738323, 0.38921247, 0.39648726, 0.39894228,
       0.39648726, 0.38921247, 0.37738323, 0.36142383, 0.34189229,
       0.31944801, 0.29481487, 0.26874286, 0.24197072])
>>> 
>>> a = np.gradient(A)
>>> 
>>> A[0] + 2 * np.c_[np.r_[0, a[1:-1:2].cumsum()], a[::2].cumsum() - a[0] / 2].ravel()[:len(a)]
array([0.24197072, 0.26874286, 0.29481487, 0.31944801, 0.34189229,
       0.36142383, 0.37738323, 0.38921247, 0.39648726, 0.39894228,
       0.39648726, 0.38921247, 0.37738323, 0.36142383, 0.34189229,
       0.31944801, 0.29481487, 0.26874286, 0.24197072])