Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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();

var dataView = mlContext.Data.LoadFromEnumerable(GetPointsWithSeasonality());
// Create a seasonal data as input for DetectSeasonality.
var input = GetPointsWithSeasonality();
SeasonalityDetector seasonalityDetector = new SeasonalityDetector();
int period = mlContext.AnomalyDetection.DetectSeasonality(dataView, "Input");

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

private static IEnumerable<TimeSeriesData> GetPointsWithSeasonality()
{
return new List<double>() {18.004, 87.401, 87.411, 18.088, 18.017, 87.759, 33.996, 18.043, 87.853, 18.364, 18.004, 86.992, 87.555,
18.088, 18.029, 87.906, 87.471, 18.039, 18.099, 87.403, 18.030, 72.991, 87.804, 18.381, 18.016, 87.145, 87.771,
18.029, 18.084, 87.976, 34.913, 18.064, 18.302, 87.723, 18.001, 86.401, 87.344, 18.295, 18.002, 87.793,
87.531, 18.055, 18.005, 87.947, 18.003, 72.743, 87.722, 18.142 }.Select(t => new TimeSeriesData(t));
}

private class TimeSeriesData
{
public double Value;

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

}
}
3 changes: 2 additions & 1 deletion src/Microsoft.ML.Data/Transforms/NormalizeColumnDbl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ internal static void GetMedianSoFar(in double num, ref double median, ref MaxHea
/// It tracks median values of non-sparse values (vCount).
/// NaNs are ignored when updating min and max.
/// </summary>
[BestFriend]
internal sealed class MedianDblAggregator : IColumnAggregator<double>
{
private MedianAggregatorUtils.MaxHeap<double> _belowMedianHeap;
Expand Down Expand Up @@ -1213,7 +1214,7 @@ private void GetResult(ref TFloat input, ref TFloat value)
}

public override NormalizingTransformer.NormalizerModelParametersBase GetNormalizerModelParams()
=> new NormalizingTransformer.BinNormalizerModelParameters<TFloat>(ImmutableArray.Create(_binUpperBounds), _den,_offset);
=> new NormalizingTransformer.BinNormalizerModelParameters<TFloat>(ImmutableArray.Create(_binUpperBounds), _den, _offset);
}

public sealed class ImplVec : BinColumnFunction
Expand Down
22 changes: 22 additions & 0 deletions src/Microsoft.ML.TimeSeries/ExtensionsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,28 @@ public static RootCause LocalizeRootCause(this AnomalyDetectionCatalog catalog,
return dst;
}

/// <summary>
/// Obtain the period by adopting techniques of spectral analysis. which is founded by
/// the fourier analysis. returns -1 means there's no significant period. otherwise, a period
/// is returned.
/// </summary>
/// <param name="catalog">The detect seasonality catalog.</param>
/// <param name="input">Input DataView.The data is an instance of <see cref="Microsoft.ML.IDataView"/>.</param>
/// <param name="inputColumnName">Name of column to process. The column data must be <see cref="System.Double"/>.</param>
/// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.
/// When set to -1, use the whole input to fit model, when set to a positive integer, use this number as batch size.
/// Default value is -1.</param>
/// <returns>The detected period if seasonality period exists, otherwise return -1.</returns>
/// <example>
/// <format type="text/markdown">
/// <![CDATA[
/// [!code-csharp[LocalizeRootCause](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSeasonality.cs)]
/// ]]>
/// </format>
/// </example>
public static int DetectSeasonality(this AnomalyDetectionCatalog catalog, IDataView input, string inputColumnName, int seasonalityWindowSize = -1)
=> new SeasonalityDetector().DetectSeasonality(CatalogUtils.GetEnvironment(catalog), input, inputColumnName, seasonalityWindowSize);

private static void CheckRootCauseInput(IHostEnvironment host, RootCauseLocalizationInput src)
{
host.CheckUserArg(src.Slices.Count >= 1, nameof(src.Slices), "Must has more than one item");
Expand Down
Loading