azure-ai-anomalydetector-java について
Azure AI Anomaly Detector を使ったアプリケーション開発向けの Java スキルと SDK リファレンスです。単変量・多変量の時系列検出を扱い、新規クラウド導入ではなく、過去の利用や移行の参考として位置付けています。
主な機能
- 01単変量の一括検出
- 02最新点の異常検出
- 03変化点検出
- 04多変量モデルの学習と推論
- 05モデル一覧と状態確認
活用シーン
- 01過去の時系列データの分析
- 02新しいデータ値の異常確認
- 03時系列トレンドの変化の特定
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-ai-anomalydetector-javaインストール後、Claude Code で/azure-ai-anomalydetector-javaを入力して読み込まれたか確認します。
詳細
Azure AI Anomaly Detector SDK for Java
Azure AI Anomaly Detector SDK for Java を使用して、異常検出アプリケーションを構築します。
サービスの状態
Microsoft の廃止に関するガイダンスによると、新しいリソースの作成は 2023 年 9 月に無効化され、サービスは 2026 年 10 月に廃止される予定です。これらの SDK の断片は、新しいクラウドへのデプロイ手段ではなく、過去の情報や移行の参考資料として扱ってください。実装前に、実際のリソースのライフサイクルを確認し、文書化されている Fabric/オープンソースの代替手段を評価してください。
コードスニペットには、前後のアプリケーションコードやインポートが含まれておらず、テスト済みの完全なプログラムではありません。非同期のトレーニング/推論では、期限を設けて終端状態をポーリングし、結果にアクセスする前に失敗を処理してください。承認済みのデータ、短期間有効な最小権限のストレージアクセス、非公開のレポートを使用してください。SAS トークンを決して公開しないでください。
インストール
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-anomalydetector</artifactId>
<version>3.0.0-beta.6</version>
</dependency>
クライアントの作成
同期クライアントと非同期クライアント
import com.azure.ai.anomalydetector.AnomalyDetectorClientBuilder;
import com.azure.ai.anomalydetector.MultivariateClient;
import com.azure.ai.anomalydetector.UnivariateClient;
import com.azure.core.credential.AzureKeyCredential;
String endpoint = System.getenv("AZURE_ANOMALY_DETECTOR_ENDPOINT");
String key = System.getenv("AZURE_ANOMALY_DETECTOR_API_KEY");
// Multivariate client for multiple correlated signals
MultivariateClient multivariateClient = new AnomalyDetectorClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildMultivariateClient();
// Univariate client for single variable analysis
UnivariateClient univariateClient = new AnomalyDetectorClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildUnivariateClient();
DefaultAzureCredential を使用する場合
import com.azure.identity.DefaultAzureCredentialBuilder;
MultivariateClient client = new AnomalyDetectorClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(endpoint)
.buildMultivariateClient();
主な概念
単変量異常検出
- バッチ検出:時系列全体を一度に分析します
- ストリーミング検出:最新のデータポイントをリアルタイムで検出します
- 変化点検出:時系列の傾向の変化を検出します
多変量異常検出
- 相関する 300 以上のシグナルから異常を検出します
- 相互相関にはグラフアテンションネットワークを使用します
- 3 段階のプロセスです:トレーニング → 推論 → 結果
基本パターン
単変量バッチ検出
import com.azure.ai.anomalydetector.models.*;
import java.time.OffsetDateTime;
import java.util.List;
List<TimeSeriesPoint> series = List.of(
new TimeSeriesPoint(OffsetDateTime.parse("2023-01-01T00:00:00Z"), 1.0),
new TimeSeriesPoint(OffsetDateTime.parse("2023-01-02T00:00:00Z"), 2.5)
// ... more data points (minimum 12 points required)
);
UnivariateDetectionOptions options = new UnivariateDetectionOptions(series)
.setGranularity(TimeGranularity.DAILY)
.setSensitivity(95);
UnivariateEntireDetectionResult result = univariateClient.detectUnivariateEntireSeries(options);
// Check for anomalies
for (int i = 0; i < result.getIsAnomaly().size(); i++) {
if (result.getIsAnomaly().get(i)) {
System.out.printf("Anomaly detected at index %d with value %.2f%n",
i, series.get(i).getValue());
}
}
単変量の最新データポイント検出(ストリーミング)
UnivariateLastDetectionResult lastResult = univariateClient.detectUnivariateLastPoint(options);
if (lastResult.isAnomaly()) {
System.out.println("Latest point is an anomaly!");
System.out.printf("Expected: %.2f, Upper: %.2f, Lower: %.2f%n",
lastResult.getExpectedValue(),
lastResult.getUpperMargin(),
lastResult.getLowerMargin());
}
変化点検出
UnivariateChangePointDetectionOptions changeOptions =
new UnivariateChangePointDetectionOptions(series, TimeGranularity.DAILY);
UnivariateChangePointDetectionResult changeResult =
univariateClient.detectUnivariateChangePoint(changeOptions);
for (int i = 0; i < changeResult.getIsChangePoint().size(); i++) {
if (changeResult.getIsChangePoint().get(i)) {
System.out.printf("Change point at index %d with confidence %.2f%n",
i, changeResult.getConfidenceScores().get(i));
}
}
多変量モデルのトレーニング
import com.azure.ai.anomalydetector.models.*;
import com.azure.core.util.polling.SyncPoller;
// Prepare training request with blob storage data
ModelInfo modelInfo = new ModelInfo()
.setDataSource("https://storage.blob.core.windows.net/container/data.zip?sasToken")
.setStartTime(OffsetDateTime.parse("2023-01-01T00:00:00Z"))
.setEndTime(OffsetDateTime.parse("2023-06-01T00:00:00Z"))
.setSlidingWindow(200)
.setDisplayName("MyMultivariateModel");
// Train model (long-running operation)
AnomalyDetectionModel trainedModel = multivariateClient.trainMultivariateModel(modelInfo);
String modelId = trainedModel.getModelId();
System.out.println("Model ID: " + modelId);
// Check training status
AnomalyDetectionModel model = multivariateClient.getMultivariateModel(modelId);
System.out.println("Status: " + model.getModelInfo().getStatus());
多変量バッチ推論
MultivariateBatchDetectionOptions detectionOptions = new MultivariateBatchDetectionOptions()
.setDataSource("https://storage.blob.core.windows.net/container/inference-data.zip?sasToken")
.setStartTime(OffsetDateTime.parse("2023-07-01T00:00:00Z"))
.setEndTime(OffsetDateTime.parse("2023-07-31T00:00:00Z"))
.setTopContributorCount(10);
MultivariateDetectionResult detectionResult =
multivariateClient.detectMultivariateBatchAnomaly(modelId, detectionOptions);
String resultId = detectionResult.getResultId();
// Poll for results
MultivariateDetectionResult result = multivariateClient.getBatchDetectionResult(resultId);
for (AnomalyState state : result.getResults()) {
if (state.getValue().isAnomaly()) {
System.out.printf("Anomaly at %s, severity: %.2f%n",
state.getTimestamp(),
state.getValue().getSeverity());
}
}
多変量の最新データポイント検出
MultivariateLastDetectionOptions lastOptions = new MultivariateLastDetectionOptions()
.setVariables(List.of(
new VariableValues("variable1", List.of("timestamp1"), List.of(1.0f)),
new VariableValues("variable2", List.of("timestamp1"), List.of(2.5f))
))
.setTopContributorCount(5);
MultivariateLastDetectionResult lastResult =
multivariateClient.detectMultivariateLastAnomaly(modelId, lastOptions);
if (lastResult.getValue().isAnomaly()) {
System.out.println("Anomaly detected!");
// Check contributing variables
for (AnomalyContributor contributor : lastResult.getValue().getInterpretation()) {
System.out.printf("Variable: %s, Contribution: %.2f%n",
contributor.getVariable(),
contributor.getContributionScore());
}
}
モデルの管理
// List all models
PagedIterable<AnomalyDetectionModel> models = multivariateClient.listMultivariateModels();
for (AnomalyDetectionModel m : models) {
System.out.printf("Model: %s, Status: %s%n",
m.getModelId(),
m.getModelInfo().getStatus());
}
// Delete a model
multivariateClient.deleteMultivariateModel(modelId);
エラー処理
import com.azure.core.exception.HttpResponseException;
try {
univariateClient.detectUnivariateEntireSeries(options);
} catch (HttpResponseException e) {
System.out.println("Status code: " + e.getResponse().getStatusCode());
System.out.println("Error: " + e.getMessage());
}
環境変数
AZURE_ANOMALY_DETECTOR_ENDPOINT=https://<resource>.cognitiveservices.azure.com/
AZURE_ANOMALY_DETECTOR_API_KEY=<your-api-key>
ベストプラクティス
- 最小データポイント数:単変量検出には少なくとも 12 個のデータポイントが必要です。データが多いほど精度が向上します
- 粒度の整合:
TimeGranularityを実際のデータ頻度に合わせてください - 感度の調整:値を高くする(0~99)と、より多くの異常を検出します
- 多変量トレーニング:パターンの複雑さに応じて、200~1000 のスライディングウィンドウを使用してください
- エラー処理:API エラーには必ず
HttpResponseExceptionを処理してください
トリガーフレーズ
- 「Java で異常を検出」
- 「時系列の異常を検出」
- 「Java で多変量異常を検出」
- 「単変量異常検出」
- 「ストリーミング異常検出」
- 「変化点検出」
- 「Azure AI Anomaly Detector」
使用する場面
このスキルは、概要で説明されているワークフローまたはアクションを実行する場合に適用できます。
制限事項
- このスキルは、タスクが上記の説明範囲に明確に当てはまる場合にのみ使用してください。
- 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
- 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、作業を中断して確認を求めてください。
コメント
Markdown 対応