Python平滑时间序列数据

标签 python time-series

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

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

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

更好的滚动平均值也不错,但此数据是图表化的,因此主要是为了平滑图表。

跟进( TSQL Rolling Average of Time Groupings 在得出结论,尝试在 SQL 中执行此操作是一种痛苦的途径)。

最佳答案

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

关于Python平滑时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5515720/

相关文章:

python - 模数和 FizzBu​​zz 的计算复杂度

python - 正则表达式多个相同模式/重复捕获无法正常工作,仅匹配第一个和最后一个

r - 如何使用data.table在日期范围内执行联接?

python - 下面的背包填充算法的实现有什么问题?

python - 如何将一维扁平化 MNIST Keras 转换为 LSTM 模型而不需要取消扁平化?

Python:如何获取调用函数的文件的绝对路径?

r - 在 R 中向量化时间序列集成生成器

使用layout()进行绘图的R代码在逐行执行时有效,但在封装在函数中时无效

algorithm - 我必须使用哪种机器学习算法来进行序列预测?

r - R:填写时间序列中的缺失日期?