I have two numpy arrays prods andindex
prods = np.asarray([ 0.5 , 0.25, 1.98, 2.4 , 2.1 , 0.6 ])
index = np.asarray([False, True, True, False, False, True], dtype=bool)
I need to calculate the sum of the values in prods array using the index array. The output I want to is
res = [0.75, 1.98, 5.1]
The first True in index array is preceded by a False, so I take the first two elements from prods(.5,.25) and sum them up(0.75). The second True in index has no preceding False (since its preceded by a True, the False at position zero doesn't count), so I simply output 1.98 in this case. The third True is preceded by two False, so I take those values from prods array (2.4,2.1,0.6) and sum them up. Any ideas on how to do this?
I basically need something like np.cumsum but I need to return the cumulative sum every time a True occurs in index and reset the cumulative sum value to zero.