时序分析 37 - 时序预测 从ARIMA到SARIMAX(六) SARIMAX
·
…接上
Step 7. SARIMAX
在前面理论部分,我们已经介绍了SARIMAX模型的基本原理,相比于SARIMA,它只不过增加了对已知外部变量进入回归模型的支持。虽然在本例中并没有非常有帮助的外部变量,但是没关系,我们只是需要演示一下这个模型的用法。
我们可以使用是否是节假日作为外部变量。
from datetime import date
import holidays
us_holidays = holidays.US()
df_store_2_item_28_with_holiday = df_store_2_item_28.copy()
df_store_2_item_28_with_holiday['holiday'] = df_store_2_item_28_with_holiday.date.apply(us_holidays.get)
df_store_2_item_28_with_holiday.head()

df_store_2_item_28_with_holiday['holiday'].fillna('Normal Day',inplace=True)
df_store_2_item_28_with_holiday.holiday.value_counts()

df_store_2_item_28_with_holiday['holiday'] = df_store_2_item_28_with_holiday.holiday.astype('category')
df_store_2_item_28_with_holiday['hcode'] = df_store_2_item_28_with_holiday.holiday.cat.codes
df_store_2_item_28_with_holiday.head()

好,我们已经创建了一个新变量hcode,是对节假日的编码;就用这个变量最为外部变量。原SARIMA部分采用自动模型选择的参数。
import statsmodels.api as sm
exog = sm.add_constant(df_store_2_item_28_with_holiday.set_index('date').loc[:, 'hcode'])
sarimax_model = SARIMAX(df_store_2_item_28_time, exog,order=(6,1,1), seasonal_order=(6,1,0,7))
sarimax_results = sarimax_model.fit()
# Calculate the mean absolute error from residuals
mae = np.mean(np.abs(sarimax_results.resid))
# Print mean absolute error
print('MAE: %.3f' % mae)
MAE: 4.787
和SARIMA2的结果几乎一样,看上去我们以假期来营造的这个外部变量并没有什么用。没关系,我们只是演示一下而已。
未完待续…
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)