2017-06-16 39 views
-1

我有数字数组索引:NumPy的:找到运行总计超过价值

>>> x 
array([20, 20, 1, 3, 13, 20, 25, 20, 10, 9, 20]) 

我想找个地方运行总计x的超过15的指数,总超过重置每次15.因此,对于上面的例子中的数据,也应该给:

>>> tot_indices(x) 
array([0, 1, 4, 5, 6, 7, 9, 10]) 

目前,我这样做:

cumulative_total = np.copy(x) 
ii = 0 
cum_tot = 0 
while ii < len(accum_spaces): 
    cum_tot += x[ii] 
    cumulative_total[ii] = cum_tot 
    if cum_tot >= 15: 
    cum_tot = 0 
    ii += 1 
indices = x[np.where(cum_tot >= 15)] 

这是有效的,但是NumPy有一个内置的方法来避免Python循环?

回答

0

你可以使用numpy.cumsum

cumulative_total = np.cumsum(x) 
indices = x[np.where(cumulative_total >= 15)] 
+0

'numpy.cumsum'不等同于我已经给了,并且不产生所需的输出的代码简化代码。 – Tom

+0

啊我看到了,对不起:/ – meetaig