Share via


適用于 JavaScript 的 Azure 異常偵測程式用戶端程式庫 - 3.0.0-Beta.5 版

Azure AnomalyDetector API 可讓您使用機器學習來監視和偵測時間序列資料中的異常狀況。

重要連結:

重要概念

提供 AnomalyDetectorClient 異常偵測的方法:

  • detectEntireSeries - 偵測整個資料集上的異常
  • detectLastPoint - 偵測最新資料點中的異常狀況
  • detectChangePoint - 評估每個數列點的變更點分數

開始使用

目前支援的環境

如需詳細資訊,請參閱我們的支援原則

必要條件

如果您使用 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 npm 的 Azure 異常偵測程式 用戶端程式庫:

npm install @azure/ai-anomaly-detector

建立和驗證 AnomalyDetectorClient

若要建立用戶端物件來存取異常偵測程式 API,您需要 endpoint 異常偵測程式資源和 credential 。 異常偵測程式用戶端可以使用 Azure Active Directory 認證或 API 金鑰認證來進行驗證。

您可以在 Azure 入口網站中按一下 Keys and Endpoint [資源管理] 底下,或使用下列 Azure CLI 程式碼片段,在 Azure 入口網站中找到異常偵測程式資源的端點:

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

使用 API 金鑰

使用 Azure 入口網站流覽至您的異常偵測程式資源,然後按一下 Keys and Endpoint [資源管理] 底下,或使用下列 Azure CLI 程式碼片段來擷取 API 金鑰:

注意: 有時 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 身分識別程式庫] 向 Azure Active Directory 進行驗證。 若要使用如下所示的 DefaultAzureCredential 提供者,或其他隨附于 Azure SDK 的認證提供者,請安裝 @azure/identity 套件:

npm install @azure/identity

您也需要註冊新的 AAD 應用程式,並將角色指派給服務 (主體,以授 "Cognitive Services User" 與存取權給異常偵測程式:其他角色,例如 "Owner" 不會授與必要的許可權,只會 "Cognitive Services User" 足以執行範例和範例程式碼) 。

將 AAD 應用程式的用戶端識別碼、租使用者識別碼和用戶端密碼的值設定為環境變數: AZURE_CLIENT_ID 、、 AZURE_TENANT_IDAZURE_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_LEVEL 環境變數設定為 info。 或者,您可以在 @azure/logger 中呼叫 setLogLevel,以在執行階段啟用記錄:

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

setLogLevel("info");

如需如何啟用記錄的詳細指示,可參閱 @azure/logger 套件文件

後續步驟

如需如何使用此程式庫的詳細範例,請參閱 範例 目錄。

參與

此專案歡迎參與和提供建議。 大部分的參與都要求您同意「參與者授權合約 (CLA)」,宣告您有權且確實授與我們使用投稿的權利。 如需詳細資料,請前往 https://cla.microsoft.com

當您提交提取要求時,CLA Bot 會自動判斷您是否需要提供 CLA,並適當地裝飾 PR (例如標籤、註解)。 請遵循 bot 提供的指示。 您只需要使用我們的 CLA 在所有存放庫上執行此動作一次。

此專案採用 Microsoft Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題集,如有任何其他問題或意見請連絡 opencode@microsoft.com

如果您希望向此程式庫投稿,請參閱投稿指南,深入瞭解如何組建與測試程式碼。