使用外部特征进行建模,您有两种选择: 1. 使用历史外部变量:将这些变量包含在传递给 forecast 方法的 DataFrame 中。 2. 使用未来外部变量:将这些变量包含在传递给 forecast 方法的 DataFrame 中,并使用 X_df 参数提供这些外部特征在预测期内的未来值。

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

1. 历史外部变量

# Read data
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-with-ex-vars.csv')

# Forecast
forecast_df = nixtla_client.forecast(
    df=df, 
    h=24,
    id_col='unique_id',
    target_col='y',
    time_col='ds',
    # Add the columns of `df` that will be considered as historical
    hist_exog_list=['Exogenous1', 'Exogenous2', 'day_0', 'day_1', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6']
)

2. 未来外部变量

# Read data
import numpy as np
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-with-ex-vars.csv')

# Load the future value of exogenous variables over the forecast horizon
future_ex_vars_df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-future-ex-vars.csv')

# Forecast
forecast_df = nixtla_client.forecast(
    df=df, 
    X_df=future_ex_vars_df, 
    h=24,
    id_col='unique_id',
    target_col='y',
    time_col='ds'
)

3. 历史和未来外部变量

# Read data
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-with-ex-vars.csv')

# Load the future value of exogenous variables over the forecast horizon
future_ex_vars_df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-future-ex-vars.csv')

# We will only use 2 exogenous of future_ex_vars_df
future_ex_vars_df = future_ex_vars_df[["unique_id", "ds", "Exogenous1", "Exogenous2"]]
# To pass historical exogenous variables, we need to add the list of columns
# in the `hist_exog_list` as follows.

# Forecast
forecast_df = nixtla_client.forecast(
    df=df, 
    X_df=future_ex_vars_df, 
    h=24,
    id_col='unique_id',
    target_col='y',
    time_col='ds',
    # Add the columns of `df` that will be considered as historical
    hist_exog_list=['day_0', 'day_1', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6']
)

📘 Azure AI 中可用的模型

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

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

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

默认情况下使用 timegpt-1。有关使用 timegpt-1-long-horizon 的详细信息,请参阅此教程

有关将外部特征与 TimeGPT 一起使用的更多详细信息,请阅读我们关于外部变量类别变量的深入教程。