Dask 是一个用于 Python 的开源并行计算库。在本指南中,我们将解释如何在 Dask 之上使用 TimeGPT

概要

  1. 安装

  2. 加载数据

  3. 导入 Dask

  4. 在 Dask 上使用 TimeGPT

1. 安装

通过 Fugue 安装 Dask。Fugue 提供了一个易于使用的分布式计算接口,允许用户在包括 Dask 在内的多个分布式计算框架之上执行 Python 代码。

注意

您可以通过 pip 安装 fugue

pip install fugue[dask]

如果在分布式 Dask 集群上执行,请确保所有 worker 都安装了 nixtla 库。

2. 加载数据

您可以将数据加载为 pandas DataFrame。在本教程中,我们将使用一个包含来自不同市场的每小时电价的数据集。

import pandas as pd
df = pd.read_csv(
    'https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short.csv',
    parse_dates=['ds'],
) 
df.head()
unique_iddsy
0BE2016-10-22 00:00:0070.00
1BE2016-10-22 01:00:0037.10
2BE2016-10-22 02:00:0037.10
3BE2016-10-22 03:00:0044.75
4BE2016-10-22 04:00:0037.10

3. 导入 Dask

导入 Dask 并将 pandas DataFrame 转换为 Dask DataFrame。

import dask.dataframe as dd
dask_df = dd.from_pandas(df, npartitions=2)
dask_df
unique_iddsy
npartitions=2
0stringstringfloat64
4200
8399

4. 在 Dask 上使用 TimeGPT

Dask 之上使用 TimeGPT 几乎与非分布式情况相同。唯一的区别是您需要使用 Dask DataFrame,这我们在上一步中已经定义了。

首先,实例化 NixtlaClient 类。

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")

然后使用 NixtlaClient 类中的任何方法,例如 forecastcross_validation

fcst_df = nixtla_client.forecast(dask_df, h=12)
fcst_df.compute().head()
unique_iddsTimeGPT
0BE2016-12-31 00:00:0045.190453
1BE2016-12-31 01:00:0043.244446
2BE2016-12-31 02:00:0041.958389
3BE2016-12-31 03:00:0039.796486
4BE2016-12-31 04:00:0039.204533

📘 Azure AI 中的可用模型

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

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

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

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

cv_df = nixtla_client.cross_validation(dask_df, h=12, n_windows=5, step_size=2)
cv_df.compute().head()
unique_iddscutoffTimeGPT
0BE2016-12-30 04:00:002016-12-30 03:00:0039.375439
1BE2016-12-30 05:00:002016-12-30 03:00:0040.039215
2BE2016-12-30 06:00:002016-12-30 03:00:0043.455849
3BE2016-12-30 07:00:002016-12-30 03:00:0047.716408
4BE2016-12-30 08:00:002016-12-30 03:00:0050.31665

您还可以在 Dask 之上使用带有 TimeGPT 的外部变量。为此,请参阅外部变量教程。请记住,您需要使用 Dask DataFrame,而不是 pandas DataFrame。