Introduction

Anomaly detection is crucial for identifying outliers in real-time data streams, such as:

Anomalies can indicate security threats, system failures, fraud, or operational inefficiencies. Without an automated detection mechanism, businesses may struggle to catch critical issues in real time.

Azure Cognitive Services Anomaly Detector enables developers to easily integrate anomaly detection capabilities into applications without requiring deep expertise in machine learning. The service handles various types of time-series data, using advanced statistical techniques to differentiate normal and anomalous patterns.

In this guide, we will walk through:

Step 1. Setting Up the Anomaly Detector API


Prerequisites

Before you begin, ensure you have:
✔️ An Azure subscription
✔️ An Anomaly Detector resource created in the Azure Portal
✔️ Python or another programming language that supports HTTP requests

Creating an Anomaly Detector Resource

1️⃣ Sign in to the Azure Portal
2️⃣ Search for "Anomaly Detector" in the marketplace
3️⃣ Click "Create", then select:

For detailed steps, refer to the Azure Anomaly Detector documentation

Step 2. Sending Data to the API

To detect anomalies, send time-series data to the API. The data must:
✔️ Contain at least 12 data points
✔️ Be structured as a list of timestamps with numerical values
✔️ Maintain a consistent interval between data points to improve accuracy

For instance, in IoT monitoring, sensor data collected at fixed intervals can be sent to the API for anomaly detection. Similarly, in financial transactions, recorded amounts over time can be analyzed for fraud detection.

Sample Code. Sending Data to Anomaly Detector

import requests
import json

# Replace with your Anomaly Detector resource details
API_KEY = "<your_api_key>"
ENDPOINT = "<your_endpoint>/anomalydetector/v1.0/timeseries/entire"

headers = {
    "Ocp-Apim-Subscription-Key": API_KEY,
    "Content-Type": "application/json"
}

data = {
    "series": [
        {"timestamp": "2024-01-01T00:00:00Z", "value": 10.0},
        {"timestamp": "2024-01-02T00:00:00Z", "value": 15.0},
        {"timestamp": "2024-01-03T00:00:00Z", "value": 30.0},
        {"timestamp": "2024-01-04T00:00:00Z", "value": 500.0},  # Anomaly
        {"timestamp": "2024-01-05T00:00:00Z", "value": 20.0}
    ],
    "granularity": "daily"
}

response = requests.post(ENDPOINT, headers=headers, json=data)
print(response.json())

For full API details, refer to the Anomaly Detector API Reference

Step 3. Processing API Responses

The API returns a response indicating whether each data point is an anomaly. It also provides expected values and confidence scores, which help users understand the anomaly’s significance.

Example Response

Import JSON

📌 This response suggests an anomaly occurred at the fourth data point, where the observed value deviated significantly from the expected trend.

Extracting Anomalies in Python

Extracting Anomalies in Python

Detected Anomalies

The expectedValues, upperMargins, and lowerMargins provide further insights into detected anomalies. Developers can use these threshold-based alerting systems to automate responses.

Step 4. Integrating Alerts for Anomalies

Once anomalies are detected, trigger alerts using Azure services like:

Use Case: Industrial IoT Monitoring

Sending an Alert via Email (Using Azure Logic Apps)

For more on automation, visit Azure Logic Apps Documentation

Conclusion

In this guide, we covered:

Azure Cognitive Services Anomaly Detector simplifies real-time anomaly detection, making it ideal for:

By leveraging automated alerts and response mechanisms, businesses can improve operational efficiency and reduce risk.

For further learning, visit Azure Anomaly Detector Documentation.