python Statsmodels SARIMAX KeyError:"'start'参数无法与与数据索引相关的位置匹配。



我的第一篇堆栈溢出文章,我正在学习数据科学资格的兼职,我一直在使用Statsmodels SARIMAX预测

我的时间序列数据如下

ts_log.head()
Calendar Week
2016-02-22    8.168486
2016-02-29    8.252707
2016-03-07    8.324821
2016-03-14    8.371474
2016-03-21    8.766238
Name: Sales Quantity, dtype: float64
ts_log.tail()
Calendar Week
2020-07-20    8.326759
2020-07-27    8.273847
2020-08-03    8.286521
2020-08-10    8.222822
2020-08-17    8.011687
Name: Sales Quantity, dtype: float64

我运行以下

train = ts_log[:'2019-07-01'].dropna()
test = ts_log['2020-08-24':].dropna()
model = SARIMAX(train, order=(2,1,2), seasonal_order=(0,1,0,52) 
,enforce_stationarity=False, enforce_invertibility=False)
results = model.fit()

摘要显示

results.summary()
Dep. Variable:  Sales Quantity  No. Observations:   175
Model:  SARIMAX(2, 1, 2)x(0, 1, 0, 52)  Log Likelihood  16.441
Date:   Mon, 21 Sep 2020    AIC -22.883
Time:   22:32:28    BIC -8.987
Sample: 0   HQIC    -17.240
- 175       
Covariance Type:    opg     
coef    std err z   P>|z|   [0.025  0.975]
ar.L1   1.3171  0.288   4.578   0.000   0.753   1.881
ar.L2   -0.5158 0.252   -2.045  0.041   -1.010  -0.022
ma.L1   -1.5829 0.519   -3.048  0.002   -2.601  -0.565
ma.L2   0.5093  0.502   1.016   0.310   -0.474  1.492
sigma2  0.0345  0.011   3.195   0.001   0.013   0.056
Ljung-Box (Q):  30.08   Jarque-Bera (JB):   2.55
Prob(Q):    0.87    Prob(JB):   0.28
Heteroskedasticity (H): 0.54    Skew:   -0.02
Prob(H) (two-sided):    0.05    Kurtosis:   3.72

然而,当我试图预测时,我得到了一个关键错误,表明我的开始日期不正确,但我看不出有什么问题

pred = results.predict(start='2019-06-10',end='2020-08-17')[1:]
KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'

我可以看到这两个日期都是有效的:

ts_log['2019-06-10']
8.95686647085414
ts_log['2020-08-17']
8.011686729127847

如果我用数字运行,它运行良好

pred = results.predict(start=175,end=200)[1:]

我想使用日期,这样我就可以在我的时间序列图中使用它和其他日期

EmmaT,

你们似乎有相同的开始和结束日期。

start='2019-06-10',end='2019-06-10'

请仔细检查一下这是否是你想要的。还要检查数据集中是否存在"2019-06-10"。

最新更新