次の方法で共有


JavaScript 用 Azure Anomaly Detector クライアント ライブラリ - バージョン 3.0.0-beta.5

Azure AnomalyDetector API を使用すると、機械学習を使用して時系列データの異常を監視および検出できます。

主要リンク:

主要な概念

には AnomalyDetectorClient 、異常検出の方法が用意されています。

  • detectEntireSeries - データ セット全体の異常を検出します
  • detectLastPoint - 最新のデータ ポイントの異常を検出します
  • detectChangePoint - すべての系列ポイントの変更ポイント スコアを評価します

はじめに

現在サポートされている環境

詳細については、Microsoft のサポート ポリシーを参照してください。

前提条件

Azure CLI を使用する場合は、 と <your-resource-name> を独自の一意の名前に置き換えます<your-resource-group-name>

az cognitiveservices account create --kind AnomalyDetector --resource-group <your-resource-group-name> --name <your-resource-name>

@azure/ai-anomaly-detector パッケージのインストール

を使用して、JavaScript 用の Azure Anomaly Detector クライアント ライブラリをnpmインストールします。

npm install @azure/ai-anomaly-detector

AnomalyDetectorClient を作成して認証する

Anomaly Detector API にアクセスするためのクライアント オブジェクトを作成するには、Anomaly Detector リソースの と credentialが必要endpointです。 Anomaly Detector クライアントは、Azure Active Directory 資格情報または API キー資格情報のいずれかを使用して認証できます。

Anomaly Detector リソースのエンドポイントは、Azure Portal でメニューの [リソース管理] をクリックKeys and Endpointするか、次の Azure CLI スニペットを使用して確認できます。

az cognitiveservices account show --name <your-resource-name> --resource-group <your-resource-group-name> --query "endpoint"

API キーの使用

Azure Portal を使用してAnomaly Detector リソースを参照し、[リソース管理] の下をクリックして API キーをKeys and Endpoint取得するか、次の Azure CLI スニペットを使用します。

メモ: API キーは、"サブスクリプション キー" または "サブスクリプション API キー" と呼ばれる場合があります。

az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>

API キーとエンドポイントを取得したら、 クラスを AzureKeyCredential 使用して、次のようにクライアントを認証できます。

const { AnomalyDetectorClient, AzureKeyCredential } = require("@azure/ai-anomaly-detector");

const client = new AnomalyDetectorClient("<endpoint>", new AzureKeyCredential("<API key>"));

Azure Active Directory 資格情報の使用

クライアント API キー認証はほとんどの例で使用されますが、[Azure Identity library] を使用して Azure Active Directory で認証することもできます。 下に示した DefaultAzureCredential プロバイダーか、Azure SDK で提供されている他の資格情報プロバイダーを使用するには、@azure/identity パッケージをインストールしてください。

npm install @azure/identity

また、新しい AAD アプリケーションを登録し、サービス プリンシパルにロールを割り当てること"Cognitive Services User"で、Anomaly Detectorへのアクセス権を付与する必要があります (注: などの"Owner"他のロールは必要なアクセス許可を付与せず、例とサンプル コードを実行するだけで"Cognitive Services User"十分です)。

AAD アプリケーションのクライアント ID、テナント ID、およびクライアント シークレットの値を、環境変数 AZURE_CLIENT_IDAZURE_TENANT_ID、、AZURE_CLIENT_SECRET、 として設定します。

const { AnomalyDetectorClient } = require("@azure/ai-anomaly-detector");
const { DefaultAzureCredential } = require("@azure/identity");

const client = new AnomalyDetectorClient("<endpoint>", new DefaultAzureCredential());

変更ポイントの検出

このサンプルでは、系列全体の変更ポイントを検出する方法を示します。

const { AnomalyDetectorClient, TimeGranularity } = require("@azure/ai-anomaly-detector");
const { AzureKeyCredential } = require("@azure/core-auth");

// You will need to set this environment variables in .env file or edit the following values
const apiKey = process.env["API_KEY"] || "";
const endpoint = process.env["ENDPOINT"] || "";

async function main() {
  // create client
  const client = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(apiKey));

  // construct request
  const request = {
    series: [
      { timestamp: new Date("2018-03-01T00:00:00Z"), value: 32858923 },
      { timestamp: new Date("2018-03-02T00:00:00Z"), value: 29615278 },
      { timestamp: new Date("2018-03-03T00:00:00Z"), value: 22839355 },
      { timestamp: new Date("2018-03-04T00:00:00Z"), value: 25948736 },
      { timestamp: new Date("2018-03-05T00:00:00Z"), value: 34139159 },
      { timestamp: new Date("2018-03-06T00:00:00Z"), value: 33843985 },
      { timestamp: new Date("2018-03-07T00:00:00Z"), value: 33637661 },
      { timestamp: new Date("2018-03-08T00:00:00Z"), value: 32627350 },
      { timestamp: new Date("2018-03-09T00:00:00Z"), value: 29881076 },
      { timestamp: new Date("2018-03-10T00:00:00Z"), value: 22681575 },
      { timestamp: new Date("2018-03-11T00:00:00Z"), value: 24629393 },
      { timestamp: new Date("2018-03-12T00:00:00Z"), value: 34010679 },
      { timestamp: new Date("2018-03-13T00:00:00Z"), value: 33893888 },
      { timestamp: new Date("2018-03-14T00:00:00Z"), value: 33760076 },
      { timestamp: new Date("2018-03-15T00:00:00Z"), value: 33093515 }
    ],
    granularity: TimeGranularity.daily
  };

  // get change point detect results
  const result = await client.detectChangePoint(request);
  const isChangePointDetected = result.isChangePoint.some((changePoint) => changePoint);

  if (isChangePointDetected) {
    console.log("Change points were detected from the series at index:");
    result.isChangePoint.forEach((changePoint, index) => {
      if (changePoint === true) {
        console.log(index);
      }
    });
  } else {
    console.log("There is no change point detected from the series.");
  }
  // output:
  // Change points were detected from the series at index:
  // 9
}

main().catch((err) => {
  console.error("The sample encountered an error:", err);
});

その他のサンプル については、こちらを参照してください

トラブルシューティング

ログ記録

ログの記録を有効にすると、エラーに関する有用な情報を明らかにするのに役立つ場合があります。 HTTP 要求と応答のログを表示するには、環境変数 AZURE_LOG_LEVELinfo に設定します。 または、@azure/loggersetLogLevel を呼び出して、実行時にログ記録を有効にすることもできます。

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

ログを有効にする方法の詳細については、@azure/logger パッケージに関するドキュメントを参照してください。

次のステップ

このライブラリの使用方法の詳細な例については、 samples ディレクトリを参照してください。

共同作成

このプロジェクトでは、共同作成と提案を歓迎しています。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。 詳細については、 https://cla.microsoft.com を参照してください。

pull request を送信すると、CLA を提供して PR (ラベル、コメントなど) を適宜装飾する必要があるかどうかを CLA ボットが自動的に決定します。 ボットによって提供される手順にそのまま従ってください。 この操作は、Microsoft の CLA を使用するすべてのリポジトリについて、1 回だけ行う必要があります。

このプロジェクトでは、Microsoft オープン ソースの倫理規定を採用しています。 詳しくは、「Code of Conduct FAQ (倫理規定についてよくある質問)」を参照するか、opencode@microsoft.com 宛てに質問またはコメントをお送りください。

このライブラリに投稿する場合、コードをビルドしてテストする方法の詳細については、投稿ガイドを参照してください。