Как прогнозировать неизвестные будущие целевые значения с помощью глюонтов DeepAR?

Как прогнозировать неизвестные будущие целевые значения с помощью глюонтов DeepAR? У меня есть временной ряд с 1 января 1995 года по 01 октября 2021 года. Ежемесячные данные частоты. Как прогнозировать значения на будущее (следующие 3 месяца): с 01.11.2021 по 01.01.2022? Обратите внимание, что у меня нет целевых значений на 01.11.2021, 01.12.2021 и 01.01.2022. Большое спасибо!

      from gluonts.model.deepar import DeepAREstimator
from gluonts.mx import Trainer 
import numpy as np
import mxnet as mx

np.random.seed(7)
mx.random.seed(7)

estimator = DeepAREstimator(
    prediction_length=12
    , context_length=120
    , freq='M'
    , trainer=Trainer(        
        epochs=5
        , learning_rate=1e-03
        , num_batches_per_epoch=50))

predictor = estimator.train(training_data=df_train)

# Forecasting
predictions = predictor.predict(df_test)
predictions = list(predictions)[0]
predictions = predictions.quantile(0.5)

print(predictions)
[163842.34  152805.08  161326.3   176823.97  127003.79  126937.78
 139575.2   117121.67  115754.67  139211.28  122623.586 120102.65 ]

Как я понял, значения прогнозов не для "2021-11-01", "2021-12-01" и "2022-01-01". Как узнать, к каким месяцам относятся эти значения? Как спрогнозировать значения на следующие 3 месяца: "2021-11-01", "2021-12-01" и "2022-01-01"?

Взгляните на этот код. Это взято из «Расширенного прогнозирования с Python». https://github.com/Apress/advanced-forecasting-python/blob/main/Chapter%2020%20-%20Amazon's%20DeepAR.ipynb

Кажется, что он не прогнозирует неизвестные будущие значения , поскольку он сравнивает последние 28 значений test_ds ( листинг 20-5. Оценка R2 и график прогнозов ) с прогнозами, сделанными на основе того же набора данных test_ds ( листинг 20-4. Прогноз )

Как прогнозировать неизвестные будущие значения ?

Большое спасибо!

Источник данных

https://www.kaggle.com/c/recruit-restaurant-visitor-forecasting

      # Listing 20-1. Importing the data
import pandas as pd
y = pd.read_csv('air_visit_data.csv.zip')
y = y.pivot(index='visit_date', columns='air_store_id')['visitors']
y = y.fillna(0)
y = pd.DataFrame(y.sum(axis=1))

y = y.reset_index(drop=False)
y.columns = ['date', 'y']


# Listing 20-2. Preparing the data format requered by the gluonts library
from gluonts.dataset.common import ListDataset
start = pd.Timestamp("01-01-2016", freq="H")
# train dataset: cut the last window of length "prediction_length", add "target" and "start" fields
train_ds = ListDataset([{'target': y.loc[:450,'y'], 'start': start}], freq='H')
# test dataset: use the whole dataset, add "target" and "start" fields
test_ds = ListDataset([{'target': y['y'], 'start': start}],freq='H')


# Listing 20-3. Fitting the default DeepAR model
from gluonts.model.deepar import DeepAREstimator
from gluonts.trainer import Trainer
import mxnet as mx
import numpy as np

np.random.seed(7)
mx.random.seed(7)

estimator = DeepAREstimator(
    prediction_length=28,
    context_length=100,
    freq=’H’,
    trainer=Trainer(ctx="gpu", # remove if running on windows
                    epochs=5,
                    learning_rate=1e-3,
                    num_batches_per_epoch=100
                   )
)

predictor = estimator.train(train_ds)



# Listing 20-4. Prediction
predictions = predictor.predict(test_ds)
predictions = list(predictions)[0]
predictions = predictions.quantile(0.5)



# Listing 20-5. R2 score and prediction graph
from sklearn.metrics import r2_score
print(r2_score( list(test_ds)[0]['target'][-28:], predictions))

import matplotlib.pyplot as plt
plt.plot(predictions)
plt.plot(list(test_ds)[0]['target'][-28:])
plt.legend(['predictions', 'actuals'])
plt.show()

1 ответ

В вашем случае длина контекста составляет 120, а длина прогноза - 12, поэтому модель будет искать 120 точек данных, чтобы предсказать 12 будущих точек данных.

Рекомендуется уменьшить контекст до 10 и включить данные за последние 10 месяцев в таблицу df_test.

вы можете получить начало прогноза, используя

      list(predictor.predict(df_test))[0].start_date

на основе этого создайте будущую таблицу из 12 дат (поскольку 12 - это длина прогноза)

Другие вопросы по тегам