語言

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

輸入資料視圖。資料是 的 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;
            }
        }

    }
}

適用於