Как использовать statsmodels ARIMA для прогнозирования следующего значения? - PullRequest
1 голос
/ 18 мая 2019

У меня есть серия панд, проиндексированная объектами datetime.date и имеющая некоторые значения, которые меня интересуют в моделировании.У меня около 250 строк.Я пытаюсь использовать первые 150 примеров для обучения модели ARIMA с помощью модуля statsmodels (версия 0.9.0), а затем для составления прогноза для 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.'
...