TimeSeriesCatalog.DetectSeasonality 方法

定义

在时序数据中,季节性 (或周期性) 存在在特定定期间隔发生的变体,例如每周、每月或季度。

此方法通过采用四叶分析技术来检测此可预测的间隔 (或周期) 。 假设输入值具有相同的时间间隔 (,例如,按时间戳) 每秒收集的传感器数据,此方法会获取时序数据列表,并返回输入季节性数据的常规周期(如果可预测的波动或模式可以在输入值期间内递归或重复)。

如果未找到此类模式,则返回 -1,即输入值不遵循季节性波动。

public static int DetectSeasonality (this Microsoft.ML.AnomalyDetectionCatalog catalog, Microsoft.ML.IDataView input, string inputColumnName, int seasonalityWindowSize = -1, double randomnessThreshold = 0.95);
static member DetectSeasonality : Microsoft.ML.AnomalyDetectionCatalog * Microsoft.ML.IDataView * string * int * double -> int
<Extension()>
Public Function DetectSeasonality (catalog As AnomalyDetectionCatalog, input As IDataView, inputColumnName As String, Optional seasonalityWindowSize As Integer = -1, Optional randomnessThreshold As Double = 0.95) As Integer

参数

catalog
AnomalyDetectionCatalog

检测季节性目录。

input
IDataView

输入 DataView.数据是一 IDataView个实例。

inputColumnName
String

要处理的列的名称。 列数据必须是 Double

seasonalityWindowSize
Int32

输入值中要考虑的值数的上限。 设置为 -1 时,使用整个输入来拟合模型;当设置为正整数时,将只考虑第一个 windowSize 值数。 默认值为 -1。

randomnessThreshold
Double

随机阈值 ,指定输入值遵循可预测模式定期作为季节性数据的方式。 范围介于 [0, 1]。 默认情况下,它设置为 0.95。

返回

输入作为季节性数据的定期间隔,否则返回 -1。

示例

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.TimeSeries;

namespace Samples.Dynamic
{
    public static class DetectSeasonality
    {
        public static void Example()
        {
            /* Create a new ML context, for ML.NET operations. It can be used for
             exception tracking and logging, as well as the source of randomness.*/
            var mlContext = new MLContext();

            // Create a seasonal data as input: y = sin(2 * Pi + x)
            var seasonalData = Enumerable.Range(0, 100).Select(x => new TimeSeriesData(Math.Sin(2 * Math.PI + x)));

            // Load the input data as a DataView.
            var dataView = mlContext.Data.LoadFromEnumerable(seasonalData);

            /* Two option parameters:
             * seasonalityWindowSize: Default value is -1. When set to -1, use the whole input to fit model; 
             * when set to a positive integer, only the first windowSize number of values will be considered.
             * randomnessThreshold: Randomness threshold that specifies how confidence the input values follows 
             * a predictable pattern recurring as seasonal data. By default, it is set as 0.99. 
             * The higher the threshold is set, the more strict recurring pattern the 
             * input values should follow to be determined as seasonal data.
             */
            int period = mlContext.AnomalyDetection.DetectSeasonality(
                dataView,
                nameof(TimeSeriesData.Value),
                seasonalityWindowSize: 40);

            // Print the Seasonality Period result.
            Console.WriteLine($"Seasonality Period: #{period}");
        }

        private class TimeSeriesData
        {
            public double Value;

            public TimeSeriesData(double value)
            {
                Value = value;
            }
        }

    }
}

适用于