TimeGPT 为多序列预测提供了强大的解决方案,它涉及同时分析多个数据序列,而不是单个序列。该工具可以使用大量序列进行微调,使您能够根据自己的特定需求或任务定制模型。

请注意,预测仍然是单变量的。这意味着尽管 TimeGPT 是一个全局模型,但它不会考虑目标序列中的特征间关系。然而,TimeGPT 支持使用外部变量,例如分类变量(例如,类别、品牌)、数值变量(例如,温度、价格)或甚至特殊假日。

让我们看看实际操作。

1. 导入包

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

像往常一样,我们首先初始化 NixtlaClient 的一个实例。

import pandas as pd
from nixtla import NixtlaClient
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. 加载数据

以下数据集包含欧洲不同电力市场的价格。

TimeGPT 使用 unique_id 列自动检测多个序列。此列包含每个序列的标签。如果此列中有多个唯一值,则表示它正在处理多序列场景。

在此特定情况下,unique_id 列包含值 BE、DE、FR、JPM 和 NP。

df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short.csv')
df.head()
unique_iddsy
0BE2016-12-01 00:00:0072.00
1BE2016-12-01 01:00:0065.80
2BE2016-12-01 02:00:0059.99
3BE2016-12-01 03:00:0050.69
4BE2016-12-01 04:00:0052.58

让我们使用 [NixtlaClient](https://Nixtla.github.io/nixtla/src/nixtla_client.html#nixtlaclient) 绘制此序列

nixtla_client.plot(df)

3. 预测多个序列

为了同时预测所有序列,我们只需将 dataframe 传递给 df 参数。TimeGPT 将自动预测所有序列。

timegpt_fcst_multiseries_df = nixtla_client.forecast(df=df, h=24, level=[80, 90])
timegpt_fcst_multiseries_df.head()
INFO:nixtlats.nixtla_client:Validating inputs...
INFO:nixtlats.nixtla_client:Preprocessing dataframes...
INFO:nixtlats.nixtla_client:Inferred freq: H
INFO:nixtlats.nixtla_client:Restricting input...
INFO:nixtlats.nixtla_client:Calling Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-90TimeGPT-lo-80TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-31 00:00:0046.15117636.66047838.33701953.96533455.641875
1BE2016-12-31 01:00:0042.42659831.60223133.97672450.87647153.250964
2BE2016-12-31 02:00:0040.24288930.43997033.63498546.85079450.045809
3BE2016-12-31 03:00:0038.26533926.84148131.02209345.50858549.689197
4BE2016-12-31 04:00:0036.61880118.54138427.98134645.25625654.696218

📘 Azure AI 中可用的模型

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

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

对于公共 API,我们支持两种模型:timegpt-1timegpt-1-long-horizon

默认情况下,使用 timegpt-1。有关如何以及何时使用 timegpt-1-long-horizon,请参阅此教程

nixtla_client.plot(df, timegpt_fcst_multiseries_df, max_insample_length=365, level=[80, 90])

从上图可以看出,模型有效地为数据集中的每个唯一序列生成了预测。

历史预测

您还可以通过添加 add_history=True 来计算历史预测的预测区间。

要指定置信区间,我们使用 level 参数。在这里,我们传递列表 [80, 90]。这将计算 80% 和 90% 的置信区间。

timegpt_fcst_multiseries_with_history_df = nixtla_client.forecast(df=df, h=24, level=[80, 90], add_history=True)
timegpt_fcst_multiseries_with_history_df.head()
INFO:nixtlats.nixtla_client:Validating inputs...
INFO:nixtlats.nixtla_client:Preprocessing dataframes...
INFO:nixtlats.nixtla_client:Inferred freq: H
INFO:nixtlats.nixtla_client:Calling Forecast Endpoint...
INFO:nixtlats.nixtla_client:Calling Historical Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-80TimeGPT-lo-90TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-06 00:00:0055.75633242.06647638.18559369.44618873.327072
1BE2016-12-06 01:00:0052.82020639.13035035.24946666.51006270.390946
2BE2016-12-06 02:00:0046.85107033.16121429.28033160.54092664.421810
3BE2016-12-06 03:00:0050.64089236.95103633.07015264.33074868.211632
4BE2016-12-06 04:00:0052.42041038.73055434.84967066.11026669.991150

📘 Azure AI 中可用的模型

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

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

对于公共 API,我们支持两种模型:timegpt-1timegpt-1-long-horizon

默认情况下,使用 timegpt-1。有关如何以及何时使用 timegpt-1-long-horizon,请参阅此教程

nixtla_client.plot(
    df, 
    timegpt_fcst_multiseries_with_history_df.groupby('unique_id').tail(365 + 24), 
    max_insample_length=365, 
    level=[80, 90],
)

在上图中,我们现在看到了 TimeGPT 为每个序列生成的历史预测,以及 80% 和 90% 的置信区间。