sarima模型python代码
时间: 2023-11-13 10:48:03 浏览: 48
以下是SARIMA模型的Python代码示例:
```python
import pandas as pd
import numpy as np
import statsmodels.api as sm
# 载入数据
data = pd.read_csv('data.csv', index_col='date', parse_dates=True)
data.index.freq = 'D'
# 拆分训练集和测试集
train_data = data.iloc[:1000]
test_data = data.iloc[1000:]
# 拟合 SARIMA 模型
model = sm.tsa.statespace.SARIMAX(train_data,
order=(1, 1, 1),
seasonal_order=(1, 1, 0, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results = model.fit()
# 预测测试集
predictions = results.get_prediction(start=pd.to_datetime('2020-01-01'), end=pd.to_datetime('2021-01-01'))
# 计算误差指标
mse = ((predictions.predicted_mean - test_data) ** 2).mean()
rmse = np.sqrt(mse)
mape = np.abs((predictions.predicted_mean - test_data) / test_data).mean()
# 打印结果
print("SARIMA模型的均方误差是:{:.2f}".format(mse))
print("SARIMA模型的均方根误差是:{:.2f}".format(rmse))
print("SARIMA模型的平均绝对误差是:{:.2%}".format(mape))
```
请注意,这是一个通用代码示例,具体的SARIMA模型的参数和数据处理逻辑需要根据实际情况进行调整。