Share via


JavaScript용 Azure Anomaly Detector 클라이언트 라이브러리 - 버전 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용 Azure Anomaly Detector 클라이언트 라이브러리를 npm설치합니다.

npm install @azure/ai-anomaly-detector

AnomalyDetectorClient 만들기 및 인증

Anomaly Detector API에 액세스하는 클라이언트 개체를 만들려면 Anomaly Detector 리소스 credential및 의 가 필요합니다endpoint. Anomaly Detector 클라이언트는 Azure Active Directory 자격 증명 또는 API 키 자격 증명을 사용하여 인증할 수 있습니다.

메뉴에서 리소스 관리를 클릭 Keys and Endpoint 하거나 아래 Azure CLI 조각을 사용하여 Azure Portal에서 Anomaly Detector 리소스에 대한 엔드포인트를 찾을 수 있습니다.

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

API 키 사용

Azure Portal을 사용하여 리소스 관리를 클릭하여 Keys and Endpoint Anomaly Detector 리소스를 찾아 API 키를 검색하거나 아래 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 ID 라이브러리]를 사용하여 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_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 package docs를 참조하세요.

다음 단계

이 라이브러리를 사용하는 방법에 대한 자세한 예제는 샘플 디렉터리를 참조하세요.

참여

이 프로젝트에 대한 기여와 제안을 환영합니다. 대부분의 경우 기여하려면 권한을 부여하며 실제로 기여를 사용할 권한을 당사에 부여한다고 선언하는 CLA(기여자 라이선스 계약)에 동의해야 합니다. 자세한 내용은 https://cla.microsoft.com 을 참조하세요.

끌어오기 요청을 제출하면 CLA-bot은 CLA를 제공하고 PR을 적절하게 데코레이팅해야 하는지 여부를 자동으로 결정합니다(예: 레이블, 설명). 봇에서 제공하는 지침을 따르기만 하면 됩니다. 이 작업은 CLA를 사용하여 모든 리포지토리에서 한 번만 수행하면 됩니다.

이 프로젝트에는 Microsoft Open Source Code of Conduct(Microsoft 오픈 소스 준수 사항)가 적용됩니다. 자세한 내용은 Code of Conduct FAQ(규정 FAQ)를 참조하세요. 또는 추가 질문이나 의견은 opencode@microsoft.com으로 문의하세요.

이 라이브러리에 기여하려면 기여 가이드 를 참조하여 코드를 빌드하고 테스트하는 방법에 대해 자세히 알아보세요.