Python 平滑时间序列数据 [英] Python Smooth Time Series Data

查看:43
本文介绍了Python 平滑时间序列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有一些unixtime数据,值:

I have some data in python that is unixtime, value:

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

时间不断地以一秒为单位.我如何减少这些数据,以便时间戳是每秒,但该值是周围 10 个值的平均值?

Time constantly steps by one second. How might I reduce this data so the timestamp is every second, but the value is the average of the surrounding 10 values?

更高级的滚动平均线也不错,但此数据已绘制成图表,因此主要是为了平滑图表.

Fancier rolling averages would be good too, but this data is graphed so it is mostly to smooth out the graph.

跟进(TSQL 滚动平均时间分组之后来到结论是尝试在 SQL 中执行此操作是一种痛苦的方式).

Follow up of ( TSQL Rolling Average of Time Groupings after coming to the conclusion that trying to do this in SQL is a route of pain).

推荐答案

使用 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]

我得到了似乎不错的结果(我不懂数学):

I get what seems to be good results with (Not that I understand the math):

   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)

这篇关于Python 平滑时间序列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