Dask

!pip install -Uqq nixtla fugue[dask]
from nixtla.utils import in_colab
IN_COLAB = in_colab()
if not IN_COLAB:
    from nixtla.utils import colab_badge
    from dotenv import load_dotenv

在Dask上分布式运行TimeGPT

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

大纲:

  1. 安装

  2. 加载数据

  3. 导入 Dask

  4. 在 Dask 上使用 TimeGPT

if not IN_COLAB:
    load_dotenv()
    colab_badge('docs/tutorials/18_computing_at_scale_dask_distributed')

1. 安装

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

Note

您可以使用 pip 安装 fugue

pip install fugue[dask]

如果在分布式的 Dask 集群上执行,请确保 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_id ds y
0 BE 2016-10-22 00:00:00 70.00
1 BE 2016-10-22 01:00:00 37.10
2 BE 2016-10-22 02:00:00 37.10
3 BE 2016-10-22 03:00:00 44.75
4 BE 2016-10-22 04:00:00 37.10

3. 导入 Dask

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

import dask.dataframe as dd
dask_df = dd.from_pandas(df, npartitions=2)
dask_df 
Dask DataFrame Structure:
unique_id ds y
npartitions=2
0 string string float64
4200 ... ... ...
8399 ... ... ...
Dask Name: to_pyarrow_string, 2 graph layers

4. 在Dask上使用TimeGPT

使用 Dask 上的 TimeGPT 与非分布式案例几乎相同。唯一的区别是您需要使用一个 Dask 数据框,我们已经在上一步中定义了它。

首先,实例化 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="你的azure ai端点", api_key="你的api_key")

if not IN_COLAB:
    nixtla_client = NixtlaClient()

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

fcst_df = nixtla_client.forecast(dask_df, h=12)
fcst_df.compute().head()
unique_id ds TimeGPT
0 BE 2016-12-31 00:00:00 45.190453
1 BE 2016-12-31 01:00:00 43.244446
2 BE 2016-12-31 02:00:00 41.958389
3 BE 2016-12-31 03:00:00 39.796486
4 BE 2016-12-31 04:00:00 39.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_id ds cutoff TimeGPT
0 BE 2016-12-30 04:00:00 2016-12-30 03:00:00 39.375439
1 BE 2016-12-30 05:00:00 2016-12-30 03:00:00 40.039215
2 BE 2016-12-30 06:00:00 2016-12-30 03:00:00 43.455849
3 BE 2016-12-30 07:00:00 2016-12-30 03:00:00 47.716408
4 BE 2016-12-30 08:00:00 2016-12-30 03:00:00 50.31665

您还可以在 Dask 之上使用 TimeGPT 处理外生变量。为此,请参考 外生变量 教程。请记住,不要使用 pandas DataFrame,而是需要使用 Dask DataFrame。

Give us a ⭐ on Github