繁体   English   中英

基于矢量化的 numpy 特定操作

[英]numpy specific operation on a vectorized basis

我有一个关于商店在一天内每小时的潜在销售数字的数组。

test = np.array([1,2,1,5,3,6,10,0,0,3,2,3,0,0,7,3,6,2,0,0,3,5,4,6])

我的问题是-我们永远不会在销售额为0时开店。-我们每天只能开店两次(每次连续)

所以我们想要的是计算潜在开业期间的累计潜在销售额。

test = np.array([ 1, 3, 4, 9, 12, 18, 28, 0, 0, 3, 5, 8, 0, 0, 7, 10, 16, 18, 0, 0, 3, 8, 12, 18])

然后我们选择我们可以获得 28 美元的第一个时期,以及我们获得 18 美元的最后一个时期。 (如果存在具有相同潜在累积收益的时期,我们选择后者)。

所以我真正想要的是

test = np.array([1,2,1,5,3,6,10,0,0,0,0,0,0,0,0,0,0,0,0,0,3,5,4,6])

现在要关闭的时间是 0。

我可以通过这个来完成累积的潜在销售:但我不知道如何进行到最后一步。

import numpy as np

test = np.array([1,2,1,5,3,6,10,0,0,3,2,3,0,0,7,3,6,2,0,0,3,5,4,6])

sum = 0
cumulative = []
for item in test:
    if item != 0:
        sum += item
        cumulative.append(sum)
    else:
        sum = 0
        cumulative.append(0)

max_rev = sorted(cumulative)[-1:]
index_max = max([i for i, x in enumerate(cumulative) if x == max_rev])
from_max = index_max
while cumulative[from_max] != 0: from_max -= 1

next_max_rev = sorted(cumulative[:from_max] + [0]*(index_max-from_max) cumulative[index_max:])[-1:]

index_next_max = max([i for i, x in enumerate(cumulative) if x == next_max_rev])
from_next_max = index_next_max
while cumulative[from_next_max] != 0: from_next_max -= 1

for i, x in enumerate(test):
    if ( i > from_max and i <= index_max) or ( i > from_next_max and i <= index_next_max): continue
    else: test[i] = 0

print(test)

返回

[ 1  3  4  9 12 18 28  0  0  0  0  0  0  0  0  0  0  0  0  0  3  8 12 18]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM