Skip to content

Commit 9e9ae13

Browse files
committed
Add samples.
1 parent e22b106 commit 9e9ae13

File tree

12 files changed

+56
-38
lines changed

12 files changed

+56
-38
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectAnomalyBySrCnn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void Example()
4040
ITransformer model = ml.Transforms.DetectAnomalyBySrCnn(outputColumnName, inputColumnName, 16, 5, 5, 3, 8, 0.35).Fit(dataView);
4141

4242
// Create a time series prediction engine from the model.
43-
var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, SrCnnAnomalyDetection>(ml);
43+
var engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, SrCnnAnomalyDetection>(ml);
4444

4545
Console.WriteLine($"{outputColumnName} column obtained post-transformation.");
4646
Console.WriteLine("Data\tAlert\tScore\tMag");

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsa.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void Example()
5757
ITransformer model = ml.Transforms.DetectChangePointBySsa(outputColumnName, inputColumnName, confidence, changeHistoryLength, TrainingSize, SeasonalitySize + 1).Fit(dataView);
5858

5959
// Create a prediction engine from the model for feeding new data.
60-
var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, ChangePointPrediction>(ml);
60+
var engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, ChangePointPrediction>(ml);
6161

6262
// Start streaming new data points with no change point to the prediction engine.
6363
Console.WriteLine($"Output from ChangePoint predictions on new data:");
@@ -99,7 +99,7 @@ public static void Example()
9999
model = ml.Model.Load(file, out DataViewSchema schema);
100100

101101
// We must create a new prediction engine from the persisted model.
102-
engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, ChangePointPrediction>(ml);
102+
engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, ChangePointPrediction>(ml);
103103

104104
// Run predictions on the loaded model.
105105
for (int i = 0; i < 5; i++)

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsaStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void Example()
5757
ITransformer model = ml.Transforms.DetectChangePointBySsa(outputColumnName, inputColumnName, confidence, changeHistoryLength, TrainingSize, SeasonalitySize + 1).Fit(dataView);
5858

5959
// Create a prediction engine from the model for feeding new data.
60-
var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, ChangePointPrediction>(ml);
60+
var engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, ChangePointPrediction>(ml);
6161

6262
// Start streaming new data points with no change point to the prediction engine.
6363
Console.WriteLine($"Output from ChangePoint predictions on new data:");
@@ -103,7 +103,7 @@ public static void Example()
103103
model = ml.Model.Load(stream, out DataViewSchema schema);
104104

105105
// We must create a new prediction engine from the persisted model.
106-
engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, ChangePointPrediction>(ml);
106+
engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, ChangePointPrediction>(ml);
107107

108108
// Run predictions on the loaded model.
109109
for (int i = 0; i < 5; i++)

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidChangePoint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void Example()
5656
ITransformer model = ml.Transforms.DetectIidChangePoint(outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView);
5757

5858
// Create a time series prediction engine from the model.
59-
var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, ChangePointPrediction>(ml);
59+
var engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, ChangePointPrediction>(ml);
6060

6161
Console.WriteLine($"{outputColumnName} column obtained post-transformation.");
6262
Console.WriteLine("Data\tAlert\tScore\tP-Value\tMartingale value");
@@ -97,7 +97,7 @@ public static void Example()
9797
model = ml.Model.Load(file, out DataViewSchema schema);
9898

