Run TimeGPT distributedly on top of Dask

Dask is an open source parallel computing library for Python. In this guide, we will explain how to use TimeGPT on top of Dask.

Outline:

  1. Installation

  2. Load Your Data

  3. Import Dask

  4. Use TimeGPT on Dask

1. Installation

Install Dask through Fugue. Fugue provides an easy-to-use interface for distributed computing that lets users execute Python code on top of several distributed computing frameworks, including Dask.

Note

You can install fugue with pip:

pip install fugue[dask]

If executing on a distributed Dask cluster, ensure that the nixtla library is installed across all the workers.

2. Load Data

You can load your data as a pandas DataFrame. In this tutorial, we will use a dataset that contains hourly electricity prices from different markets.

import pandas as pd 

df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short.csv') 
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. Import Dask

Import Dask and convert the pandas DataFrame to a Dask DataFrame.

import dask.dataframe as dd
dask_df = dd.from_pandas(df, npartitions=2)
dask_df 
Dask DataFrame Structure:
unique_iddsy
npartitions=2
0stringstringfloat64
4200.........
8399.........
Dask Name: to_pyarrow_string, 2 graph layers

4. Use TimeGPT on Dask

Using TimeGPT on top of Dask is almost identical to the non-distributed case. The only difference is that you need to use a Dask DataFrame, which we already defined in the previous step.

First, instantiate the NixtlaClient class.

from nixtla import NixtlaClient
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_provided_by_nixtla'
)

Then use any method from the NixtlaClient class such as forecast or cross_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
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

You can also use exogenous variables with TimeGPT on top of Dask. To do this, please refer to the Exogenous Variables tutorial. Just keep in mind that instead of using a pandas DataFrame, you need to use a Dask DataFrame instead.