如果您的数据集缺少外部变量,可以添加日期特征来为模型提供信息,以便进行历史异常检测。使用 date_features 参数。将其设置为 True 可提取所有可能的特征,或传入要包含的特定特征列表。

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 data
df = pd.read_csv('https://datasets-nixtla.s3.amazonaws.com/peyton-manning.csv')

# Add date features for anomaly detection
# Here, we use date features at the month and year levels
anomalies_df_x = nixtla_client.detect_anomalies(
    df,
    freq='D', 
    date_features=['month', 'year'],
    date_features_to_one_hot=True,
    level=99.99,
)

# Plot anomalies
nixtla_client.plot(df, anomalies_df_x)
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Querying model metadata...
INFO:nixtla.nixtla_client:Using the following exogenous features: ['month_1.0', 'month_2.0', 'month_3.0', 'month_4.0', 'month_5.0', 'month_6.0', 'month_7.0', 'month_8.0', 'month_9.0', 'month_10.0', 'month_11.0', 'month_12.0', 'year_2007.0', 'year_2008.0', 'year_2009.0', 'year_2010.0', 'year_2011.0', 'year_2012.0', 'year_2013.0', 'year_2014.0', 'year_2015.0', 'year_2016.0']
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 的详细信息,请参阅本教程

有关更多详细信息,请查看我们关于异常检测的深入教程。