Announcing new beta release of Azure Anomaly Detector client libraries
Today, we’re excited to announce new beta client libraries for using Azure Anomaly Detector with Python, Java, JavaScript, and .NET. The libraries enable developers to easily integrate the service into their applications. The new libraries provide a simple and intuitive interface for detecting anomalies in time series data and offer a range of customization options to fine-tune the detection algorithms for specific use cases.
Azure Anomaly Detector is a cloud-based service that uses machine learning algorithms to automatically detect anomalies in time series data. The service is designed to help customers identify and respond to anomalies in their data in real-time, enabling them to take proactive action to prevent potential issues. Azure Anomaly Detector can be used in a wide range of scenarios. Examples include:
- Monitoring the performance of IT systems.
- Detecting anomalies in complex system or equipment for predictive maintenance.
- Identifying anomalous trends in business data.
The service is available through the Azure portal and can be easily integrated into existing applications using the Azure Anomaly Detector libraries released today.
Here are the languages and correlated download materials with sample code.
Language | Version | Package | README |
---|---|---|---|
Python | 3.0.0b6 | Download | README |
.NET | 3.0.0-preview.6 | Download | README |
JavaScript | 1.0.0-beta.1 | Download | README |
Java | 3.0.0-beta.5 | Download | README |
Note: If you’re short on time, you can also try with the following notebooks:
Get started with an Anomaly Detector client
To get started, create an Anomaly Detector client. Use that client to operate with different features.
Python
from azure.core.credentials import AzureKeyCredential
from azure.ai.anomalydetector import AnomalyDetectorClient
SUBSCRIPTION_KEY = os.environ["ANOMALY_DETECTOR_API_KEY"]
ANOMALY_DETECTOR_ENDPOINT = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
credential = AzureKeyCredential(SUBSCRIPTION_KEY)
client = AnomalyDetectorClient(endpoint=ANOMALY_DETECTOR_ENDPOINT, credential=credential)
.NET
string apiKey = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_API_KEY");
string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
var credential = new AzureKeyCredential(apiKey);
var client = new AnomalyDetectorClient(new Uri(endpoint), credential);
Java
String apiKey = Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_API_KEY");
String endpoint = Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_ENDPOINT");
AnomalyDetectorClient anomalyDetectorClient =
new AnomalyDetectorClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildClient();
JavaScript
const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
const credential = new AzureKeyCredential(apiKey);
const client = AnomalyDetector(endpoint, credential);
Key features
Univariate Anomaly Detection
The Univariate Anomaly Detection API enables you to monitor and detect abnormalities in your time series data without knowledge of machine learning. The algorithms adapt by automatically identifying and applying the best-fitting models to your data, regardless of industry, scenario, or data volume. The API uses your time series data to determine boundaries for anomaly detection, expected values, and which data points are anomalies.
With the Univariate Anomaly Detection, you can automatically detect anomalies throughout your time series data, or as they occur in real-time.
Examples
The following code samples demonstrate using Univariate Anomaly Detection for batch detection.
Python
from azure.ai.anomalydetector import AnomalyDetectorClient
from azure.core.credentials import AzureKeyCredential
from azure.ai.anomalydetector.models import *
SUBSCRIPTION_KEY = os.environ["ANOMALY_DETECTOR_API_KEY"]
ANOMALY_DETECTOR_ENDPOINT = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
TIME_SERIES_DATA_PATH = os.path.join("sample_data", "request-data.csv")
client = AnomalyDetectorClient(ANOMALY_DETECTOR_ENDPOINT, AzureKeyCredential(SUBSCRIPTION_KEY))
series = []
data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding="utf-8", parse_dates=[0])
for index, row in data_file.iterrows():
series.append(TimeSeriesPoint(timestamp=row[0], value=row[1]))
request = UnivariateDetectionOptions(
series=series,
granularity=TimeGranularity.DAILY,
)
if any(response.is_anomaly):
print("An anomaly was detected at index:")
for i, value in enumerate(response.is_anomaly):
if value:
print(i)
else:
print("No anomalies were detected in the time series.")
.NET
Console.WriteLine("Detecting anomalies in the entire time series.");
try
{
UnivariateEntireDetectionResult result = client.DetectUnivariateEntireSeries(request);
bool hasAnomaly = false;
for (int i = 0; i < request.Series.Count; ++i)
{
if (result.IsAnomaly[i])
{
Console.WriteLine("An anomaly was detected at index: {0}.", i);
hasAnomaly = true;
}
}
if (!hasAnomaly)
{
Console.WriteLine("No anomalies detected in the series.");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine(String.Format("Entire detection failed: {0}", ex.Message));
throw;
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Detection error. {0}", ex.Message));
throw;
}
Java
String apiKey = Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_API_KEY");
String endpoint = Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_ENDPOINT");
AnomalyDetectorClient anomalyDetectorClient =
new AnomalyDetectorClientBuilder()
.credential(new AzureKeyCredential(apiKey))
.endpoint(endpoint)
.buildClient();
Path path = Paths.get("azure-ai-anomalydetector/src/samples/java/sample_data/request-data.csv");
List<String> requestData = Files.readAllLines(path);
List<TimeSeriesPoint> series = requestData.stream()
.map(line -> line.trim())
.filter(line -> line.length() > 0)
.map(line -> line.split(",", 2))
.filter(splits -> splits.length == 2)
.map(splits -> {
TimeSeriesPoint timeSeriesPoint = new TimeSeriesPoint(Float.parseFloat(splits[1]));
timeSeriesPoint.setTimestamp(OffsetDateTime.parse(splits[0]));
return timeSeriesPoint;
})
.collect(Collectors.toList());
System.out.println("Detecting anomalies as a batch...");
UnivariateDetectionOptions request = new UnivariateDetectionOptions(series);
request.setGranularity(TimeGranularity.DAILY);
request.setImputeMode(ImputeMode.AUTO);
UnivariateEntireDetectionResult response = anomalyDetectorClient.detectUnivariateEntireSeries(request);
if (response.getIsAnomaly().contains(true)) {
System.out.println("Anomalies found in the following data positions:");
for (int i = 0; i < request.getSeries().size(); ++i) {
if (response.getIsAnomaly().get(i)) {
System.out.print(i + " ");
}
}
System.out.println();
} else {
System.out.println("No anomalies were found in the series.");
}
JavaScript
const apiKey = process.env["ANOMALY_DETECTOR_API_KEY"] || "";
const endpoint = process.env["ANOMALY_DETECTOR_ENDPOINT"] || "";
const timeSeriesDataPath = "./samples-dev/example-data/request-data.csv";
function read_series_from_file(path: string): Array<TimeSeriesPoint> {
let result = Array<TimeSeriesPoint>();
let input = fs.readFileSync(path).toString();
let parsed = parse(input, { skip_empty_lines: true });
parsed.forEach(function (e: Array<string>) {
result.push({ timestamp: new Date(e[0]), value: Number(e[1]) });
});
return result;
}
export async function main() {
// create client
const credential = new AzureKeyCredential(apiKey);
const client = AnomalyDetector(endpoint, credential);
// construct request
const options: DetectUnivariateEntireSeriesParameters = {
body: {
granularity: "daily",
imputeMode: "auto",
maxAnomalyRatio: 0.25,
sensitivity: 95,
series: read_series_from_file(timeSeriesDataPath),
},
headers: { "Content-Type": "application/json" },
};
// get last detect result
const result = await client.path("/timeseries/entire/detect").post(options);
if (isUnexpected(result)) {
throw result;
}
if (result.body.isAnomaly) {
result.body.isAnomaly.forEach(function (anomaly, index) {
if (anomaly === true) {
console.log(index);
}
});
} else {
console.log("There is no anomaly detected from the series.");
}
Multivariate Anomaly Detection
The Multivariate Anomaly Detection APIs further enable developers by easily integrating advanced AI for detecting anomalies from groups of metrics, without the need for machine learning knowledge or labeled data. Dependencies and inter-correlations between up to 300 different signals are now automatically counted as key factors. This new capability helps you to proactively protect your complex systems such as software applications, servers, factory machines, spacecraft, or even your business, from failures.
With the Multivariate Anomaly Detection, you can automatically detect anomalies throughout your time series data, or as they occur in real-time. There are three processes to use Multivariate Anomaly Detection:
- Training: Use Train Model API to create and train a model, then use Get Model Status API to get the status and model metadata.
- Inference: Use Async Inference to trigger an asynchronous inference process and use Get Inference results API to get detection results on a batch of data. You could also use Sync Inference to trigger a detection on one timestamp every time.
- Other operations: List Model and Delete Model are supported in Multivariate Anomaly Detection model for model management.
Examples
Since Multivariate Anomaly Detection requires training and inference as a complete flow, see the following correlated samples.
Language | Sample code |
---|---|
Python | Sample |
.NET | Sample |
Java | Sample |
JavaScript | Sample |
Learn more
We hope this post offered some insight into the Azure Anomaly Detector libraries. You can send any feedback or suggestions to AnomalyDetector@microsoft.com.
For more information, see the following resources:
- What is Azure Anomaly Detector?
- GitHub repo of Anomaly Detector API and library samples
- Join our Teams Channel for better support and discussions.
0 comments