Overview

Detecting anomalies in time series data can be enhanced by using additional external (or exogenous) features. Here, you’ll learn how to include exogenous variables in your anomaly detection workflow using Nixtla.

Key Benefits

  • Leverage additional context from exogenous features

  • Boost anomaly detection accuracy

  • Flexible usage via Azure AI or the public API

Including relevant exogenous variables can greatly improve anomaly detection, especially for time series influenced by external factors such as weather or market indicators.

1

Load Dependencies

Use the following code to import the necessary libraries and create a Nixtla client instance:

Nixtla Client Initialization
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'
)
2

(Optional) Configure Azure AI

If you want to use an Azure AI endpoint, set the base URL and optional Azure-specific parameters:

You can configure an Azure AI endpoint by specifying the base_url and your api_key.

nixtla_client = NixtlaClient(
    base_url="your azure ai endpoint",
    api_key="your api_key"
)
3

Load and Inspect Data

Read and inspect your dataset, ensuring that it contains the exogenous features as additional columns:

Load Dataset
# Read the dataset
df = pd.read_csv(
    "https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-with-ex-vars.csv"
)
df.head()
4

Detect Anomalies with Exogenous Features

Use the detect_anomalies method to identify anomalies, automatically detecting which columns are exogenous features:

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

By default, the method will infer freq (data frequency) and exogenous columns automatically.

5

Visualize Anomalies

Plot Anomalies
nixtla_client.plot(df, anomalies_df)

Detected anomalies in time series with exogenous variables

6

Inspect Model Weights (Optional)

View the relative weights of the exogenous features to understand their impact:

Plot Model Weights
nixtla_client.weights_x.plot.barh(
    x='features',
    y='weights'
)

Weights of exogenous date features

For more detailed guidance on anomaly detection, including best practices and troubleshooting tips, see the Anomaly Detection Guide

Continue exploring with the NixtlaClient methods for more advanced configurations and optimizations.