It looks like you are trying to generate forecasts using GluonTS and analyze the forecast results. However, the code you provided has several issues. Let me address them step by step.
Issues Noticed
- Missing Import Statements: Ensure you have imported all necessary modules like
numpy (np), etc.
predictor.predict Usage: It seems you are loading forecasts incorrectly; this might not match the GluonTS's predictor.predict behavior.
- EVALUATION Variable: It is not clear how
EVALUATION is defined or used in this context.
- Data Shape Assumptions: Ensure your data shape matches the expectations. If each forecast point is an array of size 64, you must handle them accordingly.
Step-by-Step Approach to Fix and Clarify
1. Preconditions and Imports
Make sure you have all necessary imports and variables defined:
import numpy as np
from gluonts.model.deepar import DeepAREstimator
from gluonts.trainer import Trainer
from gluonts.evaluation.backtest import make_evaluation_predictions
from gluonts.evaluation import Evaluator
from gluonts.dataset.common import ListDataset
from gluonts.dataset.util import to_pandas
# Assuming `test_dataset`, `predictor`, `prediction_length`, etc., are already defined
2. Define the Training and Prediction Logic
- Ensure the training dataset (
training_data) and test dataset (test_data) are properly defined.
- Initialize the
predictor with the estimator and training data.
- Make predictions using
make_evaluation_predictions.
Here’s a more detailed and corrected version of your script:
import numpy as np
from gluonts.model.deepar import DeepAREstimator
from gluonts.trainer import Trainer
from gluonts.evaluation.backtest import make_evaluation_predictions
from gluonts.evaluation import Evaluator
from gluonts.dataset.common import ListDataset
# Ensure you have the necessary imports and definitions first
# `EVALUATION` is assumed to be a boolean variable, for example:
EVALUATION = True
# Example training and test dataset definitions
# (replace these with your actual datasets)
training_data = ListDataset(
[{"start": "2012-01-01", "target": time_series_data}],
freq="M" # monthly data as an example
)
test_data = ListDataset(
[{"start": "2012-01-01", "target": time_series_data}],
freq="M" # monthly data as an example
)
# Training the model and creating the predictor
trainer = Trainer(
epochs=20,
learning_rate=1e-3,
num_batches_per_epoch=100,
add_default_callbacks=True,
)
estimator = DeepAREstimator(
prediction_length=24,
context_length=48,
freq="M",
trainer=trainer,
num_layers=4,
num_cells=40,
cell_type='lstm',
dropout_rate=0.1,
use_feat_static_cat=True,
cardinality=[2],
batch_size=32,
embedding_dimension=[10],
)
predictor = estimator.train(training_data)
# Ensure EVALUATION is True, then proceed
if EVALUATION:
forecasts = []
# Generate predictions for test data
forecast_it, ts_it = make_evaluation_predictions(
dataset=test_data, predictor=predictor, num_samples=100
)
for forecast in forecast_it:
forecasts.append(forecast.samples)
forecasts = np.concatenate(forecasts, axis=0)
forecast_mean = np.mean(forecasts, axis=0)
print(f'Размерность матрицы с прогнозами: {forecasts.shape}')
print(f'Median Forecast: {np.median(forecast_mean, axis=1)}')
# Evaluating the forecasts
evaluator = Evaluator()
agg_metrics, item_metrics = evaluator(iter(ts_it), iter(forecast_it))
print(agg_metrics)
Explanation
- Training and Test Datasets: Use
ListDataset to define your datasets correctly.
- Model Training: Define your
Trainer and DeepAREstimator and then train the model.
- Prediction & Evaluation:
- Use
make_evaluation_predictions to generate predictions.
- Iterate over forecasts and calculate the mean and median as required.
- Evaluation: Use the
Evaluator to compute evaluation metrics.
Make sure you replace time_series_data with your actual data array. The prediction_length, context_length, and freq should be set according to your data's frequency and required forecast configuration.