How to use a sliding window for test pairs in GluonTS?

183 Views Asked by At

I am new to GluonTS and I am trying to understand how the concept works. On the documentation website, under the section "Splitting datasets into training and test"

they define a mechanism to split the train and test data as follows:

training_dataset, test_template = split(
    dataset, date=pd.Period("2015-04-07 00:00:00", freq="1H")
)
test_pairs = test_template.generate_instances(
    prediction_length=prediction_length,
    windows=3,
    distance=24,
)

training_dataset can be used directly as follows:

predictor = estimator.train(training_dataset)

The type of this test_pairs is gluonts.dataset.split.TestData

but when using test_pairs as input for forecasting:

forecast_it, ts_it = make_evaluation_predictions(
        dataset=test_pairs, predictor=predictor,
        num_samples=100,
    )

The type for ts_it will be a "map" and when converting it to a list it will return an empty list.

Does anyone know how to use the test_pairs for actually making predictions and evaluating the results?

1

There are 1 best solutions below

0
On

I've had to write custom code for it

Load dataframe
df = pd.read_parquet("inbounds.parquet")
df.head(3)

df.head(3) dataframe

Convert to PandasDataset
ds = PandasDataset(df, target="containers")
Define variables
freq = "D"
split_date = "2023-03-31"
windows = 10
distance = 1
Create windowed dataset
ds_train, test_template = split(
    ds, date=pd.Period(split_date, freq=freq)
)
ds_test_windows = test_template.generate_instances(
    prediction_length=prediction_length,
    windows=windows,
    distance=distance,
)
Convert windowed dataset to ListDataset
def windows_to_ds_test(ds_test_windows: TestData) -> ListDataset:
    windows = list(ds_test_windows)
    ds_test = [{
            "start": w[0]["start"],
            "target": np.concatenate((w[0]["target"], w[1]["target"]))
    } for w in windows]
    return ListDataset(ds_test, freq=freq)

ds_test = windows_to_ds_test(ds_test_windows)
Use ds_test to forecast
forecast_it, ts_it = make_evaluation_predictions(
    dataset=ds_test,
    predictor=predictor,
)