要使用外生变量检测异常,请加载一个以这些外生特征作为列的数据集,并使用相同的 detect_anomalies 方法。

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

# Read the dataset
# The dataset has exogenous features in its columns
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-with-ex-vars.csv')

# Detect anomalies
anomalies_df = nixtla_client.detect_anomalies(
    df=df,
    time_col='ds',
    target_col='y',
)

# Plot anomalies
nixtla_client.plot(df, anomalies_df)
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Inferred freq: H
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Querying model metadata...
INFO:nixtla.nixtla_client:Using the following exogenous features: ['Exogenous1', 'Exogenous2', 'day_0', 'day_1', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6']
INFO:nixtla.nixtla_client:Calling Anomaly Detector Endpoint...

# Plot weights of date features
nixtla_client.weights_x.plot.barh(x='features', y='weights')

📘 Azure AI 中可用的模型

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

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

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

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

阅读我们关于异常检测的详细指南以获取更多信息。