【发布时间】:2021-06-08 01:18:47
【问题描述】:
下面的代码正是我想要的;但是,for 循环太慢了。在我的机器上,for 循环的挂墙时间是 1 分 5 秒。我正在寻找一个更快的 for 循环替代方案。
# Imports
from sympy.solvers.solveset import solveset_real
from sympy import Symbol, Eq
# Define variables
initial_value = 1
rate = Symbol('r')
decay_obs_window = 1480346
target_decay = .15
# Solver to calculate decay rate
decay_rate = solveset_real(Eq((initial_value - rate * decay_obs_window), target_decay), rate).args[0]
# Generate weights
weights = []
for i in range(5723673):
# How to handle data BEYOND decay_obs_window
if i > decay_obs_window and target_decay == 0:
# Record a weight of zero
weights.append(0)
elif i > decay_obs_window and target_decay > 0:
# Record the final target weight
weights.append(decayed_weight)
# How to handle data WITHIN decay_obs_window
else:
# Calculate the new slightly decayed weight
decayed_weight = 1 - (decay_rate * i)
weights.append(decayed_weight)
weights[0:10]
我写了这个列表理解,希望能缩短执行时间。虽然它工作得很好,但它并没有对 for 循环产生任何明显的运行时改进????:
weights = [0 if i > decay_obs_window and target_decay == 0 else decayed_weight if i > decay_obs_window and target_decay > 0 else (decayed_weight := 1 - (decay_rate * i)) for i in range(len(weights_df))]
我对任何有助于加快这一进程的方法感兴趣。谢谢????!
最终解决方案:
这是我确定的最终解决方案。在我的机器上,执行整个事情的时间只有 425 毫秒。这是Aaron 提出的解决方案的略微修改版本。
import numpy as np
from sympy.solvers.solveset import solveset_real
from sympy import Symbol, Eq
# Define variables
initial_value = 1
rate = Symbol('r')
decay_obs_window = 1480346
target_decay = .15
# Instantiate weights array
weights = np.zeros(5723673)
# Solver to calculate decay rate
decay_rate = solveset_real(Eq((initial_value - rate * decay_obs_window), target_decay), rate).args[0]
# Fix a bug where numpy doesn't like sympy floats :(
decay_rate = float(decay_rate)
# How to weight observations WITHIN decay_obs_window
weights[:decay_obs_window + 1] = 1 - np.arange(decay_obs_window + 1) * decay_rate
# How to weight observations BEYOND decay_obs_window
weights[decay_obs_window + 1 : 5723673] = target_decay
weights
【问题讨论】:
-
您可以通过将其分成两个循环来删除大量重复测试:
for i in range(decay_obs_window):和for i in range(decay_obs_window, 5723673)
标签: python python-3.x loops for-loop