Add date features
How to enrich datasets with date features for historical anomaly detection.
Adding date features is a powerful way to enrich your dataset when no exogenous variables are available. These features help guide the historical anomaly detection model in recognizing seasonal and temporal patterns.
Step 1: Install and Import
First, ensure the nixtla Python client is installed and then import the required libraries:
Step 2: Initialize the Nixtla Client
Create an instance of NixtlaClient
. By default, it uses your NIXTLA_API_KEY
environment variable. You can also provide a key directly:
When using an Azure AI endpoint, specify the base_url
argument:
Step 3: Add Date Features for Anomaly Detection
Read your dataset, and add time-based features to guide the anomaly detection model. You can enable all possible features by setting date_features=True
, or specify certain features to focus on:
This approach extracts monthly and yearly patterns, then converts them to one-hot-encoded features, creating multiple exogenous variables for the anomaly detection model.
Step 4: Review Output Logs & Visualize
When you run the detection, logs inform you about which exogenous features were used:
Next, visualize the anomalies:
Anomaly plot showing flagged points based on date features.
And plot the weight contributions of the date features:
Bar chart indicating which date features contribute most to anomaly detection.
Key Concept: Date Features
Date features help the model recognize seasonal patterns, holiday effects, or recurring fluctuations. Examples include day_of_week
, month
, year
, and more.
Learn More
For a deeper dive into anomaly detection, refer to the comprehensive anomaly detection tutorial.