statsmodels 0.13.2中ARIMAResults无plot_predict属性报错咨询
问题产生原因
statsmodels在版本迭代过程中对ARIMA模块的API做了调整:0.12.0及之后的版本,已经将原本绑定在ARIMAResults对象上的plot_predict实例方法移除,统一迁移到独立的时间序列绘图工具模块下。你使用的0.13.2版本属于调整后的版本,因此直接调用model_fit.plot_predict()会触发属性不存在的报错,不属于代码逻辑错误。
可用解决方法
方法1:调用官方迁移后的独立绘图函数(最推荐,和原方法逻辑完全兼容)
从时间序列绘图模块导入独立的plot_predict函数,替换原有绘图代码即可,原有参数无需修改:import matplotlib.pyplot as plt import statsmodels.api as sm # 新增导入绘图函数 from statsmodels.graphics.tsaplots import plot_predict model = sm.tsa.arima.ARIMA(df.Sales, order=(1,1,1)) model_fit = model.fit() print(model_fit.summary()) # Actual vs Fitted plot_predict(model_fit, dynamic=False) plt.show()方法2:手动提取拟合值自定义绘图(灵活度最高)
如果需要自定义绘图样式,可以直接从训练好的模型对象中提取样本内拟合值,和真实值拼接后自行绘图:import matplotlib.pyplot as plt model = sm.tsa.arima.ARIMA(df.Sales, order=(1,1,1)) model_fit = model.fit() print(model_fit.summary()) # Actual vs Fitted plt.figure(figsize=(12, 6)) # 绘制真实值 plt.plot(df.Sales, label='Actual Sales') # 绘制模型拟合值 plt.plot(model_fit.fittedvalues, label='Fitted Values', color='darkorange') plt.legend() plt.show()注:由于你设置的差分阶数d=1,模型返回的拟合值会自动和原始序列索引对齐,差分导致的前序缺失值会自动填充为NaN,不会干扰绘图结果。
方法3:降级statsmodels版本(不推荐)
可以将statsmodels降级到0.11.x及更早的版本,即可直接使用model_fit.plot_predict()的旧写法,但旧版本存在较多已修复的逻辑bug和性能问题,非特殊情况不建议使用。
内容的提问来源于stack exchange,提问作者MohammedE
相关产品推荐
相关产品推荐

