1 使用方法
statsmodels.tsa.seasonal.seasonal_decompose(
x,
model='additive',
filt=None,
period=None,
two_sided=True,
extrapolate_trend=0)
使用滑动平均的方法进行时间序列分解
注:这只是一个最为简单的分解方法
通过首先通过对数据应用卷积滤波器估计趋势来获得结果。 然后从序列中删除趋势,每个时期的去趋势序列的平均值是返回的季节性分量。
2 参数说明
x |
时间序列。 如果是 两维的,则单个Series应该在一列中。 x 必须包含 2 个完整的周期。 |
model |
{“additive”, “multiplicative”} 时间序列分解的类型(加和 or 求乘积) 参数名称缩写是允许的 |
filt |
过滤掉季节性分量的过滤系数。 滤波中使用的具体移动平均法(单边or两侧)由 two_sided确定。 (个人理解是,计算滑动平均时,滑动平均阶数内各点所乘的那个系数) |
period |
时间序列的周期。 如果 x 不是 pandas 对象或 x 的索引没有频率,则必须使用。 如果 x 是具有时间序列索引的 pandas 对象,则覆盖 x 的默认周期。 |
two_sided |
滤波中使用的移动平均法。 如果为 True(默认),则使用 filt 计算居中移动平均值。 如果为 False,则滤波器系数filt仅适用于过去的值。 |
extrapolate_trend |
如果设置为 > 0,考虑到这么多 (+1) 个最近点,移动平均(卷积)产生的趋势是在两端外推的线性最小二乘法(如果 two_lateral 为 False,则为单侧外推)。 如果设置为“freq”,则使用最近点。 设置此参数会导致趋势或残差组件中没有 NaN 值。 |
返回内容:有季节性,趋势和残差三个属性的对象
3 使用方法
假设我们现在有一个1*744维的ndarray:
import matplotlib.pyplot as plt
plt.figure(figsize=(30,5))
plt.plot(west_tensor[0,:])
现在我们使用seasonal_decompose:
import statsmodels.api as sm
res = sm.tsa.seasonal_decompose(west_tensor[0,:],
period=48,
model="add")
rcParams['figure.figsize'] = 30, 15
fig=res.plot()
fig.savefig('decompose_add.png')