前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用python做时间序列预测三:时间序列分解

用python做时间序列预测三:时间序列分解

作者头像
程序员一一涤生
发布2020-06-03 17:24:29
2.6K0
发布2020-06-03 17:24:29
举报

在初始概念篇中,我们简单提到了时间序列由趋势、周期性、季节性、误差构成,本文将介绍如何将时间序列的这些成分分解出来。分解的使用场景有很多,比如当我们需要计算该时间序列是否具有季节性,或者我们要去除该时间序列的趋势和季节性,让时间序列变得平稳时都会用到时间序列分解。

加法和乘法时间序列

时间序列的各个观测值可以是以上成分相加或相乘得到: Value = Trend + Seasonality + Error Value = Trend * Seasonality * Error

分解

下面的代码展示了如何用python从时间序列中分解出相应的成分:

代码语言:javascript
复制
from statsmodels.tsa.seasonal import seasonal_decompose
from dateutil.parser import parse

# Import Data
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date'], index_col='date')

# Multiplicative Decomposition 
result_mul = seasonal_decompose(df['value'], model='multiplicative', extrapolate_trend='freq')

# Additive Decomposition
result_add = seasonal_decompose(df['value'], model='additive', extrapolate_trend='freq')

# Plot
plt.rcParams.update({'figure.figsize': (10,10)})
result_mul.plot().suptitle('Multiplicative Decompose', fontsize=22)
result_add.plot().suptitle('Additive Decompose', fontsize=22)
plt.show()

# Extract the Components ----# Actual Values = Product of (Seasonal * Trend * Resid)
df_reconstructed = pd.concat([result_mul.seasonal, result_mul.trend, result_mul.resid, result_mul.observed], axis=1)
df_reconstructed.columns = ['seas', 'trend', 'resid', 'actual_values']
df_reconstructed.head()

对比上面的加法分解和乘法分解可以看到,加法分解的残差图中有一些季节性成分没有被分解出去,而乘法相对而言随机多了(越随机意味着留有的成分越少),所以对于当前时间序列来说,乘法分解更适合。

小结

时间序列分解不仅可以让我们更清晰的了解序列的特性,有时候人们还会用分解出的残差序列(误差)代替原始序列来做预测,因为原始时间序列一般是非平稳序列,而这个残差序列是平稳序列,有助于我们做出更好的预测,当然预测后的序列还要加回或乘回趋势成分和季节性成分,平稳序列的具体内容将在下一篇文章中介绍。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加法和乘法时间序列
  • 分解
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档