问题
Working with numpy.diff function, suppose this simple case:
>>> x = np.array([1, 2, 4, 7, 0])
>>> x_diff = np.diff(x)
array([ 1, 2, 3, -7])
How can I get easily x back to original scale not differenced? I suppose there is something with numpy.cumsum().
回答1:
Concatenate with the first element and then use cumsum -
np.r_[x[0], x_diff].cumsum()
For concatenating, we can also use np.hstack
, like so -
np.hstack((x[0], x_diff)).cumsum()
Or with np.concatenate
for the concatenation -
np.concatenate(([x[0]], x_diff)).cumsum()
来源:https://stackoverflow.com/questions/43563241/numpy-diff-inverted-operation