ARIMA模型预测每周时间序列数据的误差

时间:2018-05-30 11:06:28

标签: python python-3.x matplotlib statsmodels arima

我使用ARIMA模型来预测产品的销售情况。数据位于2015年1月1日至2016年11月24日的csv文件中,间隔为1周。我试图提前9步,即未来9周。

csv中的数据:

"01-01-2015",9
"08-01-2015",8
"15-01-2015",13
"22-01-2015",10
"29-01-2015",12
"05-02-2015",5
"12-02-2015",4
"19-02-2015",6
"26-02-2015",9
"05-03-2015",3
"12-03-2015",3
"19-03-2015",2
...

这是我使用的代码:

def parser(x):
    return datetime.datetime.strptime(x, '%d-%m-%Y')
fn = 'filename.csv'
y = pd.read_csv(fn, header = 0, parse_dates = [0], index_col = 0, squeeze = True, date_parser = parser)
newmod = sm.tsa.statespace.SARIMAX(y,order=(1, 1, 0),seasonal_order=(1, 1, 0, 12),enforce_stationarity=False,enforce_invertibility=False)
newresults = newmod.fit()
pred_uc = newresults.get_forecast(steps = 9)
pred_ci = pred_uc.conf_int()
y1 = pred_ci.iloc[:,0]
y2 = pred_ci.iloc[:,1]
ax = y.plot(label = "observed")
pred_uc.predicted_mean.plot(ax=ax, label='Forecast')
ax.fill_between(pred_ci.index, pred_ci.iloc[:,0],pred_ci.iloc[:,1], color = 'k' , alpha = 0.5)
ax.set_xlabel('Date')
ax.set_ylabel('Sales count')
plt.legend()
plt.show()

该模型适用于[其他csv文件]的月度数据,但会为每周数据提供错误。这是错误:

Traceback (most recent call last):
File "H:\p36564\fit_net1.py", line 57, in <module>
pred_uc.predicted_mean.plot(ax=ax, label='Forecast')
File "H:\p36564\lib\site-packages\pandas\plotting\_core.py", line 2503, in __call__
**kwds)
File "H:\p36564\lib\site-packages\pandas\plotting\_core.py", line 1927, in plot_series
**kwds)
File "H:\p36564\lib\site-packages\pandas\plotting\_core.py", line 1729, in _plot
plot_obj.generate()
File "H:\p36564\lib\site-packages\pandas\plotting\_core.py", line 258, in generate
self._post_plot_logic_common(ax, self.data)
File "H:\p36564\lib\site-packages\pandas\plotting\_core.py", line 396, in _post_plot_logic_common
fontsize=self.fontsize)
File "H:\p36564\lib\site-packages\pandas\plotting\_core.py", line 470, in _apply_axis_properties
labels = axis.get_majorticklabels() + axis.get_minorticklabels()
File "H:\p36564\lib\site-packages\matplotlib\axis.py", line 1245, in get_majorticklabels
ticks = self.get_major_ticks()
File "H:\p36564\lib\site-packages\matplotlib\axis.py", line 1396, in get_major_ticks
numticks = len(self.get_major_locator()())
File "H:\p36564\lib\site-packages\matplotlib\dates.py", line 1249, in __call__
self.refresh()
File "H:\p36564\lib\site-packages\matplotlib\dates.py", line 1269, in refresh
dmin, dmax = self.viewlim_to_dt()
File "H:\p36564\lib\site-packages\matplotlib\dates.py", line 1026, in viewlim_to_dt
.format(vmin))
ValueError: view limit minimum -36710.65 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units.

可能有什么不对?

0 个答案:

没有答案