Home > Software engineering >  python: generate a list values with cumulative percent increments preferably using numpy
python: generate a list values with cumulative percent increments preferably using numpy

Time:10-13

I'd like to generate a list of values that are x% increment over base value. For example, if base value = 100, then generate additional values that are 5% increments over cumulative value.

The list would be [100, 105, 110.25, 115.75]

Is there a numpy routine I can use to do this? or any other quick way of doing this.

CodePudding user response:

One approach is to use np.cumprod

import numpy as np

increment = 1.05  # is .05 because the increment is 5%
start = 100 # the start value

arr = np.repeat(increment, 4)
arr[0] = start

res = arr.cumprod()
print(res)

Output

[100.     105.     110.25   115.7625]

An alternative for building the array arr is to do:

arr = np.array([100, *np.repeat(increment, 3)])
  • Related