2011-04-01 148 views
11

我有蟒蛇一些数据,是unixtime,值:Python的平稳时间序列数据

[(1301672429, 274), (1301672430, 302), (1301672431, 288)...] 

持续时间一秒几步之遥。我怎样才能减少这些数据,所以时间戳是每秒钟的,但是这个值是周围10个值的平均值?

Fancier滚动平均值也不错,但是这个数据是绘图的,所以它主要是平滑图形。

后续的(TSQL Rolling Average of Time Groupings后得出结论,试图在SQL中这样做是一种痛苦的途径)。

回答

14

使用http://www.scipy.org/Cookbook/SignalSmooth

import numpy 
def smooth(x,window_len=11,window='hanning'): 
     if x.ndim != 1: 
       raise ValueError, "smooth only accepts 1 dimension arrays." 
     if x.size < window_len: 
       raise ValueError, "Input vector needs to be bigger than window size." 
     if window_len<3: 
       return x 
     if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']: 
       raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'" 
     s=numpy.r_[2*x[0]-x[window_len-1::-1],x,2*x[-1]-x[-1:-window_len:-1]] 
     if window == 'flat': #moving average 
       w=numpy.ones(window_len,'d') 
     else: 
       w=eval('numpy.'+window+'(window_len)') 
     y=numpy.convolve(w/w.sum(),s,mode='same') 
     return y[window_len:-window_len+1] 

我得到什么似乎是(不,我明白了良好的效果数学):

if form_results['smooth']: 
      a = numpy.array([x[1] for x in results]) 
      smoothed = smooth(a,window_len=21) 
      results = zip([x[0] for x in results], smoothed) 
+2

这似乎是合理的。如果你想要的意思是那么你的窗户应该'平坦'。其他开窗协议以不同的方式加权窗口中的数据点。 – JoshAdel 2011-04-01 17:00:37