快速入门:使用异常检测器客户端库Quickstart: Use the Anomaly Detector client library

开始使用适用于 .NET 的异常检测器客户端库。Get started with the Anomaly Detector client library for .NET. 请按照以下步骤操作,以使用服务提供的算法安装软件包。Follow these steps to install the package start using the algorithms provided by the service. 通过异常检测器服务,可以对时序数据自动使用最佳适配模型,从而查找器其中的异常,不限行业、场景或数据量。The Anomaly Detector service enables you to find abnormalities in your time series data by automatically using the best-fitting models on it, regardless of industry, scenario, or data volume.

使用适用于 .NET 的异常检测器客户端库,可实现以下操作:Use the Anomaly Detector client library for .NET to:

  • 以批请求的形式检测整个时序数据集中的异常Detect anomalies throughout your time series data set, as a batch request
  • 在时序中检测最新数据点的异常状态Detect the anomaly status of the latest data point in your time series
  • 检测数据集中的趋势更改点。Detect trend change points in your data set.

库参考文档 | 库源代码 | 包 (NuGet) | 在 GitHub 上查找代码Library reference documentation | Library source code | Package (NuGet) | Find the code on GitHub

先决条件Prerequisites

  • Azure 订阅 - 创建试用订阅Azure subscription - Create one for trial
  • .NET Core 的当前版本The current version of .NET Core
  • 拥有 Azure 订阅后,可在 Azure 门户中创建异常检测器资源来获取密钥和终结点。Once you have your Azure subscription, create an Anomaly Detector resource in the Azure portal to get your key and endpoint. 等待其部署并单击“转到资源”按钮。Wait for it to deploy and click the Go to resource button.
    • 需要从创建的资源获取密钥和终结点,以便将应用程序连接到异常检测器 API。You will need the key and endpoint from the resource you create to connect your application to the Anomaly Detector API. 你稍后会在快速入门中将密钥和终结点粘贴到下方的代码中。You'll paste your key and endpoint into the code below later in the quickstart. 可以使用免费定价层 (F0) 试用该服务,然后再升级到付费层进行生产。You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.

设置Setting up

创建环境变量Create an environment variable

备注

在 2019 年 7 月 1 日之后创建的非试用资源的终结点使用如下所示的自定义子域格式。The endpoints for non-trial resources created after July 1, 2019 use the custom subdomain format shown below. 有关详细信息和区域终结点的完整列表,请参阅认知服务的自定义子域名For more information and a complete list of regional endpoints, see Custom subdomain names for Cognitive Services.

从创建的资源使用密钥和终结点,创建两个用于身份验证的环境变量:Using your key and endpoint from the resource you created, create two environment variables for authentication:

  • ANOMALY_DETECTOR_KEY - 用于验证请求的资源密钥。ANOMALY_DETECTOR_KEY - The resource key for authenticating your requests.
  • ANOMALY_DETECTOR_ENDPOINT - 用于发送 API 请求的资源终结点。ANOMALY_DETECTOR_ENDPOINT - The resource endpoint for sending API requests. 它将如下所示:It will look like this:
    • https://<your-custom-subdomain>.api.cognitive.azure.cn

使用操作系统的说明。Use the instructions for your operating system.

setx ANOMALY_DETECTOR_KEY <replace-with-your-anomaly-detector-key>
setx ANOMALY_DETECTOR_ENDPOINT <replace-with-your-anomaly-detector-endpoint>

添加环境变量后,请重启控制台窗口。After you add the environment variable, restart the console window.

创建新的 .NET Core 应用程序Create a new .NET Core application

在控制台窗口(例如 cmd、PowerShell 或 Bash)中,使用 dotnet new 命令创建名为 anomaly-detector-quickstart 的新控制台应用。In a console window (such as cmd, PowerShell, or Bash), use the dotnet new command to create a new console app with the name anomaly-detector-quickstart. 此命令将创建包含单个 C# 源文件 (Program.cs) 的简单“Hello World”项目。This command creates a simple "Hello World" project with a single C# source file: Program.cs.

dotnet new console -n anomaly-detector-quickstart

将目录更改为新创建的应用文件夹。Change your directory to the newly created app folder. 可使用以下代码生成应用程序:You can build the application with:

dotnet build

生成输出不应包含警告或错误。The build output should contain no warnings or errors.

...
Build succeeded.
 0 Warning(s)
 0 Error(s)
...

安装客户端库Install the client library

在应用程序目录中,使用以下命令安装适用于 .NET 的异常检测器客户端库:Within the application directory, install the Anomaly Detector client library for .NET with the following command:

dotnet add package Microsoft.Azure.CognitiveServices.AnomalyDetector

从项目目录中,打开 Program.cs 文件,并添加以下 using directivesFrom the project directory, open the program.cs file and add the following using directives:

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

在应用程序的 main() 方法中,为资源的 Azure 位置创建变量,并将密钥创建为环境变量。In the application's main() method, create variables for your resource's Azure location, and your key as an environment variable. 如果在启动应用程序后创建了环境变量,则需要关闭并重新加载运行它的编辑器、IDE 或 shell 以访问该变量。If you created the environment variable after application is launched, the editor, IDE, or shell running it will need to be closed and reloaded to access the variable.

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

对象模型Object model

异常检测器客户端是 AnomalyDetectorClient 对象,使用包含密钥的 ApiKeyServiceClientCredentials 对 Azure 进行身份验证。The Anomaly Detector client is a AnomalyDetectorClient object that authenticates to Azure using ApiKeyServiceClientCredentials, which contains your key. 客户端可以使用 EntireDetectAsync() 对整个数据集进行异常情况检测,或使用 LastDetectAsync() 对最新的数据点进行异常情况检测。The client can do anomaly detection on an entire dataset using EntireDetectAsync(), or on the latest data point using LastDetectAsync(). ChangePointDetectAsync 方法可检测在趋势中标记更改的点。The ChangePointDetectAsync method detects points that mark changes in a trend.

