GluonTS
GluonTS is a Python toolkit for probabilistic time series modeling, providing facilities for loading datasets, defining models, training them, and making predictions. It supports various deep learning backends, with PyTorch being the currently recommended and most actively developed one. The library is actively maintained with frequent minor releases, currently at version 0.16.2.
Warnings
- breaking The `pytorch-lightning` dependency often requires specific version alignment with GluonTS releases. Mismatched versions can lead to runtime errors, especially during training. For example, v0.16.0 included a `pytorch lightning compat` update.
- deprecated The MXNet backend (`gluonts.mx`) is less actively maintained and developed compared to the PyTorch backend (`gluonts.torch`). New models and features are primarily added to the PyTorch ecosystem.
- gotcha In older versions, `freq` string parsing could be inconsistent, leading to errors with certain frequency specifications or custom datasets. While fixed in v0.16.0, this was a common source of data loading issues.
- gotcha When constructing `PandasDataset` from `pandas.DataFrame.groupby` operations, a `FutureWarning` could be raised if `observed=True` was not explicitly passed to `groupby`. This was fixed in v0.16.1.
- gotcha The `rpy2` dependency, used for R-based models, was capped at a specific version in v0.16.2 (`rpy2<3.5.11,>=3.4.5`). Mismatched `rpy2` versions can lead to installation failures or runtime errors when using R models.
Install
-
pip install gluonts[torch] -
pip install gluonts[mxnet]
Imports
- ListDataset
from gluonts.dataset.common import ListDataset
- SimpleFeedForwardEstimator
from gluonts.torch.model.simple_feedforward import SimpleFeedForwardEstimator
- Trainer
from pytorch_lightning import Trainer
- make_evaluation_predictions
from gluonts.evaluation import make_evaluation_predictions
- MSE
from gluonts.evaluation.backtest import make_evaluation_predictions, backtest_metrics
- freqstr_to_timedelta
from gluonts.time_feature.util import freqstr_to_timedelta
Quickstart
from gluonts.dataset.common import ListDataset
from gluonts.torch.model.simple_feedforward import SimpleFeedForwardEstimator
from pytorch_lightning import Trainer
from gluonts.evaluation import make_evaluation_predictions, Evaluator
import pandas as pd
import numpy as np
# 1. Prepare Data
target_data = np.random.rand(100) # Dummy time series data
start_date = pd.Timestamp("2023-01-01", freq="H")
data_entry = {
"start": start_date,
"target": target_data,
"item_id": "item_A"
}
training_data = ListDataset([data_entry],
freq="H")
# For evaluation, we need to split data into past and future
# In a real scenario, you'd have separate test data or perform backtesting
prediction_length = 24
full_data_entry = {
"start": pd.Timestamp("2023-01-01", freq="H"),
"target": np.concatenate([np.random.rand(80), np.random.rand(24)]), # 80 for training, 24 for future
"item_id": "item_B"
}
# Simulate a test dataset by cutting off the prediction_length from the full series
test_data = ListDataset([{
"start": full_data_entry["start"],
"target": full_data_entry["target"][:-prediction_length],
"feat_static_cat": [0],
"item_id": full_data_entry["item_id"]
}], freq="H")
# 2. Define Estimator
estimator = SimpleFeedForwardEstimator(
prediction_length=prediction_length,
context_length=prediction_length * 2,
trainer=Trainer(max_epochs=5, enable_checkpointing=False, enable_progress_bar=False, logger=False),
num_hidden_dimensions=[10, 10]
)
# 3. Train the model
predictor = estimator.train(training_data=training_data)
# 4. Make Predictions
forecast_it, ts_it = make_evaluation_predictions(
dataset=test_data,
predictor=predictor,
num_samples=100
)
forecasts = list(forecast_it)
ts = list(ts_it)
# 5. Evaluate
evaluator = Evaluator(quantiles=[0.1, 0.5, 0.9])
agg_metrics, item_metrics = evaluator(ts, forecasts, num_series=len(test_data))
print("Aggregated metrics:", agg_metrics)
print("First forecast (mean):")
print(forecasts[0].mean)