Working with numpy.diff function, suppose this simple case:
JavaScript
x
4
1
>>> x = np.array([1, 2, 4, 7, 0])
2
>>> x_diff = np.diff(x)
3
array([ 1, 2, 3, -7])
4
How can I get easily x back to original scale not differenced? I suppose there is something with numpy.cumsum().
Advertisement
Answer
Concatenate with the first element and then use cumsum
–
JavaScript
1
2
1
np.r_[x[0], x_diff].cumsum()
2
For concatenating, we can also use np.hstack
, like so –
JavaScript
1
2
1
np.hstack((x[0], x_diff)).cumsum()
2
Or with np.concatenate
for the concatenation –
JavaScript
1
2
1
np.concatenate(([x[0]], x_diff)).cumsum()
2