两个模型的预测值放在一张图上
时间: 2024-03-15 18:47:37 浏览: 23
要将两个模型的预测值放在同一张图中,可以使用两个模型分别进行预测,然后将它们的预测值合并到一个DataFrame中,并使用`plot`方法来绘制图表。下面是示例代码:
```python
import pandas as pd
from statsmodels.tsa.api import ExponentialSmoothing
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('data.csv', header=0, index_col=0, parse_dates=True)
# 拆分训练集和测试集
train_size = int(len(df) * 0.8)
train, test = df[:train_size], df[train_size:]
# 拟合Holt-Winter模型
model1 = ExponentialSmoothing(train, seasonal_periods=12, trend='add', seasonal='add')
model1_fit = model1.fit()
# 拟合ARIMA模型
model2 = ARIMA(train, order=(2, 1, 1))
model2_fit = model2.fit()
# 预测测试集
pred1 = model1_fit.forecast(len(test))
pred2 = model2_fit.forecast(len(test))
# 合并预测结果
result = pd.DataFrame({'Holt-Winter': pred1, 'ARIMA': pred2}, index=test.index)
# 绘制预测结果和实际值
ax = df.plot()
result.plot(ax=ax)
plt.legend()
plt.show()
```
在这个示例中,我们首先分别拟合了两个模型,并使用`forecast`方法对它们进行了预测。然后,我们将两个模型的预测结果合并到一个DataFrame中,并使用`plot`方法将其绘制在同一个图表上。最后,我们使用`legend`方法来添加图例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)