时序数据作为 Request 对象中的一系列 Point 进行发送。Time series data is sent as a series of Points in a Request object. Request 对象包含描述数据的属性(例如Granularity)以及异常检测的参数。The Request object contains properties to describe the data (Granularity for example), and parameters for the anomaly detection.

异常检测器响应是 EntireDetectResponseLastDetectResponsechangePointDetectResponse 对象,具体取决于所使用的方法。The Anomaly Detector response is either an EntireDetectResponse, LastDetectResponse, or changePointDetectResponse object, depending on the method used.

代码示例Code examples

这些代码片段展示如何使用适用于 .NET 的异常检测器客户端库执行以下操作:These code snippets show you how to do the following with the Anomaly Detector client library for .NET:

验证客户端Authenticate the client

在新方法中,使用终结点和密钥实例化客户端。In a new method, instantiate a client with your endpoint and key. 使用密钥创建 ApiKeyServiceClientCredentials 对象,并将其与终结点一起创建 AnomalyDetectorClient 对象。Create an ApiKeyServiceClientCredentials object with your key, and use it with your endpoint to create an AnomalyDetectorClient object.

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

从文件加载时序数据Load time series data from a file

GitHub 下载此快速入门中的示例数据:Download the example data for this quickstart from GitHub:

  1. 在浏览器中,右键单击“原始”。In your browser, right-click Raw.
  2. 单击“将链接另存为”。Click Save link as.
  3. 将文件另存为 .csv 文件,保存到你的应用程序目录。Save the file to your application directory, as a .csv file.

此时序数据的格式为 .csv 文件,它将被发送到异常检测器 API。This time series data is formatted as a .csv file, and will be sent to the Anomaly Detector API.

创建新方法来读取时序数据,并将其添加到 Request 对象。Create a new method to read in the time series data and add it to a Request object. 使用文件路径调用 File.ReadAllLines(),并创建一系列 Point 对象,并去除任何新行字符。Call File.ReadAllLines() with the file path and create a list of Point objects, and strip any new line characters. 提取值并将日期戳与其数值分开,并将它们添加到新 Point 对象。Extract the values and separate the datestamp from its numerical value, and add them to a new Point object.

创建包含一系列点的 Request 对象,并创建 Granularity.Daily 来表示数据点的 Granularity(或周期)。Make a Request object with the series of points, and Granularity.Daily for the Granularity (or periodicity) of the data points.

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

在整个数据集中检测异常Detect anomalies in the entire data set

创建一个方法使用 Request 对象调用客户端的 EntireDetectAsync() 方法,并等待作为 EntireDetectResponse 对象的响应。Create a method to call the client's EntireDetectAsync() method with the Request object and await the response as an EntireDetectResponse object. 如果时序包含任何异常,循环访问响应的 IsAnomaly 值,并打印显示为 true 的值。If the time series contains any anomalies, iterate through the response's IsAnomaly values and print any that are true. 如果找到任何此类值,这些值对应于异常数据点的索引。These values correspond to the index of anomalous data points, if any were found.

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

检测最新数据点的异常状态Detect the anomaly status of the latest data point

创建一个方法使用 Request 对象调用客户端的 LastDetectAsync() 方法,并等待作为 LastDetectResponse 对象的响应。Create a method to call the client's LastDetectAsync() method with the Request object and await the response as a LastDetectResponse object. 检查响应的 IsAnomaly 属性以确定发送的最新数据点是否异常。Check the response's IsAnomaly attribute to determine if the latest data point sent was an anomaly or not.

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

检测数据集中的更改点Detect change points in the data set

创建一个方法,使用 Request 对象调用客户端的 DetectChangePointAsync 方法,并等待作为 ChangePointDetectResponse 对象的响应。Create a method to call the client's DetectChangePointAsync method with the Request object and await the response as a ChangePointDetectResponse object. 检查响应的 IsChangePoint 值,并打印任何为 true 的值。Check the response's IsChangePoint values and print any that are true. 如果找到任何此类值,这些值对应于趋势更改点。These values correspond to trend change points, if any were found.

/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.


This sample demonstrates the Anomaly Detection service's two detection methods:
    * Anomaly detection on an entire time-series dataset.
    * Anomaly detection on the latest data point in a dataset.

 * Prerequisites:
     * The Anomaly Detector client library for .NET
     * A .csv file containing a time-series data set with 
        UTC-timestamp and numerical values pairings. 
        Example data is included in this repo.
*/

namespace AnomalyDetectorSample
{
    // <usingStatements>
    using System;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector;
    using Microsoft.Azure.CognitiveServices.AnomalyDetector.Models;
    // </usingStatements>

    class Program{

        // <mainMethod>
        static void Main(string[] args){
            //This sample assumes you have created an environment variable for your key and endpoint
            string endpoint = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_ENDPOINT");
            string key = Environment.GetEnvironmentVariable("ANOMALY_DETECTOR_KEY");
            string datapath = "request-data.csv";

            IAnomalyDetectorClient client = createClient(endpoint, key); //Anomaly Detector client

            Request request = GetSeriesFromFile(datapath); // The request payload with points from the data file

            EntireDetectSampleAsync(client, request).Wait(); // Async method for batch anomaly detection
            LastDetectSampleAsync(client, request).Wait(); // Async method for analyzing the latest data point in the set
            DetectChangePoint(client, request).Wait(); // Async method for change point detection

            Console.WriteLine("\nPress ENTER to exit.");
            Console.ReadLine();
        }
        // </mainMethod>

