均线或均线 [英] Moving average or running mean
本文介绍了均线或均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在给定特定窗口的情况下,是否存在用于Python的SciPy函数或NumPy函数或模块来计算一维数组的运行平均值?
Is there a SciPy function or NumPy function or module for Python that calculates the running mean of a 1D array given a specific window?
推荐答案
对于一个简短,快速的解决方案,它可以在一个循环中完成所有操作,而无需依赖项,下面的代码效果很好.
For a short, fast solution that does the whole thing in one loop, without dependencies, the code below works great.
mylist = [1, 2, 3, 4, 5, 6, 7]
N = 3
cumsum, moving_aves = [0], []
for i, x in enumerate(mylist, 1):
cumsum.append(cumsum[i-1] + x)
if i>=N:
moving_ave = (cumsum[i] - cumsum[i-N])/N
#can do stuff with moving_ave here
moving_aves.append(moving_ave)
这篇关于均线或均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文