...

/

Univariate Anomaly Detection - Change Points and Best Practices

Univariate Anomaly Detection - Change Points and Best Practices

Learn to build and perform anomaly detection on univariate data using the Azure Anomaly Detectors.

To recap, in the previous lesson we discussed the two approaches to detect the anomalies in time-series data. In this lesson, we’ll explore the third scenario—detecting change points in the time-series data. We’ll take the same sample data set of the stock prices with their timestamp.

Detecting change points in the time-series data

Let’s explore how change points are detected in the time-series data.

Press + to interact
Please provide values for the following:
anomaly_detector_key
Not Specified...
anomaly_detector_endpoint
Not Specified...
main.py
univariate_data.csv
from azure.ai.anomalydetector import AnomalyDetectorClient
from azure.ai.anomalydetector.models import DetectRequest, TimeSeriesPoint, TimeGranularity
from azure.core.credentials import AzureKeyCredential
import pandas as pd
anomaly_detector_client = AnomalyDetectorClient(
AzureKeyCredential(anomaly_detector_key),
anomaly_detector_endpoint
)
series_data = []
data_file = pd.read_csv("univariate_data.csv", header = None, parse_dates = [0])
for index, row in data_file.iterrows():
series_data.append(TimeSeriesPoint(timestamp=row[0], value=row[1]))
request = DetectRequest(series = series_data, granularity = TimeGranularity.daily, sensitivity = 97)
try:
response = anomaly_detector_client.detect_change_point(request)
except Exception as e:
print(e)
change_point_index = []
for index, change_point_found in enumerate(response.is_change_point):
if change_point_found:
change_point_index.append(index)
if len(change_point_index) > 0:
for i in range(len(change_point_index)):
print("Change point at index: ", change_point_index[i])
print("Stock Price before: $", data_file.iloc[i - 1][1])
print("Original Stock Price: $", data_file.iloc[i][1])
print("Stock Price after: $", data_file.iloc[i + 1][1])
print()
else:
print('No anomalies were detected in the time series.')

Explanation:

  • The code is almost the same as the code we discussed in the
...