Cumulative sum of variable till a given percentile
import numpy as npa = np.array([15, 40, 124, 282, 914, 308])b = np.cumsum(a)p90 = np.percentile(a, 90)print b[b < p90][-1] #461
A=np.array(a)A[:(A<np.percentile(a, 90)).argmin()].sum() #461
@JoshAdel's
%%timeit ...: b = np.cumsum(a) ...: p90 = np.percentile(a, 90) ...: b[b < p90][-1] ...: 1000 loops, best of 3: 217 µs per loop
This:
%timeit A[:(A<np.percentile(a, 90)).argmin()].sum()10000 loops, best of 3: 191 µs per loop
None that I know of, but you can do this
import numpy as npfrom itertools import takewhilea = [15, 40, 124, 282, 914, 308]p90 = np.percentile(a,90)print sum(takewhile(lambda x : x < p90, a))
Out:
461