时间序列(三)滑动窗口

来源:互联网 发布:景观设计需要哪些软件 编辑:程序博客网 时间:2024/05/21 17:23

滑动窗口就是能够根据指定的单位长度来框住时间序列,从而计算框内的统计指标。相当于一个长度指定的滑块在刻度尺上面滑动,每滑动一个单位即可反馈滑块内的数据。

import matplotlib.pylabimport numpy as npimport pandas as pd

指定六百个数据的序列

df = pd.Series(np.random.randn(600), index = pd.date_range('7/1/2016', freq = 'D', periods = 600))

指定该序列一个单位长度为10的滑块

r = df.rolling(window = 10)

输出滑块内的平均值

#r.max, r.median, r.std, r.skew, r.sum, r.varprint(r.mean())

2016-07-01 NaN
2016-07-02 NaN
2016-07-03 NaN
2016-07-04 NaN
2016-07-05 NaN
2016-07-06 NaN
2016-07-07 NaN
2016-07-08 NaN
2016-07-09 NaN
2016-07-10 0.300133 该处为前10个值得平均值
2016-07-11 0.284780
2016-07-12 0.252831
2016-07-13 0.220699
2016-07-14 0.167137
2016-07-15 0.018593
2016-07-16 -0.061414
2016-07-17 -0.134593
2016-07-18 -0.153333
2016-07-19 -0.218928
2016-07-20 -0.169426
2016-07-21 -0.219747
2016-07-22 -0.181266
2016-07-23 -0.173674
2016-07-24 -0.130629
2016-07-25 -0.166730
2016-07-26 -0.233044
2016-07-27 -0.256642
2016-07-28 -0.280738
2016-07-29 -0.289893
2016-07-30 -0.379625

通过画图库来看原始序列与滑动窗口产生序列的关系图

import matplotlib.pyplot as pltplt.figure(figsize=(15, 5))df.plot(style='r--')df.rolling(window=10).mean().plot(style='b')

这里写图片描述

能看到线是在波动线的中心的

原创粉丝点击