如何从上一行中减去?

时间:2017-02-10 15:27:43

标签: python numpy

我有np.array存储在变量x中,如下所示:

array([[ 956],
       [ 929],
       [1083],
       [1074],
       [ 922]]

我想从前一个数字中减去每个数字,我想要一个新的变量y,如下所示:

array([[ -27],
       [ 154],
       [  -9],
       [-152]]

1 个答案:

答案 0 :(得分:2)

import numpy as np 

x = np.array([[ 956],
       [ 929],
       [1083],
       [1074],
       [ 922]] 

out = np.diff(out, axis=0)

Out: array([[ -27],
   [ 154],
   [  -9],
   [-152]]