        // <createClient>
        static IAnomalyDetectorClient createClient(string endpoint, string key)
        {
            IAnomalyDetectorClient client = new AnomalyDetectorClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            return client;
        }
        // </createClient>

        // <runSamplesHelper>
        //Run the anomaly detection examples with extra error handling
        static void runSamples(IAnomalyDetectorClient client, string dataPath)
        {

            try
            {
                List<Point> series = GetSeriesFromFile(dataPath);
                Request request = new Request(series, Granularity.Daily);

                EntireDetectSampleAsync(client, request).Wait();
                LastDetectSampleAsync(client, request).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null && e.InnerException is APIErrorException)
                {
                    APIError error = ((APIErrorException)e.InnerException).Body;
                    Console.WriteLine("Error code: " + error.Code);
                    Console.WriteLine("Error message: " + error.Message);
                }
                else if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
        // </runSamplesHelper>

        // <GetSeriesFromFile>
        static Request GetSeriesFromFile(string path)
        {
            List<Point> list = File.ReadAllLines(path, Encoding.UTF8)
                .Where(e => e.Trim().Length != 0)
                .Select(e => e.Split(','))
                .Where(e => e.Length == 2)
                .Select(e => new Point(DateTime.Parse(e[0]), Double.Parse(e[1]))).ToList();
            
            return new Request(list, Granularity.Daily); 
        }
        // </GetSeriesFromFile>

        // <entireDatasetExample>
        static async Task EntireDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting anomalies in the entire time series.");

            EntireDetectResponse result = await client.EntireDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly.Contains(true))
            {
                Console.WriteLine("An anomaly was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsAnomaly[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" No anomalies detected in the series.");
            }
        }
        // </entireDatasetExample>

        // <latestPointExample>
        static async Task LastDetectSampleAsync(IAnomalyDetectorClient client, Request request)
        {

            Console.WriteLine("Detecting the anomaly status of the latest point in the series.");
            LastDetectResponse result = await client.LastDetectAsync(request).ConfigureAwait(false);

            if (result.IsAnomaly)
            {
                Console.WriteLine("The latest point was detected as an anomaly.");
            }
            else
            {
                Console.WriteLine("The latest point was not detected as an anomaly.");
            }
        }
        // </latestPointExample>

        // <changePointExample>
        public async Task DetectChangePoint(IAnomalyDetectorClient client, Request request)
        {
            Console.WriteLine("Detecting the change points in the series.");

            ChangePointDetectResponse result = await client.DetectChangePointAsync(request).ConfigureAwait(false);

            if (result.IsChangePoint.Contains(true))
            {
                Console.WriteLine("A change point was detected at index:");
                for (int i = 0; i < request.Series.Count; ++i)
                {
                    if (result.IsChangePoint[i])
                    {
                        Console.Write(i);
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No change point detected in the series.");
            }
        }
        // </changePointExample>
    }
}

运行应用程序Run the application

从应用程序目录使用 dotnet run 命令运行应用程序。Run the application with the dotnet run command from your application directory.

dotnet run

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与资源组相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with the resource group.

后续步骤Next steps

概念:Concepts:

教程:Tutorials:

适用于 JavaScript 的异常检测器客户端库入门。Get started with the Anomaly Detector client library for JavaScript. 请按照以下步骤操作,以使用服务提供的算法安装软件包。Follow these steps to install the package start using the algorithms provided by the service. 通过异常检测器服务,可以对时序数据自动使用最佳适配模型,从而查找器其中的异常,不限行业、场景或数据量。The Anomaly Detector service enables you to find abnormalities in your time series data by automatically using the best-fitting models on it, regardless of industry, scenario, or data volume.

使用适用于 JavaScript 的异常检测器客户端库,可实现以下操作:Use the Anomaly Detector client library for JavaScript to:

  • 以批请求的形式检测整个时序数据集中的异常Detect anomalies throughout your time series data set, as a batch request
  • 在时序中检测最新数据点的异常状态Detect the anomaly status of the latest data point in your time series
  • 检测数据集中的趋势更改点。Detect trend change points in your data set.

库参考文档 | 库源代码 | 包 (npm) | 在 GitHub 上查找代码Library reference documentation | Library source code | Package (npm) | Find the code on GitHub

先决条件Prerequisites

  • Azure 订阅 - 创建试用订阅Azure subscription - Create one for trial
  • 最新版本的 Node.jsThe current version of Node.js
  • 拥有 Azure 订阅后,可在 Azure 门户中创建异常检测器资源来获取密钥和终结点。Once you have your Azure subscription, create an Anomaly Detector resource in the Azure portal to get your key and endpoint. 等待其部署并单击“转到资源”按钮。Wait for it to deploy and click the Go to resource button.
    • 需要从创建的资源获取密钥和终结点,以便将应用程序连接到异常检测器 API。You will need the key and endpoint from the resource you create to connect your application to the Anomaly Detector API. 你稍后会在快速入门中将密钥和终结点粘贴到下方的代码中。You'll paste your key and endpoint into the code below later in the quickstart. 可以使用免费定价层 (F0) 试用该服务,然后再升级到付费层进行生产。You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.

设置Setting up

创建环境变量Create an environment variable

备注

在 2019 年 7 月 1 日之后创建的非试用资源的终结点使用如下所示的自定义子域格式。The endpoints for non-trial resources created after July 1, 2019 use the custom subdomain format shown below. 有关详细信息和区域终结点的完整列表,请参阅认知服务的自定义子域名For more information and a complete list of regional endpoints, see Custom subdomain names for Cognitive Services.

从创建的资源使用密钥和终结点,创建两个用于身份验证的环境变量:Using your key and endpoint from the resource you created, create two environment variables for authentication:

