Last active
August 29, 2015 14:02
example application of using RunningStats class for running mean calcs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pylab as plt | |
# generate some data | |
x = np.random.rand(1000,1) | |
# get the class | |
import RunningStats | |
# get an instance of the class | |
rs = RunningStats.RunningStats() | |
# define a window size for the running average | |
window_size = 10 | |
from itertools import product | |
# use sliding window_nd from here: http://www.johnvinyard.com/blog/?p=268 | |
xs = sliding_window_nd(np.squeeze(x),window_size,1) | |
runav = [] | |
for j in xrange(len(xs)): | |
for k in xs[j]: | |
rs.Push(k) | |
runav.append(rs.Mean()) | |
rs.Clear() | |
i = np.linspace(1,len(x),len(runav)) | |
plt.plot(x); plt.plot(i,runav,'r'); | |
# compute using a larger window size | |
window_size = 50 | |
xs = sliding_window_nd(np.squeeze(x),window_size,1) | |
runav = [] | |
for j in xrange(len(xs)): | |
for k in xs[j]: | |
rs.Push(k) | |
runav.append(rs.Mean()) | |
rs.Clear() | |
i = np.linspace(1,len(x),len(runav)) | |
plt.plot(i,runav,'k'); | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment