如何使用statsmodels ARIMA预测下一个值?

时间:2019-05-18 20:47:56

标签: python statistics statsmodels arima

我有一个由datetime.date对象索引的熊猫系列,并且有一些我感兴趣的建模值。我大约有250行。我正在尝试使用前150个示例使用statsmodels模块(版本0.9.0)来训练ARIMA模型,然后为第151个示例生成预测(我也具有它的真实价值)。需要注意的重要一点是,我只有工作日的值,而没有周末的值。我希望有一个很好的方法来解决这个问题。

>> close[0:151]
2018-01-02    0.460
2018-01-03    0.465
2018-01-04    0.475
2018-01-05    0.480
2018-01-08    0.475
2018-01-09    0.500
2018-01-10    0.490
2018-01-11    0.505
2018-01-12    0.500
2018-01-15    0.510
2018-01-16    0.535
2018-01-17    0.500
2018-01-18    0.530
2018-01-19    0.540
2018-01-22    0.505
2018-01-23    0.475
2018-01-24    0.440
2018-01-25    0.490
2018-01-29    0.465
2018-01-30    0.465
2018-01-31    0.465
2018-02-01    0.475
2018-02-02    0.475
2018-02-05    0.455
2018-02-06    0.435
2018-02-07    0.455
2018-02-08    0.455
2018-02-09    0.285
2018-02-12    0.290
2018-02-13    0.275
              ...  
2018-06-27    0.250
2018-06-28    0.245
2018-06-29    0.230
2018-07-02    0.220
2018-07-03    0.250
2018-07-04    0.240
2018-07-05    0.235
2018-07-06    0.225
2018-07-09    0.225
2018-07-10    0.220
2018-07-11    0.215
2018-07-12    0.220
2018-07-13    0.210
2018-07-16    0.220
2018-07-17    0.215
2018-07-18    0.235
2018-07-19    0.240
2018-07-20    0.230
2018-07-23    0.235
2018-07-24    0.215
2018-07-25    0.220
2018-07-26    0.215
2018-07-27    0.215
2018-07-30    0.205
2018-07-31    0.200
2018-08-01    0.195
2018-08-02    0.205
2018-08-03    0.215
2018-08-06    0.215
2018-08-07    0.210
Name: Close, Length: 151, dtype: float64

有人可以帮助我使用前150行来训练ARIMA模型并为第151行打印预测吗?

尝试跟随别人所做的事情,我尝试了以下方法:

from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(close[:150], order=(1,1,1))
model_fit = model.fit(disp=0)

那个模型可以训练,但是当我尝试时:

pred = model_fit.predict(start=close.index[150], dynamic=False, typ='levels')

返回以下错误:

    ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: datetime.date(2018, 8, 7)

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    415                 if not isinstance(key, (int, long, np.integer)):
--> 416                     loc = self.data.row_labels.get_loc(key)
    417                 else:

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: datetime.date(2018, 8, 7)

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in _get_prediction_index(self, start, end, index, silent)
    476         try:
--> 477             start, start_index, start_oos = self._get_index_label_loc(start)
    478         except KeyError:

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    422             except:
--> 423                 raise e
    424         return loc, index, index_was_expanded

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_label_loc(self, key, base_index)
    411             loc, index, index_was_expanded = (
--> 412                 self._get_index_loc(key, base_index))
    413         except KeyError as e:

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in _get_index_loc(self, key, base_index)
    363             except (IndexError, ValueError) as e:
--> 364                 raise KeyError(str(e))
    365             loc = key

KeyError: 'only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-111-398ffb8cf06c> in <module>
----> 1 pred = model_fit.predict(start=close.index[150], dynamic=False, typ='levels')

~/anaconda3/lib/python3.7/site-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs)
     93             obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:])
     94         elif how:
---> 95             obj = data.wrap_output(func(results, *args, **kwargs), how)
     96         return obj
     97 

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, typ, dynamic)
   1823     def predict(self, start=None, end=None, exog=None, typ='linear',
   1824                 dynamic=False):
-> 1825         return self.model.predict(self.params, start, end, exog, typ, dynamic)
   1826     predict.__doc__ = _arima_results_predict
   1827 

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/arima_model.py in predict(self, params, start, end, exog, typ, dynamic)
   1196             if not dynamic:
   1197                 predict = super(ARIMA, self).predict(params, start, end, exog,
-> 1198                                                      dynamic)
   1199 
   1200                 start, end, out_of_sample, _ = (

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/arima_model.py in predict(self, params, start, end, exog, dynamic)
    717         # will return an index of a date
    718         start, end, out_of_sample, _ = (
--> 719             self._get_prediction_index(start, end, dynamic))
    720 
    721         if out_of_sample and (exog is None and self.k_exog > 0):

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/arima_model.py in _get_prediction_index(self, start, end, dynamic, index)
   1059 
   1060         start, end, out_of_sample, prediction_index = (
-> 1061             super(ARIMA, self)._get_prediction_index(start, end, index))
   1062 
   1063         # From _get_predict_end

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/arima_model.py in _get_prediction_index(self, start, end, dynamic, index)
    649 
    650         start, end, out_of_sample, prediction_index = (
--> 651             super(ARMA, self)._get_prediction_index(start, end, index))
    652 
    653         # This replaces the _validate() call

~/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/base/tsa_model.py in _get_prediction_index(self, start, end, index, silent)
    477             start, start_index, start_oos = self._get_index_label_loc(start)
    478         except KeyError:
--> 479             raise KeyError('The `start` argument could not be matched to a'
    480                            ' location related to the index of the data.')
    481         if end is None:

KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'

0 个答案:

没有答案