  • ANOMALY_DETECTOR_KEY - 用于验证请求的资源密钥。ANOMALY_DETECTOR_KEY - The resource key for authenticating your requests.
  • ANOMALY_DETECTOR_ENDPOINT - 用于发送 API 请求的资源终结点。ANOMALY_DETECTOR_ENDPOINT - The resource endpoint for sending API requests. 它将如下所示:It will look like this:
    • https://<your-custom-subdomain>.api.cognitive.azure.cn

使用操作系统的说明。Use the instructions for your operating system.

setx ANOMALY_DETECTOR_KEY <replace-with-your-anomaly-detector-key>
setx ANOMALY_DETECTOR_ENDPOINT <replace-with-your-anomaly-detector-endpoint>

添加环境变量后,请重启控制台窗口。After you add the environment variable, restart the console window.

创建新的 Node.js 应用程序Create a new Node.js application

在控制台窗口(例如 cmd、PowerShell 或 Bash)中,为应用创建一个新目录并导航到该目录。In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.

mkdir myapp && cd myapp

运行 npm init 命令以使用 package.json 文件创建一个 node 应用程序。Run the npm init command to create a node application with a package.json file.

npm init

创建一个名为 index.js 的文件,并导入以下库:Create a file named index.js and import the following libraries:

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

为资源的 Azure 终结点和密钥创建变量。Create variables your resource's Azure endpoint and key. 如果在启动应用程序后创建了环境变量,则需要关闭再重新打开运行该应用程序的编辑器、IDE 或 shell 才能访问该变量。If you created the environment variable after you launched the application, you will need to close and reopen the editor, IDE, or shell running it to access the variable. 为将在后面的步骤中下载的示例数据文件创建另一个变量,并为数据点创建一个空列表。Create another variable for the example data file you will download in a later step, and an empty list for the data points. 然后创建一个 ApiKeyCredentials 对象以包含该密钥。Then create a ApiKeyCredentials object to contain the key.

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

安装客户端库Install the client library

安装 ms-rest-azureazure-cognitiveservices-anomalydetector NPM 包。Install the ms-rest-azure and azure-cognitiveservices-anomalydetector NPM packages. 此快速入门中也使用了 csv-parse 库:The csv-parse library is also used in this quickstart:

npm install @azure/ai-anomaly-detector @azure/ms-rest-js csv-parse

应用的 package.json 文件将使用依赖项进行更新。Your app's package.json file will be updated with the dependencies.

对象模型Object model

异常检测器客户端是 AnomalyDetectorClient 对象,该对象使用你的密钥向 Azure 进行身份验证。The Anomaly Detector client is an AnomalyDetectorClient object that authenticates to Azure using your key. 客户端可以使用 entireDetect() 对整个数据集进行异常情况检测,或使用 LastDetect() 对最新的数据点进行异常情况检测。The client can do anomaly detection on an entire dataset using entireDetect(), or on the latest data point using LastDetect(). ChangePointDetectAsync 方法可检测在趋势中标记更改的点。The ChangePointDetectAsync method detects points that mark changes in a trend.

时序数据作为 Request 对象中的一系列 Point 进行发送。Time series data is sent as series of Points in a Request object. Request 对象包含描述数据的属性(例如Granularity)以及异常检测的参数。The Request object contains properties to describe the data (Granularity for example), and parameters for the anomaly detection.

异常检测器响应是 LastDetectResponseEntireDetectResponseChangePointDetectResponse 对象,具体取决于所使用的方法。The Anomaly Detector response is a LastDetectResponse, EntireDetectResponse, or ChangePointDetectResponse object depending on the method used.

代码示例Code examples

这些代码片段展示如何使用适用于 Node.js 的异常检测器客户端库执行以下操作:These code snippets show you how to do the following with the Anomaly Detector client library for Node.js:

验证客户端Authenticate the client

使用终结点和凭据实例化 AnomalyDetectorClient 对象。Instantiate a AnomalyDetectorClient object with your endpoint and credentials.

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

从文件加载时序数据Load time series data from a file

GitHub 下载此快速入门中的示例数据:Download the example data for this quickstart from GitHub:

