Issue
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?
Solution
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)
Answered By - Aikude
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.