-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DetectSeasonality as a Helper function in TimeSeries ExtensionDia…
…log (#5231) * create class PeriodDetectUtils * Test period detect * math utils * restore file * update license * 1. Add DetectSeasonality as a helper method in ExtensionsCatalog, 2. Remove the MathUtils and use MedianDblAggregator (make it BestFriend) 3. Add Unit Tests * Change SeasonalityDetector to be internal class * 1. Introduce randomnessThreshold as an optional parameter 2. Update comments and polish SeasonalityDetector for readability. * minor float to double type change * fix unit tests * address Harish's comments: 1. Change Randomness threshold to [0, 1] range as confidence internal and map to inverse normal cumulative distribution 2. Update unit tests to use sin(2pi + x) 3. Other formatting issues * minor format update * update comments * minor follow up comment update * update threshold to p value Co-authored-by: yuyi@microsoft.com <Yuanxiang.Ying@microsoft.com> Co-authored-by: Lisa Hua <jinhua@microsoft.com>
- Loading branch information
1 parent
1c2469f
commit bb13d62
Showing
5 changed files
with
463 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSeasonality.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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; | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.