  1. 在浏览器中,右键单击“原始”。In your browser, right-click Raw.
  2. 单击“将链接另存为”。Click Save link as.
  3. 将文件另存为 .csv 文件,保存到你的应用程序目录。Save the file to your application directory, as a .csv file.

此时序数据的格式为 .csv 文件,它将被发送到异常检测器 API。This time series data is formatted as a .csv file, and will be sent to the Anomaly Detector API.

使用 csv-parse 库的 readFileSync() 方法读取数据文件,并使用 parse() 分析文件。Read your data file with the csv-parse library's readFileSync() method, and parse the file with parse(). 对于每一行,推送包含时间戳和数值的 Point 对象。For each line, push a Point object containing the timestamp, and the numeric value.

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

在整个数据集中检测异常Detect anomalies in the entire data set

调用 API 以使用客户端的 entireDetect() 方法批量检测整个时间序列中的异常。Call the API to detect anomalies through the entire time series as a batch with the client's entireDetect() method. 存储返回的 EntireDetectResponse 对象。Store the returned EntireDetectResponse object. 循环访问响应的 isAnomaly 列表,并输出任何 true 值的索引。Iterate through the response's isAnomaly list, and print the index of any true values. 如果找到任何此类值,这些值对应于异常数据点的索引。These values correspond to the index of anomalous data points, if any were found.

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

检测最新数据点的异常状态Detect the anomaly status of the latest data point

调用异常检测器 API,以便使用客户端的 lastDetect() 方法确定最新数据点是否异常,并存储返回的 LastDetectResponse 对象。Call the Anomaly Detector API to determine if your latest data point is an anomaly using the client's lastDetect() method, and store the returned LastDetectResponse object. 响应的 isAnomaly 值是一个布尔值,用于指定该点的异常状态。The response's isAnomaly value is a boolean that specifies that point's anomaly status.

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

检测数据集中的更改点Detect change points in the data set

调用 API,以使用客户端的 detectChangePoint() 方法检测时序中的更改点。Call the API to detect change points in the time series with the client's detectChangePoint() method. 存储返回的 ChangePointDetectResponse 对象。Store the returned ChangePointDetectResponse object. 循环访问响应的 isChangePoint 列表,并输出任何 true 值的索引。Iterate through the response's isChangePoint list, and print the index of any true values. 如果找到任何此类值,这些值对应于趋势更改点的索引。These values correspond to the indices of trend change points, if any were found.

// <imports>
'use strict'

const fs = require('fs');
const parse = require("csv-parse/lib/sync");
const { AnomalyDetectorClient } = require('@azure/ai-anomaly-detector');
const { AzureKeyCredential } = require('@azure/core-auth');
// </imports>

/**
 * This quickstart will detect anomolies in spreadsheet (.csv) data in two different ways:
 *  - Using the batch method (detects anomalies in entire series of data points)
 *  - Using the stream method (detects the latest data point in a series)
 * 
 * Prerequisites:
 *   - Install the following modules: 
 *       npm install csv-parse
 *       npm install @azure/core-auth
 *       npm install @azure/ai-anomaly-detector
 *   - Add your Anomaly Detector subscription key and endpoint to your environment variables
 *   - Add the request-data.csv file to your local root folder
 * 
 * Reference:
 *   Anomaly Detector documentation: /cognitive-services/anomaly-detector/
 *   SDK: https://docs.microsoft.com/javascript/api/overview/azure/cognitiveservices/anomalydetector?view=azure-node-latest
 *   API: https://dev.cognitive.azure.cn/docs/services/AnomalyDetector/operations/post-timeseries-entire-detect
 */

//<vars>
// Spreadsheet with 2 columns and n rows.
let CSV_FILE = './request-data.csv';

// Authentication variables
// Add your Anomaly Detector subscription key and endpoint to your environment variables.
let key = process.env['ANOMALY_DETECTOR_KEY'];
let endpoint = process.env['ANOMALY_DETECTOR_ENDPOINT'];

// Points array for the request body
let points = [];
//</vars>

// <authentication>
let anomalyDetectorClient = new AnomalyDetectorClient(endpoint, new AzureKeyCredential(key));
// </authentication>

// <readFile>
function readFile() {
    let input = fs.readFileSync(CSV_FILE).toString();
    let parsed = parse(input, { skip_empty_lines: true });
    parsed.forEach(function (e) {
        points.push({ timestamp: new Date(e[0]), value: parseFloat(e[1]) });
    });
}
readFile()
// </readFile>

// <batchCall>
async function batchCall() {
    // Create request body for API call
    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in whole series of points
    await anomalyDetectorClient.detectEntireSeries(body)
        .then((response) => {
            console.log("Batch (entire) anomaly detection):")
            for (let item = 0; item < response.isAnomaly.length; item++) {
                if (response.isAnomaly[item]) {
                    console.log("An anomaly was detected from the series, at row " + item)
                }
            }
        }).catch((error) => {
            console.log(error)
        })

}
batchCall()
// </batchCall>

// <lastDetection>
async function lastDetection() {

    let body = { series: points, granularity: 'daily' }
    // Make the call to detect anomalies in the latest point of a series
    await anomalyDetectorClient.detectLastPoint(body)
        .then((response) => {
            console.log("Latest point anomaly detection:")
            if (response.isAnomaly) {
                console.log("The latest point, in row " + points.length + ", is detected as an anomaly.")
            } else {
                console.log("The latest point, in row " + points.length + ", is not detected as an anomaly.")
            }
        }).catch((error) => {
            console.log(error)
        })
}
lastDetection()
// </lastDetection>

// <changePointDetection>
async function changePointDetection() {

    let body = { series: points, granularity: 'daily' }
    // get change point detect results
    await anomalyDetectorClient.detectChangePoint(body)
        .then((response) => {
            if (
                response.isChangePoint.some(function (changePoint) {
                    return changePoint === true;
                })
            ) {
                console.log("Change points were detected from the series at index:");
                response.isChangePoint.forEach(function (changePoint, index) {
                    if (changePoint === true) {
                        console.log(index);
                    }
                });
            } else {
                console.log("There is no change point detected from the series.");
            }
        }).catch((error) => {
            console.log(error)
        })
}
// </changePointDetection>
changePointDetection();

运行应用程序Run the application

在快速入门文件中使用 node 命令运行应用程序。Run the application with the node command on your quickstart file.

node index.js

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与资源组相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with the resource group.

后续步骤Next steps

概念:Concepts:

教程:Tutorials:

适用于 Python 的异常检测器客户端库入门。Get started with the Anomaly Detector client library for Python. 请按照以下步骤操作,以使用服务提供的算法安装软件包。Follow these steps to install the package start using the algorithms provided by the service. 通过异常检测器服务,可以对时序数据自动使用最佳适配模型,从而查找器其中的异常,不限行业、场景或数据量。The Anomaly Detector service enables you to find abnormalities in your time series data by automatically using the best-fitting models on it, regardless of industry, scenario, or data volume.

使用适用于 Python 的异常检测器客户端库,可实现以下操作:Use the Anomaly Detector client library for Python to:

