arima代码python
时间: 2023-09-17 21:07:16 浏览: 66
以下是使用ARIMA模型进行时间序列预测的Python代码:
```python
import pandas as pd
import numpy as np
import statsmodels.api as sm
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import math
import matplotlib.pyplot as plt
# 假设你的时间序列数据存储在名为dc的变量中
# 确定差分次数
d = 1
# 一阶差分
data1 = np.diff(dc, n=d)
# 确定ARIMA模型的阶数
train_results = sm.tsa.arma_order_select_ic(data1, ic=['aic', 'bic'], max_ar=4, max_ma=4)
p, q = train_results.aic_min_order
# 建立ARIMA模型
model = sm.tsa.ARIMA(dc, order=(p, d, q))
model_fit = model.fit()
# 预测未来值
forecast_data = model_fit.forecast(steps=11)[0]
# 可视化预测结果
plt.plot(dc, label='原数据')
plt.plot(forecast_data, label='预测数据')
plt.legend()
plt.show()
# 计算评估指标
mse = mean_squared_error(dc, forecast_data)
rmse = math.sqrt(mse)
mae = mean_absolute_error(dc, forecast_data)
r_square = r2_score(dc, forecast_data)
# 打印评估指标结果
print('均方误差MSE: %.6f' % mse)
print('均方根误差RMSE: %.6f' % rmse)
print('平均绝对误差MAE: %.6f' % mae)
print('R_square: %.6f' % r_square)
```
这段代码首先根据给定的时间序列数据确定ARIMA模型的阶数(P, D, Q),然后通过ARIMA模型进行时间序列预测,并计算预测结果的评估指标(均方误差MSE、均方根误差RMSE、平均绝对误差MAE和R_square)。最后,可视化原始数据和预测数据的图像。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)