加权累积和python

时间:2018-01-04 13:36:19

标签: python numpy

我有问题查找功能类似于累积总和我只想加权。所以使用外部for循环看起来像这样:

discount_rate = 0.95 
rewards = [0, 0, 10] # want to translate to: [9, 9.5, 10] (approximation)

reversed_rewards = [10, 0, 0]
new_rewards = [0] * len( rewards)

previus = 0
for index in range( len( rewards)):
     new_rewards[ index] = reversed_rewards[ index] + previus * discount_rate
     previus = new_rewards[ index]

print( list( reversed( new_rewards)))

但如果你有大量的奖励阵列,这是一种慢速版本。是否有任何现有功能可以更快地完成此操作?

1 个答案:

答案 0 :(得分:1)

注意:我使用Python 3.6.0

您可以尝试使用itertoolshttps://docs.python.org/3/library/itertools.html

itertools.accumulate函数显示可能比np.cumsum更快:https://stackoverflow.com/a/39534850/7175945

from itertools import accumulate

def weighted():
    discount_rate = 0.95 #your discount rate
    rewards = [0, 0, 10] # want to translate to: [9, 9.5, 10](approximation)
    reversed_rewards = rewards[::-1] #list reversal
    acc = list(accumulate(reversed_rewards, lambda x,y: x*discount_rate + y))
    return acc[::-1] 

print(weighted())

如果您真的不想使用numpy,我认为这应该是您正在寻找的内容,否则您已编写的内容也是可行的选择。