TimeGPT 支持分类变量,我们可以使用 SpecialDates 创建它们。

import pandas as pd
import datetime
from nixtla import NixtlaClient
from nixtla.date_features import SpecialDates
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")

# Read the data
df = pd.read_csv("https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv")

# Create categorical variables to label Christmas and summer vacations
categories_dates = SpecialDates(
    special_dates={
        'christmas_vacations': [datetime.date(year, 12, 1) for year in range(1949, 1960 + 1)],
        'summer_vacations': [datetime.date(year, month, 1) for year in range(1949, 1960 + 1) for month in (6, 7)]
    }
)

dates = pd.date_range('1949-01-01', '1960-12-01', freq='MS')

categories_df = categories_dates(dates).reset_index(drop=True)

# Merge with the dataset
cat_df = pd.concat([df, categories_df], axis=1)

# Forecast
forecast_df = nixtla_client.forecast(
    df=cat_df, 
    h=24,
    target_col='value',
    time_col='timestamp'
)
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:You did not provide X_df. Exogenous variables in df are ignored. To surpress this warning, please add X_df with exogenous variables: christmas_vacations, summer_vacations
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Restricting input...
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...

📘 Azure AI 中可用的模型

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

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

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

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

有关使用分类变量进行预测的详细指南,请阅读我们关于分类变量的深入教程。