9999
// Create a time series prediction engine from the checkpointed model.
100-
engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, ChangePointPrediction>(ml);
100+
engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, ChangePointPrediction>(ml);
101101
for (int index = 0; index < 8; index++)
102102
{
103103
// Anomaly change point detection.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidSpike.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void Example()
4848
ITransformer model = ml.Transforms.DetectIidSpike(outputColumnName, inputColumnName, 95, Size).Fit(dataView);
4949

5050
// Create a time series prediction engine from the model.
51-
var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, IidSpikePrediction>(ml);
51+
var engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, IidSpikePrediction>(ml);
5252

5353
Console.WriteLine($"{outputColumnName} column obtained post-transformation.");
5454
Console.WriteLine("Data\tAlert\tScore\tP-Value");

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSpikeBySsa.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void Example()
5454
ITransformer model = ml.Transforms.DetectSpikeBySsa(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView);
5555

5656
// Create a prediction engine from the model for feeding new data.
57-
var engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, SsaSpikePrediction>(ml);
57+
var engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, SsaSpikePrediction>(ml);
5858

5959
// Start streaming new data points with no change point to the prediction engine.
6060
Console.WriteLine($"Output from spike predictions on new data:");
@@ -94,7 +94,7 @@ public static void Example()
9494
model = ml.Model.Load(file, out DataViewSchema schema);
9595

9696
// We must create a new prediction engine from the persisted model.
97-
engine = model.CreateTimeSeriesPredictionFunction<TimeSeriesData, SsaSpikePrediction>(ml);
97+
engine = model.CreateAnomalyDetectionEngine<TimeSeriesData, SsaSpikePrediction>(ml);
9898

9999
// Run predictions on the loaded model.
100100
for (int i = 0; i < 5; i++)

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/Forecasting.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*using System;
1+
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using Microsoft.ML;
45
using Microsoft.ML.Transforms.TimeSeries;
5-
using Microsoft.ML.TimeSeries;*/
66

77
namespace Samples.Dynamic
88
{
@@ -12,12 +12,11 @@ public static class Forecasting
1212
// does forecasting.
1313
public static void Example()
1414
{
15-
/*// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
15+
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
1616
// as well as the source of randomness.
1717
var ml = new MLContext();
1818

19-
// Generate sample series data with a recurring pattern
20-
const int SeasonalitySize = 5;
19+
// Generate sample series data with a recurring pattern.
2120
var data = new List<TimeSeriesData>()
2221
{
2322
new TimeSeriesData(0),
@@ -46,41 +45,56 @@ public static void Example()
4645
var inputColumnName = nameof(TimeSeriesData.Value);
4746

4847
// Instantiate the forecasting model.
49-
var model = ml.Forecasting.AdaptiveSingularSpectrumSequenceModeler(inputColumnName, data.Count, SeasonalitySize + 1, SeasonalitySize,
50-
1, AdaptiveSingularSpectrumSequenceForecasting.RankSelectionMethod.Exact, null, SeasonalitySize / 2, false, false);
48+
var model = ml.Forecasting.ForecastBySsa(inputColumnName, inputColumnName, 5, 11, data.Count, 5);
5149

5250
// Train.
53-
model.Train(dataView);
51+
var transformer = model.Fit(dataView);
5452

5553
// Forecast next five values.
56-
var forecast = model.Forecast(5);
54+
var forecastEngine = transformer.CreateForecastingEngine<TimeSeriesData>(ml);
55+
var forecast = ml.Data.CreateEnumerable<ForecastResult>(forecastEngine.Forecast(5), false);
5756
Console.WriteLine($"Forecasted values:");
5857
Console.WriteLine("[{0}]", string.Join(", ", forecast));
5958
// Forecasted values:
6059
// [2.452744, 2.589339, 2.729183, 2.873005, 3.028931]
6160

6261
// Update with new observations.
63-
dataView = ml.Data.LoadFromEnumerable(new List<TimeSeriesData>() { new TimeSeriesData(0), new TimeSeriesData(0), new TimeSeriesData(0), new TimeSeriesData(0) });
64-
model.Update(dataView);
62+
forecastEngine.Update(new TimeSeriesData(0));
63+
forecastEngine.Update(new TimeSeriesData(0));
64+
forecastEngine.Update(new TimeSeriesData(0));
65+
forecastEngine.Update(new TimeSeriesData(0));
6566

6667
// Checkpoint.
67-
ml.Model.SaveForecastingModel(model, "model.zip");
68+
forecastEngine.CheckPoint(ml, "model.zip");
6869

6970
// Load the checkpointed model from disk.
70-
var modelCopy = ml.Model.LoadForecastingModel<float>("model.zip");
71+
// Load the model.
72+
ITransformer modelCopy;
73+
using (var file = File.OpenRead("model.zip"))
74+
modelCopy = ml.Model.Load(file, out DataViewSchema schema);
75+
76+
// We must create a new prediction engine from the persisted model.
77+
var forecastEngineCopy = modelCopy.CreateForecastingEngine<TimeSeriesData>(ml);
7178

7279
// Forecast with the checkpointed model loaded from disk.
73-
forecast = modelCopy.Forecast(5);
80+
forecast = ml.Data.CreateEnumerable<ForecastResult>(forecastEngineCopy.Forecast(5), false);
7481
Console.WriteLine("[{0}]", string.Join(", ", forecast));
7582
// [0.8681176, 0.8185108, 0.8069275, 0.84405, 0.9455081]
7683

7784
// Forecast with the original model(that was checkpointed to disk).
78-
forecast = model.Forecast(5);
85+
forecast = ml.Data.CreateEnumerable<ForecastResult>(forecastEngine.Forecast(5), false);
7986
Console.WriteLine("[{0}]", string.Join(", ", forecast));
80-
// [0.8681176, 0.8185108, 0.8069275, 0.84405, 0.9455081]*/
87+
// [0.8681176, 0.8185108, 0.8069275, 0.84405, 0.9455081]
8188

8289
}
8390

91+
class ForecastResult
92+
{
93+
#pragma warning disable CS0649
94+
public float Forecast;
95+
#pragma warning restore CS0649
96+
}
97+
8498
class TimeSeriesData
8599
{
86100
public float Value;

docs/samples/Microsoft.ML.Samples/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Reflection;
3+
using Samples.Dynamic;
34

45
namespace Microsoft.ML.Samples
56
{
@@ -9,7 +10,7 @@ public static class Program
910

1011
internal static void RunAll()
1112
{
12-
int samples = 0;
13+
/*int samples = 0;
1314
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
1415
{
1516
var sample = type.GetMethod("Example", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
@@ -22,7 +23,8 @@ internal static void RunAll()
2223
}
2324
}
2425
25-
Console.WriteLine("Number of samples that ran without any exception: " + samples);
26+
Console.WriteLine("Number of samples that ran without any exception: " + samples);*/
27+
Forecasting.Example();
2628
}
2729
}
2830
}

src/Microsoft.ML.TimeSeries/AdaptiveSingularSpectrumSequenceModeler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public AdaptiveSingularSpectrumSequenceModelerInternal(IHostEnvironment env, int
230230
_host = env.Register(LoaderSignature);
231231
_host.CheckParam(windowSize >= 2, nameof(windowSize), "The window size should be at least 2."); // ...because otherwise we have nothing to autoregress on
232232
_host.CheckParam(seriesLength > windowSize, nameof(seriesLength), "The series length should be greater than the window size.");
233-
_host.Check(trainSize > 2 * windowSize, "The input series length for training should be greater than twice the window size.");
233+
_host.Check(trainSize > 2 * windowSize, "The input size for training should be greater than twice the window size.");
234234
_host.CheckParam(0 <= discountFactor && discountFactor <= 1, nameof(discountFactor), "The discount factor should be in [0,1].");
235235
_host.CheckParam(maxGrowth == null || maxGrowth.Value.TimeSpan > 0, nameof(GrowthRatio.TimeSpan), "The time span must be strictly positive.");
236236
_host.CheckParam(maxGrowth == null || maxGrowth.Value.Growth >= 0, nameof(GrowthRatio.Growth), "The growth must be non-negative.");

src/Microsoft.ML.TimeSeries/PredictionFunction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public static class PredictionFunctionExtensions
385385
/// ]]>
386386
/// </format>
387387
/// </example>
388-
public static TimeSeriesAnomalyDetection<TSrc, TDst> CreateTimeSeriesPredictionFunction<TSrc, TDst>(this ITransformer transformer, IHostEnvironment env,
388+
public static TimeSeriesAnomalyDetection<TSrc, TDst> CreateAnomalyDetectionEngine<TSrc, TDst>(this ITransformer transformer, IHostEnvironment env,
389389
bool ignoreMissingColumns = false, SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null)
390390
where TSrc : class
391391
where TDst : class, new()
@@ -416,7 +416,7 @@ public static TimeSeriesAnomalyDetection<TSrc, TDst> CreateTimeSeriesPredictionF
416416
/// ]]>
417417
/// </format>
418418
/// </example>
419-
public static TimeSeriesForecasting<TSrc> CreateTimeSeriesForecastingEngine<TSrc>(this ITransformer transformer, IHostEnvironment env,
419+
public static TimeSeriesForecasting<TSrc> CreateForecastingEngine<TSrc>(this ITransformer transformer, IHostEnvironment env,
420420
bool ignoreMissingColumns = false, SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null)
421421
where TSrc : class
422422
{

0 commit comments

Comments
 (0)