  • 以批请求的形式检测整个时序数据集中的异常Detect anomalies throughout your time series data set, as a batch request
  • 在时序中检测最新数据点的异常状态Detect the anomaly status of the latest data point in your time series
  • 检测数据集中的趋势更改点。Detect trend change points in your data set.

库参考文档 | 库源代码 | 包 (PyPi) | 在 GitHub 上查找示例代码Library reference documentation | Library source code | Package (PyPi) | Find the sample code on GitHub

先决条件Prerequisites

  • Python 3.xPython 3.x
  • Pandas 数据分析库The Pandas data analysis library
  • Azure 订阅 - 创建试用订阅Azure subscription - Create one for trial
  • 拥有 Azure 订阅后,可在 Azure 门户中创建异常检测器资源来获取密钥和终结点。Once you have your Azure subscription, create an Anomaly Detector resource in the Azure portal to get your key and endpoint. 等待其部署并单击“转到资源”按钮。Wait for it to deploy and click the Go to resource button.
    • 需要从创建的资源获取密钥和终结点,以便将应用程序连接到异常检测器 API。You will need the key and endpoint from the resource you create to connect your application to the Anomaly Detector API. 你稍后会在快速入门中将密钥和终结点粘贴到下方的代码中。You'll paste your key and endpoint into the code below later in the quickstart. 可以使用免费定价层 (F0) 试用该服务,然后再升级到付费层进行生产。You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.

设置Setting up

创建环境变量Create an environment variable

备注

在 2019 年 7 月 1 日之后创建的非试用资源的终结点使用如下所示的自定义子域格式。The endpoints for non-trial resources created after July 1, 2019 use the custom subdomain format shown below. 有关详细信息和区域终结点的完整列表,请参阅认知服务的自定义子域名For more information and a complete list of regional endpoints, see Custom subdomain names for Cognitive Services.

从创建的资源使用密钥和终结点,创建两个用于身份验证的环境变量:Using your key and endpoint from the resource you created, create two environment variables for authentication:

  • ANOMALY_DETECTOR_KEY - 用于验证请求的资源密钥。ANOMALY_DETECTOR_KEY - The resource key for authenticating your requests.
  • ANOMALY_DETECTOR_ENDPOINT - 用于发送 API 请求的资源终结点。ANOMALY_DETECTOR_ENDPOINT - The resource endpoint for sending API requests. 它将如下所示:It will look like this:
    • https://<your-custom-subdomain>.api.cognitive.azure.cn

使用操作系统的说明。Use the instructions for your operating system.

setx ANOMALY_DETECTOR_KEY <replace-with-your-anomaly-detector-key>
setx ANOMALY_DETECTOR_ENDPOINT <replace-with-your-anomaly-detector-endpoint>

添加环境变量后,请重启控制台窗口。After you add the environment variable, restart the console window.

创建新的 Python 应用程序Create a new python application

创建新的 Python 文件并导入以下库。Create a new Python file and import the following libraries.

import os
from azure.ai.anomalydetector import AnomalyDetectorClient
from azure.ai.anomalydetector.models import DetectRequest, TimeSeriesPoint, TimeGranularity, \
    AnomalyDetectorError
from azure.core.credentials import AzureKeyCredential
import pandas as pd

将密钥的变量创建为环境变量,同时创建时序数据文件的路径,以及订阅的 Azure 位置。Create variables for your key as an environment variable, the path to a time series data file, and the Azure location of your subscription. 例如,chinanorthFor example, chinanorth.

SUBSCRIPTION_KEY = os.environ["ANOMALY_DETECTOR_KEY"]
ANOMALY_DETECTOR_ENDPOINT = os.environ["ANOMALY_DETECTOR_ENDPOINT"]
TIME_SERIES_DATA_PATH = os.path.join("./sample_data", "request-data.csv")

安装客户端库Install the client library

在安装 Python 后,可以通过以下命令安装客户端库:After installing Python, you can install the client library with:

pip install --upgrade azure-ai-anomalydetector

对象模型Object model

异常检测器客户端是 AnomalyDetectorClient 对象,该对象使用你的密钥向 Azure 进行身份验证。The Anomaly Detector client is a AnomalyDetectorClient object that authenticates to Azure using your key. 客户端可使用 detect_entire_series 对整个数据集进行异常情况检测,或者使用 detect_last_point 对最新的数据点进行异常情况检测。The client can do anomaly detection an entire dataset using detect_entire_series, or on the latest data point using detect_last_point. detect_change_point 函数可检测在趋势中标记更改的点。The detect_change_point function detects points that mark changes in a trend.

时序数据作为一系列 TimeSeriesPoints 对象进行发送。Time series data is sent as a series of TimeSeriesPoints object. DetectRequest 对象包含描述数据的属性(例如 TimeGranularity)以及异常情况检测的参数。The DetectRequest object contains properties to describe the data TimeGranularity for example, and parameters for the anomaly detection.

异常检测器响应是 LastDetectResponseEntireDetectResponseChangePointDetectResponse 对象,具体取决于所使用的方法。The Anomaly Detector response is a LastDetectResponse, EntireDetectResponse, or ChangePointDetectResponse object depending on the method used.

代码示例Code examples

这些代码片段展示如何使用适用于 Python 的异常检测器客户端库执行以下操作:These code snippets show you how to do the following with the Anomaly Detector client library for Python:

验证客户端Authenticate the client

将 Azure 位置变量添加到终结点,并使用密钥对客户端进行身份验证。Add your Azure location variable to the endpoint, and authenticate the client with your key.

client = AnomalyDetectorClient(AzureKeyCredential(SUBSCRIPTION_KEY), ANOMALY_DETECTOR_ENDPOINT)

从文件加载时序数据Load time series data from a file

GitHub 下载此快速入门中的示例数据:Download the example data for this quickstart from GitHub:

