0

I'm curious if there is a built in function to transform an array of values into a cumulative array of values.

Example:

input = np.asarray([0.000,1.500,2.100,5.000])

into

[0.000,1.500,3.600,8.600]

Thanks!

1 Answer 1

1

Use in-built cumsum from NumPy to get the cumulative sum of your array inputt as

inputt = np.asarray([0.000,1.500,2.100,5.000])
print (np.cumsum(inputt)) 

# [0.  1.5 3.6 8.6]

I renamed your array because input is already an in-built function in python to get the user input from the keyboard

Sign up to request clarification or add additional context in comments.

3 Comments

out of curiosity do you know how to truncate it at 100?
@jparanich: 8.6 and 8.6000 are equivalent. But still you can control the number of digits after decimal while printing them. See here
Sorry I should have been clearer. I meant truncate the cumulative rolling numbers at 100 so the sum does not exceed 100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.