[英]Calculating a moving average
I struggle to implement the moving average formula in my function. 我很难在我的函数中实现移动平均公式。 Took me quite a while to get where the code is right now.
花了我很长时间才得到代码现在的位置。
Is there a library I could probably take? 我可以带一个图书馆吗?
Input: 输入:
ma([2,3,4,3,2,6,9,3,2,1], 4)
Expected Output: 预期产出:
[None, None, None, 3.0, 3.0, 3.75, 5.0, 5.0, 5.0, 3.75]
My Output: 我的输出:
[None, None, 0.0, 3.0, 3.75, 5.0, 5.0, None, None, None]
I am running into the problem that the middle parts of my result is right but the rest is a mystery. 我遇到的问题是我的结果的中间部分是正确的,但其余部分是个谜。
def ma(prices, n): def ma(价格,n):
ma = [] sums = [] s = 0 ave = 0
for idx, i in enumerate(prices):
s += i
sums.append(s)
print('idx: ' + str(idx))
print('list of sums ' + str(sums))
#print('sum ' + str(s))
if i >= n+1:
print('sums[idx] ' + str(sums[idx]))
print('sums[idx-n] ' + str(sums[idx-n]))
ave = (sums[idx] - sums[idx-n]) / n
print('ave ' + str(ave))
ma.append(ave)
print('ma ' + str(ma))
else:
m = None
ma.append(m)
print('ma ' + str(ma))
(Sorry for all those print
function calls, but I really wanted to get to the source of the issue). (对不起,所有这些
print
函数调用,但我真的想找到问题的根源)。
There were several other logical errors in your code. 您的代码中还有其他几个逻辑错误。 I tried to correct it to make it work as you want.
我试图纠正它,使它按你想要的方式工作。 Following is only the modified version of the for loop.
以下仅是for循环的修改版本。 Rest stays the same.
休息保持不变。 The added/modified lines are highlighted by a comment
添加/修改的行由注释突出显示
for idx, i in enumerate(prices):
s += i
sums.append(s)
if idx == n-1: # Added
ave = (sums[idx]) / n # Added
ma.append(ave) # Added
elif idx >= n: # modified
ave = (sums[idx] - sums[idx-n]) / n
ma.append(ave)
else:
ma.append(None) # removed extra variable m
The problem was that you were using the wrong variable as the index: 问题是您使用了错误的变量作为索引:
One major problem was that you were using 一个主要问题是你正在使用
if i >= n+1:
You should use: 你应该使用:
if idx >= n+1:
Moreover, I added an if statement to take care of the average of the first three elements. 此外,我添加了一个if语句来处理前三个元素的平均值。
Now 现在
moving_average([2,3,4,5,8,5,4,3,2,1], 3)
gives the following output (you can round off later): 给出以下输出(您可以稍后舍入):
[None, None, 3.0, 4.0, 5.666666666666667, 6.0, 5.666666666666667, 4.0, 3.0, 2.0]
If you are ok using the standard library, this might help. 如果您可以使用标准库,这可能会有所帮助。 What you really need is a sliding window over your iterator.
你真正需要的是在迭代器上的滑动窗口。 You can use this function for that (this was based on
grouper
from itertools recipes): 您可以使用此功能(这是基于来自itertools食谱的
grouper
):
from itertools import islice
def window(iterable, n=2):
# window('123', 2) --> '12' '23'
args = [islice(iterable, i, None) for i in range(n)]
return zip(*args)
For the average you can use statistics.mean
. 对于平均值,您可以使用
statistics.mean
。 The paddig part can be simply achieved by adding the average list with [None] * (n - 1)
: paddig部分可以通过添加
[None] * (n - 1)
的平均列表来简单地实现:
from statistics import mean
def moving_average(prices, n):
avgs = [mean(w) for w in window(prices, n)]
padding = [None] * (n - 1)
return padding + avgs
Sample usage: 样品用法:
>>> moving_average([2,3,4,5,8,5,4,3,2,1], 3)
[None, None, 3, 4, 5.666666666666667, 6, 5.666666666666667, 4, 3, 2]
>>> moving_average([1, 2, 3], 3)
[None, None, 2]
>>> moving_average([1, 2, 3], 1)
[1, 2, 3]
>>> moving_average([5, 10, 0], 2)
[None, 7.5, 5]
The reason why your program returned that 9-9 / 3 = 0
is negative indexing. 你的程序返回
9-9 / 3 = 0
是负索引。 When idx
is 2
, sums[idx-n]
is saying sums[-1]
, which points at the last item of the list, 9
. 当
idx
为2
, sums[idx-n]
表示sums[-1]
,它指向列表的最后一项, 9
。 Understanding Python's slice notation could help explain that. 理解Python的切片表示法可以帮助解释这一点。
You could also solve this using list slicing to partition your input list smartly and calculate the avg over the list-partitions: 您还可以使用列表切片来解决此问题,以便巧妙地对输入列表进行分区并计算列表分区上的平均值:
def moving_average(data,window):
"""The partitions begin with window-1 None. Then follow partial lists, containing
window-sized elements. We do this only up to len(data)-window+1 as the following
partitions would have less then window elements."""
parts = [None]*(window-1) + [ data[i:i+window] for i in range(len(data)-window+1)]
# The None's The sliding window of window elements
# we return None if the value is None else we calc the avg
return [ sum(x)/window if x else None for x in parts]
print( moving_average([2,3,4,5,8,5,4,3,2,1], 1) )
print( moving_average([2,3,4,5,8,5,4,3,2,1], 2) )
print( moving_average([2,3,4,5,8,5,4,3,2,1], 3) )
Output ( parts
included as comment): 输出(包括注释的
parts
):
# [[2], [3], [4], [5], [8], [5], [4], [3], [2], [1]]
[2.0, 3.0, 4.0, 5.0, 8.0, 5.0, 4.0, 3.0, 2.0, 1.0]
# [None, [2, 3], [3, 4], [4, 5], [5, 8], [8, 5], [5, 4], [4, 3], [3, 2], [2, 1]]
[None, 2.5, 3.5, 4.5, 6.5, 6.5, 4.5, 3.5, 2.5, 1.5]
# [None, None, [2, 3, 4], [3, 4, 5], [4, 5, 8], [5, 8, 5], [8, 5, 4],
# [5, 4, 3], [4, 3, 2], [3, 2, 1]]
[None, None, 3.0, 4.0, 5.666666666666667, 6.0, 5.666666666666667, 4.0, 3.0, 2.0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.