  1. 在浏览器中,右键单击“原始”。In your browser, right-click Raw.
  2. 单击“将链接另存为”。Click Save link as.
  3. 将文件另存为 .csv 文件,保存到你的应用程序目录。Save the file to your application directory, as a .csv file.

此时序数据的格式为 .csv 文件,它将被发送到异常检测器 API。This time series data is formatted as a .csv file, and will be sent to the Anomaly Detector API.

使用 Pandas 库的 read_csv() 方法加载数据文件,并构建一个空的列表变量来存储数据系列。Load your data file with the Pandas library's read_csv() method, and make an empty list variable to store your data series. 循环访问该文件,并将数据作为 TimeSeriesPoint 对象追加。Iterate through the file, and append the data as a TimeSeriesPoint object. 此对象将包含 .csv 数据文件的行中的时间戳和数字值。This object will contain the timestamp and numerical value from the rows of your .csv data file.

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]))

使用时序及其数据点的 TimeGranularity(或周期)创建一个 DetectRequest 对象。Create a DetectRequest object with your time series, and the TimeGranularity (or periodicity) of its data points. 例如,TimeGranularity.dailyFor example, TimeGranularity.daily.

request = DetectRequest(series=series, granularity=TimeGranularity.daily)

在整个数据集中检测异常Detect anomalies in the entire data set

调用 API,以使用客户端的 detect_entire_series 方法检测整个时序数据中的异常。Call the API to detect anomalies through the entire time series data using the client's detect_entire_series method. 存储返回的 EntireDetectResponse 对象。Store the returned EntireDetectResponse object. 循环访问响应的 is_anomaly 列表,并输出任何 true 值的索引。Iterate through the response's is_anomaly list, and print the index of any true values. 如果找到任何此类值,这些值对应于异常数据点的索引。These values correspond to the index of anomalous data points, if any were found.

print('Detecting anomalies in the entire time series.')

try:
    response = client.detect_entire_series(request)
except AnomalyDetectorError as e:
    print('Error code: {}'.format(e.error.code), 'Error message: {}'.format(e.error.message))
except Exception as e:
    print(e)

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.')

检测最新数据点的异常状态Detect the anomaly status of the latest data point

调用异常检测器 API,以使用客户端的 detect_last_point 方法确定最新数据点是否异常,并存储返回的 LastDetectResponse 对象。Call the Anomaly Detector API to determine if your latest data point is an anomaly using the client's detect_last_point method, and store the returned LastDetectResponse object. 响应的 is_anomaly 值是一个布尔值,用于指定该点的异常状态。The response's is_anomaly value is a boolean that specifies that point's anomaly status.

print('Detecting the anomaly status of the latest data point.')

try:
    response = client.detect_last_point(request)
except AnomalyDetectorError as e:
    print('Error code: {}'.format(e.error.code), 'Error message: {}'.format(e.error.message))
except Exception as e:
    print(e)

if response.is_anomaly:
    print('The latest point is detected as anomaly.')
else:
    print('The latest point is not detected as anomaly.')

检测数据集中的更改点Detect change points in the data set

调用 API,以使用客户端的 detect_change_point 方法检测时序数据中的更改点。Call the API to detect change points in the time series data using the client's detect_change_point method. 存储返回的 ChangePointDetectResponse 对象。Store the returned ChangePointDetectResponse object. 循环访问响应的 is_change_point 列表,并输出任何 true 值的索引。Iterate through the response's is_change_point list, and print the index of any true values. 如果找到任何此类值,这些值对应于趋势更改点的索引。These values correspond to the indices of trend change points, if any were found.

print('Detecting change points in the entire time series.')

try:
    response = client.detect_change_point(request)
except AnomalyDetectorError as e:
    print('Error code: {}'.format(e.error.code), 'Error message: {}'.format(e.error.message))
except Exception as e:
    print(e)

if any(response.is_change_point):
    print('An change point was detected at index:')
    for i, value in enumerate(response.is_change_point):
        if value:
            print(i)
else:
    print('No change point were detected in the time series.')

运行应用程序Run the application

使用 python 命令和文件名运行应用程序。Run the application with the python command and your file name.

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与资源组相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with the resource group.

后续步骤Next steps

概念:Concepts:

教程:Tutorials:

在本快速入门中,你将学习如何使用异常检测器服务和 cURL 在一批时序数据中检测异常。In this quickstart, you learn how to detect anomalies in a batch of time series data using the Anomaly Detector service and cURL.

若要深入了解“异常检测器”的概念,请参阅概述一文。For a high-level look at Anomaly Detector concepts, see the overview article.

先决条件Prerequisites

  • Azure 订阅 - 创建试用订阅Azure subscription - Create one for trial
  • 拥有 Azure 订阅后,可在 Azure 门户中创建异常检测器资源来获取密钥和终结点。Once you have your Azure subscription, create an Anomaly Detector resource in the Azure portal to get your key and endpoint. 等待其部署并选择“转到资源”按钮。Wait for it to deploy and select the Go to resource button.
    • 你将需要使用创建的资源中的密钥和终结点地址来使用 REST API。You will need the key and endpoint address from the resource you create to use the REST API. 可以使用免费定价层 (F0) 试用该服务,然后再升级到付费层进行生产。You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.

