长期预测是指对遥远未来的预测,通常超过两个季节周期。然而,‘长期’的精确定义可能因数据频率而异。例如,处理每小时数据时,预测未来三天的数据被认为是长期预测,因为它涵盖了 72 个时间戳(计算方式为 3 天 × 24 小时/天)。在月度数据的背景下,超过两年的时期通常被归类为长期预测。类似地,对于每日数据,跨越两周以上的预测属于长期类别。

当然,长期预测也伴随着挑战。预测范围越长,预测结果的不确定性就越大。在长期预测中,还可能出现预测时未预料到的未知因素。

为了应对这些挑战,您可以在设置中通过指定 model='timegpt-1-long-horizon' 来使用 TimeGPT 的长期预测专用模型。

有关详细的分步指南,请参阅此长期预测教程。

1. 导入包

首先,我们安装并导入所需的包,并初始化 Nixtla 客户端。

from nixtla import NixtlaClient
from datasetsforecast.long_horizon import LongHorizon
from utilsforecast.losses import mae
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_provided_by_nixtla'
)

👍 使用 Azure AI 端点

要使用 Azure AI 端点,请记住也要设置 base_url 参数

nixtla_client = NixtlaClient(base_url="you azure ai endpoint", api_key="your api_key")

2. 加载数据

让我们加载 ETTh1 数据集。这是一个广泛用于评估模型长期预测能力的数据集。

ETTh1 数据集监控了中国某省一个区域的电力变压器,包含油温和负荷变量(如有益负荷和无用负荷),时间范围从 2016 年 7 月到 2018 年 7 月,频率为每小时。

在本教程中,我们只考虑油温随时间的变化。

Y_df, *_ = LongHorizon.load(directory='./', group='ETTh1')

Y_df.head()
100%|██████████| 314M/314M [00:14<00:00, 21.3MiB/s] 
INFO:datasetsforecast.utils:Successfully downloaded datasets.zip, 314116557, bytes.
INFO:datasetsforecast.utils:Decompressing zip file...
INFO:datasetsforecast.utils:Successfully decompressed longhorizon\datasets\datasets.zip
unique_iddsy
0OT2016-07-01 00:00:001.460552
1OT2016-07-01 01:00:001.161527
2OT2016-07-01 02:00:001.161527
3OT2016-07-01 03:00:000.862611
4OT2016-07-01 04:00:000.525227

对于这个小实验,我们将预测范围设置为 96 个时间步(未来 4 天),并将 TimeGPT 输入序列设置为 42 天的数据。

test = Y_df[-96:]             # 96 = 4 days x 24h/day
input_seq = Y_df[-1104:-96]   # Gets a sequence of 1008 observations (1008 = 42 days * 24h/day)

3. 长期预测

现在,我们准备使用 TimeGPT 进行长期预测。在这里,我们需要将 model 参数设置为 "timegpt-1-long-horizon"。这是 TimeGPT 中专门处理此类任务的模型。

fcst_df = nixtla_client.forecast(
    df=input_seq,
    h=96,
    level=[90],
    finetune_steps=10,
    finetune_loss='mae',
    model='timegpt-1-long-horizon',
    time_col='ds',
    target_col='y'
)
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: H
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...

📘 Azure AI 中可用的模型

如果您正在使用 Azure AI 端点,请务必设置 model="azureai"

nixtla_client.forecast(..., model="azureai")

nixtla_client.plot(Y_df[-168:], fcst_df, models=['TimeGPT'], level=[90], time_col='ds', target_col='y')

评估

现在,我们使用平均绝对误差 (MAE) 来评估 TimeGPT 的性能。

test = test.copy()

test.loc[:, 'TimeGPT'] = fcst_df['TimeGPT'].values
evaluation = mae(test, models=['TimeGPT'], id_col='unique_id', target_col='y')

print(evaluation)
  unique_id   TimeGPT
0        OT  0.145393

在这里,TimeGPT 的 MAE 为 0.146。