时间序列周期性特征
简介
在时间序列问题中,周期特征是异常重要的,例如:
上面这些在某些固定时间点周而复始的出现某种现象的,我们一般称之为周期性,那么在时间序列问题中,我们如何捕捉这些周期性呢?
此处我们介绍两种常见的周期性特征。
时间周期特征
01周期性indicators
周期性indicators一般就是表示时间序列周期性的一些二元特征。例如每周一我们的数据呈现出某种周期性,我们会加入是否为周一来表示该信息,例如下表所示。
02傅里叶特征
周期性indicators一般是我们有一些先验知识,但是还有一类数据,它的周期可能是每隔20天一次的周期,我们可以很容易的从图像中观测到,但是往往没法使用周期性indicators来捕捉此类信息。
此时我们可以使用傅里叶特征尝试捕捉此类到信息。
如果我们把这些sine和cosine的曲线加入到我们的训练集合中,往往可以取得不错的帮助,尤其是对于线性类的模型。
代码
01自己定义
import numpy as np
def fourier_features(index, freq, order):
time = np.arange(len(index), dtype=np.float32)
k = 2 * np.pi * (1 / freq) * time
features = {}
for i in range(1, order + 1):
features.update({
f"sin_{freq}_{i}": np.sin(i * k),
f"cos_{freq}_{i}": np.cos(i * k),
})
return pd.DataFrame(features, index=index)
# Compute Fourier features to the 4th order (8 new features) for a
# series y with daily observations and annual seasonality:
#
# fourier_features(y, freq=365.25, order=4)
02使用statsmodels工具包
from statsmodels.tsa.deterministic import CalendarFourier, DeterministicProcess
fourier = CalendarFourier(freq="A", order=10) # 10 sin/cos pairs for "A"nnual seasonality
dp = DeterministicProcess(
index=tunnel.index,
constant=True, # dummy feature for bias (y-intercept)
order=1, # trend (order 1 means linear)
seasonal=True, # weekly seasonality (indicators)
additional_terms=[fourier], # annual seasonality (fourier)
drop=True, # drop terms to avoid collinearity
)
X = dp.in_sample() # create features for dates in tunnel.index
适用问题
如果可视化分析中发现数据存在周期性的话,就可以使用上面的两种策略进行周期性特征的抽取。
参考文献
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有