Numpy diff inverted operation?

醉酒当歌 提交于 2019-12-10 03:39:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!