Skip to content
Advertisement

Numpy diff inverted operation?

Working with numpy.diff function, suppose this simple case:

JavaScript

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

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

JavaScript

Or with np.concatenate for the concatenation –

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement