numpy diff倒置运算? [英] Numpy diff inverted operation?

查看:219
本文介绍了numpy diff倒置运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设使用 numpy.diff 函数这个简单的例子:

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])

如何轻松地将x恢复至原始比例不变?我想有些东西与 numpy.cumsum() .

How can I get easily x back to original scale not differenced? I suppose there is something with numpy.cumsum().

推荐答案

与第一个元素并置,然后使用

Concatenate with the first element and then use cumsum -

np.r_[x[0], x_diff].cumsum()

对于连接,我们也可以使用np.hstack,就像这样-

For concatenating, we can also use np.hstack, like so -

np.hstack((x[0], x_diff)).cumsum()

或使用np.concatenate进行串联-

np.concatenate(([x[0]], x_diff)).cumsum()

这篇关于numpy diff倒置运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