虽然我喜欢Martijn对此的回答,比如乔治,但我想知道通过使用运行求和而不是sum()
一遍又一遍地对几乎相同的数字应用是否会更快。
None
在加速阶段将值作为默认值的想法也很有趣。事实上,对于移动平均线,可能有很多不同的场景。让我们将平均值的计算分为三个阶段:
- Ramp Up:开始迭代,其中当前迭代计数 < 窗口大小
- 稳步进展:我们有确切的窗口大小的元素可用于计算法线
average := sum(x[iteration_counter-window_size:iteration_counter])/window_size
- Ramp Down:在输入数据的末尾,我们可以返回另一个
window_size - 1
“平均”数字。
这是一个接受的函数
- 任意迭代(生成器很好)作为数据的输入
- 任意窗口大小 >= 1
- 在 Ramp Up/Down 阶段打开/关闭值生成的参数
- 这些阶段的回调函数以控制值的生成方式。这可用于不断提供默认值(例如
None
)或提供部分平均值
这是代码:
from collections import deque
def moving_averages(data, size, rampUp=True, rampDown=True):
"""Slide a window of <size> elements over <data> to calc an average
First and last <size-1> iterations when window is not yet completely
filled with data, or the window empties due to exhausted <data>, the
average is computed with just the available data (but still divided
by <size>).
Set rampUp/rampDown to False in order to not provide any values during
those start and end <size-1> iterations.
Set rampUp/rampDown to functions to provide arbitrary partial average
numbers during those phases. The callback will get the currently
available input data in a deque. Do not modify that data.
"""
d = deque()
running_sum = 0.0
data = iter(data)
# rampUp
for count in range(1, size):
try:
val = next(data)
except StopIteration:
break
running_sum += val
d.append(val)
#print("up: running sum:" + str(running_sum) + " count: " + str(count) + " deque: " + str(d))
if rampUp:
if callable(rampUp):
yield rampUp(d)
else:
yield running_sum / size
# steady
exhausted_early = True
for val in data:
exhausted_early = False
running_sum += val
#print("st: running sum:" + str(running_sum) + " deque: " + str(d))
yield running_sum / size
d.append(val)
running_sum -= d.popleft()
# rampDown
if rampDown:
if exhausted_early:
running_sum -= d.popleft()
for (count) in range(min(len(d), size-1), 0, -1):
#print("dn: running sum:" + str(running_sum) + " deque: " + str(d))
if callable(rampDown):
yield rampDown(d)
else:
yield running_sum / size
running_sum -= d.popleft()
它似乎比 Martijn 的版本要快一些——不过,后者要优雅得多。这是测试代码:
print("")
print("Timeit")
print("-" * 80)
from itertools import islice
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
# Martijn's version:
def moving_averages_SO(values, size):
for selection in window(values, size):
yield sum(selection) / size
import timeit
problems = [int(i) for i in (10, 100, 1000, 10000, 1e5, 1e6, 1e7)]
for problem_size in problems:
print("{:12s}".format(str(problem_size)), end="")
so = timeit.repeat("list(moving_averages_SO(range("+str(problem_size)+"), 5))", number=1*max(problems)//problem_size,
setup="from __main__ import moving_averages_SO")
print("{:12.3f} ".format(min(so)), end="")
my = timeit.repeat("list(moving_averages(range("+str(problem_size)+"), 5, False, False))", number=1*max(problems)//problem_size,
setup="from __main__ import moving_averages")
print("{:12.3f} ".format(min(my)), end="")
print("")
和输出:
Timeit
--------------------------------------------------------------------------------
10 7.242 7.656
100 5.816 5.500
1000 5.787 5.244
10000 5.782 5.180
100000 5.746 5.137
1000000 5.745 5.198
10000000 5.764 5.186
现在可以用这个函数调用来解决原来的问题:
print(list(moving_averages(range(1,11), 5,
rampUp=lambda _: None,
rampDown=False)))
输出:
[None, None, None, None, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]