如何在 GluonTS 中使用滑动窗口进行测试对?

问题描述 投票:0回答:1

我是 GluonTS 的新手,我正在尝试了解这个概念是如何工作的。 在文档网站上的“将数据集拆分为训练和测试”部分下

他们定义了一种分割训练数据和测试数据的机制,如下所示:

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可以直接使用,如下:

predictor = estimator.train(training_dataset)

这个test_pairs的类型是gluonts.dataset.split.TestData

但是当使用 test_pairs 作为预测的输入时:

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

ts_it 的类型将是一个“map”,当将其转换为列表时,它将返回一个空列表。

有谁知道如何使用 test_pairs 来实际进行预测和评估结果?

python amazon-web-services prediction forecasting gluonts
1个回答
0
投票

我必须为其编写自定义代码

加载数据框
df = pd.read_parquet("inbounds.parquet")
df.head(3)

df.head(3) dataframe

转换为 Pandas 数据集
ds = PandasDataset(df, target="containers")
定义变量
freq = "D"
split_date = "2023-03-31"
windows = 10
distance = 1
创建窗口数据集
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,
)
将窗口数据集转换为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)
使用 ds_test 进行预测
forecast_it, ts_it = make_evaluation_predictions(
    dataset=ds_test,
    predictor=predictor,
)
© www.soinside.com 2019 - 2024. All rights reserved.