简体   繁体   English

努力制作带有统计模型的ARIMA预测图

[英]Struggling to make a working ARIMA forecast graph with statsmodels

I'm hoping to read data from a file (Date and Quantity columns) and plot them onto a graph with an ARIMA forecast. 我希望从文件(日期和数量列)中读取数据,并将其绘制到具有ARIMA预测的图形上。

Unfortunately, I've had no luck with the online guides that I've used, and each has lead me to different issues. 不幸的是,我使用的在线指南运气不佳,而且每个指南都会导致我遇到不同的问题。

Here is my basic code (which just plots the data without a forecast): 这是我的基本代码(它只是在没有预测的情况下绘制数据):

from pandas import Series
from matplotlib import pyplot
from statsmodels.tsa.arima_model import ARIMA

series = Series.from_csv('Quantity.csv',header=0)

model = ARIMA(series, order=(2,0,1))
series.plot()
pyplot.show()

Here is the data I'm practicing with: 这是我正在使用的数据:

Date    Quantity
2010/01/01  1358
2010/07/02  0
2010/08/03  0
2011/02/04  0
2011/11/05  0
2011/12/06  274
2012/06/07  1074
2012/08/30  2223
2013/04/16  0
2013/03/18  1753
2014/02/22  345
2014/01/27  24
2015/12/15  652
2015/09/28  275
2016/05/04  124
2017/11/07  75
2017/09/22  32
2017/04/04  12

So how can I create an ARIMA forecast and put it onto my excising plot? 那么,如何创建ARIMA预测并将其放到我的切除图上?

Also, I'm not entirely sure how an ARIMA forecast in Python is meant to look once plotted (I've only seen them in R but from what I've seen its not the same case for Python), so perhaps an example would be nice. 另外,我也不是完全确定Python中的ARIMA预测是如何绘制的(我只在R中看到过,但是从我看到的情况来看,对于Python并不相同),所以也许举一个例子对人好点。

First, I'd advise to use read_csv method and parse your dates here: 首先,建议您使用read_csv方法并在此处解析日期:

series = pd.read_csv('Quantity.csv', header=0, parse_dates=[0])
series.columns = ['Date', 'Quantity']

You need to use the fit method from the library, convert the Quantity column to float64 in order to avoid the error you mentionned: 您需要使用库中的fit方法,将Quantity列转换为float64,以避免出现您提到的错误:

model = ARIMA(series['Quantity'].astype(float), order=(2,0,1), dates=series['Date'])

(disp=0 if you don't want to print all the details of the computation) (如果您不想打印所有计算细节,则disp = 0)

Then you get your predicted value from the fitted model using: 然后,您可以使用以下方法从拟合模型中获得预测值:

fittedModel.forecast(steps=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM