在Python中np.diff()意味着out [n] = a [n + 1]-a [n]但我想要out [n] = a [n] -a [n-1]?

时间:2019-01-09 07:25:26

标签: python python-3.x

在numpy中: np.diff()out[n] = a[n+1] - a[n]

但是它的语法是什么 out[n]= a[n]-a[n-1]

x = np.array([1, 2, 4, 7, 0])
np.diff(x)

op- array([ 1, 2, 3, -7])

但是我想要输出       op- array([-1, -2, -3, 7])

我具有3D阵列形状(73,80,97),实际上是我想要降雨率但无法做到的WRF模型输出:

for k in range(1,72):
    rainrate[k,:,:]=rainnc[k,:,:]-rainnc[k-1,:,:]

或者如果我在做

np.diff(rainnc,0) #showing negative value

1 个答案:

答案 0 :(得分:0)

只需将结果乘以-1

In [1]: import numpy as np

In [2]: x = np.array([1, 2, 4, 7, 0])

In [3]: np.diff(x) * -1
Out[3]: array([-1, -2, -3,  7])