Arima 模型的重要假设
ARMA, ARIMA, SARIMA assumptions:
▪ Time-series data is stationary.
▪ If nonstationary, remove trend, seasonality, apply differencing, and so on.
▪ Remember that stationary data has no trend, seasonality, constant mean, and
constant variance.
▪ Therefore, the past is assumed to represent what will happen in the future
in a probabilistic sense.
1. 安装
pyramid-arima 的安装请见 https://pypi.org/project/pyramid-arima/
(我只在linux系统上成功安装了,windows上没有成功)
$ pip install pyramid-arima
函数参数介绍请见 https://www.alkaline-ml.com/pmdarima/index.html
Github上的例子请见 https://github.com/tgsmith61591/pmdarima
2. 代码实例
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import pmdarima as pm
import warnings
warnings.filterwarnings("ignore")
2.1 加载数据集
df = pd.read_csv('dataset.csv')
split_point = 1000
data_train = df['x'].iloc[:split_point].values
data_test = df['x'].iloc[split_point:].values
2.2 训练模型
pm.auto_arima可以自动搜索出arima模型中的(q, d, p)参数
-
p--代表预测模型中采用的时序数据本身的滞后数(lags) ,也叫做AR/Auto-Regressive项
-
d--代表时序数据需要进行几阶差分化,才是稳定的,也叫Integrated项
-
q--代表预测模型中采用的预测误差的滞后数(lags),也叫做MA/Moving Average项
参考 https://blog.csdn.net/HHXUN/article/details/79858672
model = pm.auto_arima(data_train)
2.3 模型预测
利用 model.predict() 函数预测
x_pred = model.predict(n_periods=1)
或更优的,使用 model.update() 函数,不断用新观测到的 value 更新模型,以达到更长时间的预测。
pred_list = []
for x_i in data_test:
pred_list += [model.predict(n_periods=1)]
# 更新模型
model.update(x_i)
2.4 模型评价
from sklearn import metrics
def eval_metrics(y_true, y_pred):
metrics_dict = dict()
metrics_dict['MAE'] = metrics.mean_absolute_error(y_true, y_pred)
metrics_dict['MSE'] = metrics.mean_squared_error(y_true, y_pred)
metrics_dict['MAPE'] = np.mean(np.true_divide(np.abs(y_true-y_pred), y_true))
return metrics_dict
eval_dict = eval_metrics(data_test, np.array(pred_list))
print(eval_dict)
作者:菜鸟程序猿zq
链接:https://www.jianshu.com/p/54826718be4f
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论