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!

jparanich
  • 8,372
  • 4
  • 26
  • 34

1 Answers1

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

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • out of curiosity do you know how to truncate it at 100? – jparanich Jan 27 '19 at 18:33
  • @jparanich: 8.6 and 8.6000 are equivalent. But still you can control the number of digits after decimal while printing them. See [here](https://stackoverflow.com/questions/22222818/how-to-printing-numpy-array-with-3-decimal-places) – Sheldore Jan 27 '19 at 18:57
  • Sorry I should have been clearer. I meant truncate the cumulative rolling numbers at 100 so the sum does not exceed 100. – jparanich Jan 28 '19 at 19:06