检测整个系列是否存在异常Detect anomalies for an entire series

请在命令提示符处运行以下命令。At a command prompt, run the following command. 需要将以下值插入到命令中。You will need to insert the following values into the command.

  • 你的异常检测器服务订阅密钥。Your Anomaly detector service subscription key.
  • 你的异常探测器终结点地址。Your Anomaly detector endpoint address.
  • 时序数据的有效 JSON 文件,用于测试异常。A valid JSON file of time series data to test for anomalies. 如果你自己没有文件,可从请求正文示例创建一个 sample.json 文件。If you don't have your own file, you can create a sample.json file from the Request body sample.
curl -v -X POST "https://{endpoint}/anomalydetector/v1.0/timeseries/entire/detect"
-H "Content-Type: application/json"
-H "Ocp-Apim-Subscription-Key: {subscription key}"
-d "@{path_to_file.json}" 

对于所有数据均已填充的示例:For an example with all values populated:

curl -v -X POST "https://my-resource-name.cognitiveservices.azure.cn/anomalydetector/v1.0/timeseries/entire/detect" -H  "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key:1111112222222ed333333ab333333333" -d "@test.json"

如果你使用了先决条件中的示例数据,则应收到包含以下结果的响应 200:If you used the sample data from the pre-requisites, you should receive a response 200 with the following results:

{
  "expectedValues": [
    827.7940908243968,
    798.9133774671927,
    888.6058431807189,
    900.5606407986661,
    962.8389426378304,
    933.2591606306954,
    891.0784104799666,
    856.1781601363697,
    809.8987227908941,
    807.375129007505,
    764.3196682448518,
    803.933498594564,
    823.5900620883058,
    794.0905641334288,
    883.164245249282,
    894.8419000690953,
    956.8430591101258,
    927.6285055190114,
    885.812983784303,
    851.6424797402517,
    806.0927886943216,
    804.6826815312029,
    762.74070738882,
    804.0251702513732,
    825.3523662579559,
    798.0404188724976,
    889.3016505577698,
    902.4226124345937,
    965.867078532635,
    937.3200495736695,
    896.1720524711102,
    862.0087368413656,
    816.4662342097423,
    814.4297745524709,
    771.8614479159354,
    811.859271346729,
    831.8998279215521,
    802.947544797165,
    892.5684407435083,
    904.5488214533809,
    966.8527063844707,
    937.3168391003043,
    895.180003672544,
    860.3649596356635,
    814.1707285969043,
    811.9054862686213,
    769.1083769610742,
    809.2328084659704
  ],
  "upperMargins": [
    41.389704541219835,
    39.94566887335964,
    44.43029215903594,
    45.02803203993331,
    48.14194713189152,
    46.66295803153477,
    44.55392052399833,
    42.808908006818484,
    40.494936139544706,
    40.36875645037525,
    38.215983412242586,
    40.196674929728196,
    41.17950310441529,
    39.70452820667144,
    44.1582122624641,
    44.74209500345477,
    47.84215295550629,
    46.38142527595057,
    44.290649189215145,
    42.58212398701258,
    40.30463943471608,
    40.234134076560146,
    38.137035369441,
    40.201258512568664,
    41.267618312897795,
    39.90202094362488,
    44.46508252788849,
    45.121130621729684,
    48.29335392663175,
    46.86600247868348,
    44.80860262355551,
    43.100436842068284,
    40.82331171048711,
    40.721488727623544,
    38.593072395796774,
    40.59296356733645,
    41.5949913960776,
    40.14737723985825,
    44.62842203717541,
    45.227441072669045,
    48.34263531922354,
    46.86584195501521,
    44.759000183627194,
    43.01824798178317,
    40.70853642984521,
    40.59527431343106,
    38.45541884805371,
    40.46164042329852
  ],
  "lowerMargins": [
    41.389704541219835,
    39.94566887335964,
    44.43029215903594,
    45.02803203993331,
    48.14194713189152,
    46.66295803153477,
    44.55392052399833,
    42.808908006818484,
    40.494936139544706,
    40.36875645037525,
    38.215983412242586,
    40.196674929728196,
    41.17950310441529,
    39.70452820667144,
    44.1582122624641,
    44.74209500345477,
    47.84215295550629,
    46.38142527595057,
    44.290649189215145,
    42.58212398701258,
    40.30463943471608,
    40.234134076560146,
    38.137035369441,
    40.201258512568664,
    41.267618312897795,
    39.90202094362488,
    44.46508252788849,
    45.121130621729684,
    48.29335392663175,
    46.86600247868348,
    44.80860262355551,
    43.100436842068284,
    40.82331171048711,
    40.721488727623544,
    38.593072395796774,
    40.59296356733645,
    41.5949913960776,
    40.14737723985825,
    44.62842203717541,
    45.227441072669045,
    48.34263531922354,
    46.86584195501521,
    44.759000183627194,
    43.01824798178317,
    40.70853642984521,
    40.59527431343106,
    38.45541884805371,
    40.46164042329852
  ],
  "isAnomaly": [
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    true,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false
  ],
  "isPositiveAnomaly": [
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    true,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false
  ],
  "isNegativeAnomaly": [
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false
  ],
  "period": 12
}

有关详细信息,请参阅异常情况检测 REST 参考For more information, see the Anomaly Detection REST reference.

清理资源Clean up resources

如果想要清理并删除认知服务订阅,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. 删除资源组同时也会删除与资源组相关联的任何其他资源。Deleting the resource group also deletes any other resources associated with the resource group.

后续步骤Next steps

概念:Concepts:

教程:Tutorials: