Skip to content

Commit 30e5db4

Browse files
committed
Addressing PR comments
1 parent e2487bf commit 30e5db4

File tree

11 files changed

+38
-395
lines changed

11 files changed

+38
-395
lines changed

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

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.IO;
87
using Microsoft.ML.Data;
9-
using Microsoft.ML.Transforms.TimeSeries;
108

119
namespace Microsoft.ML.Samples.Dynamic
1210
{
@@ -29,7 +27,7 @@ public IidChangePointData(float value)
2927
}
3028

3129
// This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
32-
// IidChangePointDetector is applied then to identify points where data distribution changed.
30+
// The estimator is applied then to identify points where data distribution changed.
3331
public static void Example()
3432
{
3533
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
@@ -48,7 +46,7 @@ public static void Example()
4846
// Convert data to IDataView.
4947
var dataView = ml.Data.LoadFromEnumerable(data);
5048

51-
// Setup IidSpikeDetector arguments
49+
// Setup estimator arguments
5250
string outputColumnName = nameof(ChangePointPrediction.Prediction);
5351
string inputColumnName = nameof(IidChangePointData.Value);
5452

@@ -84,109 +82,5 @@ public static void Example()
8482
// 7 0 7.00 0.50 0.00
8583
// 7 0 7.00 0.50 0.00
8684
}
87-
88-
// This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
89-
// IidChangePointDetector is applied then to identify points where data distribution changed using time series
90-
// prediction engine. The engine is checkpointed and then loaded back from disk into memory and used for prediction.
91-
public static void IidChangePointDetectorPrediction()
92-
{
93-
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
94-
// as well as the source of randomness.
95-
var ml = new MLContext();
96-
97-
// Generate sample series data with a change
98-
const int Size = 16;
99-
var data = new List<IidChangePointData>(Size);
100-
for (int i = 0; i < Size / 2; i++)
101-
data.Add(new IidChangePointData(5));
102-
// This is a change point
103-
for (int i = 0; i < Size / 2; i++)
104-
data.Add(new IidChangePointData(7));
105-
106-
// Convert data to IDataView.
107-
var dataView = ml.Data.LoadFromEnumerable(data);
108-
109-
// Setup IidSpikeDetector arguments
110-
string outputColumnName = nameof(ChangePointPrediction.Prediction);
111-
string inputColumnName = nameof(IidChangePointData.Value);
112-
113-
// Time Series model.
114-
ITransformer model = ml.Transforms.DetectIidChangePoint(outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView);
115-
116-
// Create a time series prediction engine from the model.
117-
var engine = model.CreateTimeSeriesPredictionFunction<IidChangePointData, ChangePointPrediction>(ml);
118-
for(int index = 0; index < 8; index++)
119-
{
120-
// Anomaly change point detection.
121-
var prediction = engine.Predict(new IidChangePointData(5));
122-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 5, prediction.Prediction[0],
123-
prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
124-
}
125-
126-
// Change point
127-
var changePointPrediction = engine.Predict(new IidChangePointData(7));
128-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, changePointPrediction.Prediction[0],
129-
changePointPrediction.Prediction[1], changePointPrediction.Prediction[2], changePointPrediction.Prediction[3]);
130-
131-
// Checkpoint the model.
132-
var modelPath = "temp.zip";
133-
engine.CheckPoint(ml, modelPath);
134-
135-
// Reference to current time series engine because in the next step "engine" will point to the
136-
// checkpointed model being loaded from disk.
137-
var timeseries1 = engine;
138-
139-
// Load the model.
140-
using (var file = File.OpenRead(modelPath))
141-
model = ml.Model.Load(file);
142-
143-
// Create a time series prediction engine from the checkpointed model.
144-
engine = model.CreateTimeSeriesPredictionFunction<IidChangePointData, ChangePointPrediction>(ml);
145-
for (int index = 0; index < 8; index++)
146-
{
147-
// Anomaly change point detection.
148-
var prediction = engine.Predict(new IidChangePointData(7));
149-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0],
150-
prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
151-
}
152-
153-
// Prediction from the original time series engine should match the prediction from
154-
// check pointed model.
155-
engine = timeseries1;
156-
for (int index = 0; index < 8; index++)
157-
{
158-
// Anomaly change point detection.
159-
var prediction = engine.Predict(new IidChangePointData(7));
160-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0],
161-
prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
162-
}
163-
164-
// Data Alert Score P-Value Martingale value
165-
// 5 0 5.00 0.50 0.00 <-- Time Series 1.
166-
// 5 0 5.00 0.50 0.00
167-
// 5 0 5.00 0.50 0.00
168-
// 5 0 5.00 0.50 0.00
169-
// 5 0 5.00 0.50 0.00
170-
// 5 0 5.00 0.50 0.00
171-
// 5 0 5.00 0.50 0.00
172-
// 5 0 5.00 0.50 0.00
173-
// 7 1 7.00 0.00 10298.67 <-- alert is on, predicted changepoint (and model is checkpointed).
174-
175-
// 7 0 7.00 0.13 33950.16 <-- Time Series 2 : Model loaded back from disk and prediction is made.
176-
// 7 0 7.00 0.26 60866.34
177-
// 7 0 7.00 0.38 78362.04
178-
// 7 0 7.00 0.50 0.01
179-
// 7 0 7.00 0.50 0.00
180-
// 7 0 7.00 0.50 0.00
181-
// 7 0 7.00 0.50 0.00
182-
183-
// 7 0 7.00 0.13 33950.16 <-- Time Series 1 and prediction is made.
184-
// 7 0 7.00 0.26 60866.34
185-
// 7 0 7.00 0.38 78362.04
186-
// 7 0 7.00 0.50 0.01
187-
// 7 0 7.00 0.50 0.00
188-
// 7 0 7.00 0.50 0.00
189-
// 7 0 7.00 0.50 0.00
190-
}
19185
}
19286
}

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

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class IidSpikePrediction
2525
}
2626

2727
// This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
28-
// IidSpikeDetector is applied then to identify spiking points in the series.
28+
// The estimator is applied then to identify spiking points in the series.
2929
public static void Example()
3030
{
3131
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
@@ -45,7 +45,7 @@ public static void Example()
4545
// Convert data to IDataView.
4646
var dataView = ml.Data.LoadFromEnumerable(data);
4747

48-
// Setup IidSpikeDetector arguments
48+
// Setup the estimator arguments
4949
string outputColumnName = nameof(IidSpikePrediction.Prediction);
5050
string inputColumnName = nameof(IidSpikeData.Value);
5151

@@ -75,75 +75,5 @@ public static void Example()
7575
// 0 5.00 0.50
7676
// 0 5.00 0.50
7777
}
78-
79-
public static void IidSpikeDetectorPrediction()
80-
{
81-
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
82-
// as well as the source of randomness.
83-
var ml = new MLContext();
84-
85-
// Generate sample series data with a spike
86-
const int Size = 10;
87-
var data = new List<IidSpikeData>(Size);
88-
for (int i = 0; i < Size / 2; i++)
89-
data.Add(new IidSpikeData(5));
90-
// This is a spike
91-
data.Add(new IidSpikeData(10));
92-
for (int i = 0; i < Size / 2; i++)
93-
data.Add(new IidSpikeData(5));
94-
95-
// Convert data to IDataView.
96-
var dataView = ml.Data.LoadFromEnumerable(data);
97-
98-
// Setup IidSpikeDetector arguments
99-
string outputColumnName = nameof(IidSpikePrediction.Prediction);
100-
string inputColumnName = nameof(IidSpikeData.Value);
101-
// The transformed model.
102-
ITransformer model = ml.Transforms.DetectIidSpike(outputColumnName, inputColumnName, 95, Size).Fit(dataView);
103-
104-
// Create a time series prediction engine from the model.
105-
var engine = model.CreateTimeSeriesPredictionFunction<IidSpikeData, IidSpikePrediction>(ml);
106-
for (int index = 0; index < 5; index++)
107-
{
108-
// Anomaly spike detection.
109-
var prediction = engine.Predict(new IidSpikeData(5));
110-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 5, prediction.Prediction[0],
111-
prediction.Prediction[1], prediction.Prediction[2]);
112-
}
113-
114-
// Spike.
115-
var spikePrediction = engine.Predict(new IidSpikeData(10));
116-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 10, spikePrediction.Prediction[0],
117-
spikePrediction.Prediction[1], spikePrediction.Prediction[2]);
118-
119-
// Checkpoint the model.
120-
var modelPath = "temp.zip";
121-
engine.CheckPoint(ml, modelPath);
122-
123-
// Load the model.
124-
using (var file = File.OpenRead(modelPath))
125-
model = ml.Model.Load(file);
126-
127-
for (int index = 0; index < 5; index++)
128-
{
129-
// Anomaly spike detection.
130-
var prediction = engine.Predict(new IidSpikeData(5));
131-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 5, prediction.Prediction[0],
132-
prediction.Prediction[1], prediction.Prediction[2]);
133-
}
134-
135-
// Data Alert Score P-Value
136-
// 5 0 5.00 0.50
137-
// 5 0 5.00 0.50
138-
// 5 0 5.00 0.50
139-
// 5 0 5.00 0.50
140-
// 5 0 5.00 0.50
141-
// 10 1 10.00 0.00 <-- alert is on, predicted spike (check-point model)
142-
// 5 0 5.00 0.26 <-- load model from disk.
143-
// 5 0 5.00 0.26
144-
// 5 0 5.00 0.50
145-
// 5 0 5.00 0.50
146-
// 5 0 5.00 0.50
147-
}
14878
}
14979
}
Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using Microsoft.ML.Data;
5-
using Microsoft.ML.Transforms.TimeSeries;
64

75
namespace Microsoft.ML.Samples.Dynamic
86
{
@@ -25,9 +23,8 @@ public SsaChangePointData(float value)
2523
}
2624

2725
// This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
28-
// SsaChangePointDetector is applied then to identify points where data distribution changed.
29-
// SsaChangePointDetector differs from IidChangePointDetector in that it can account for temporal seasonality
30-
// in the data.
26+
// The estimator is applied then to identify points where data distribution changed.
27+
// This estimator can account for temporal seasonality in the data.
3128
public static void Example()
3229
{
3330
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
@@ -49,12 +46,12 @@ public static void Example()
4946
// Convert data to IDataView.
5047
var dataView = ml.Data.LoadFromEnumerable(data);
5148

52-
// Setup SsaChangePointDetector arguments
49+
// Setup estimator arguments
5350
var inputColumnName = nameof(SsaChangePointData.Value);
5451
var outputColumnName = nameof(ChangePointPrediction.Prediction);
5552

5653
// The transformed data.
57-
var transformedData = ml.Transforms.SsaDetectChangePoint(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView).Transform(dataView);
54+
var transformedData = ml.Transforms.DetectChangePointBySsa(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView).Transform(dataView);
5855

5956
// Getting the data of the newly created column as an IEnumerable of ChangePointPrediction.
6057
var predictionColumn = ml.Data.CreateEnumerable<ChangePointPrediction>(transformedData, reuseRowObject: false);
@@ -89,95 +86,5 @@ public static void Example()
8986
// 300 0 270.40 0.01 3578470.47
9087
// 400 0 357.11 0.03 45298370.86
9188
}
92-
93-
// This example shows change point detection as above, but demonstrates how to train a model
94-
// that can run predictions on streaming data, and how to persist the trained model and then re-load it.
95-
public static void SsaChangePointDetectorPrediction()
96-
{
97-
// Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
98-
// as well as the source of randomness.
99-
var ml = new MLContext();
100-
101-
// Generate sample series data with a recurring pattern
102-
const int SeasonalitySize = 5;
103-
const int TrainingSeasons = 3;
104-
const int TrainingSize = SeasonalitySize * TrainingSeasons;
105-
var data = new List<SsaChangePointData>();
106-
for (int i = 0; i < TrainingSeasons; i++)
107-
for (int j = 0; j < SeasonalitySize; j++)
108-
data.Add(new SsaChangePointData(j));
109-
110-
// Convert data to IDataView.
111-
var dataView = ml.Data.LoadFromEnumerable(data);
112-
113-
// Setup SsaChangePointDetector arguments
114-
var inputColumnName = nameof(SsaChangePointData.Value);
115-
var outputColumnName = nameof(ChangePointPrediction.Prediction);
116-
117-
// Train the change point detector.
118-
ITransformer model = ml.Transforms.SsaDetectChangePoint(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView);
119-
120-
// Create a prediction engine from the model for feeding new data.
121-
var engine = model.CreateTimeSeriesPredictionFunction<SsaChangePointData, ChangePointPrediction>(ml);
122-
123-
// Start streaming new data points with no change point to the prediction engine.
124-
Console.WriteLine($"Output from ChangePoint predictions on new data:");
125-
Console.WriteLine("Data\tAlert\tScore\tP-Value\tMartingale value");
126-
ChangePointPrediction prediction = null;
127-
for (int i = 0; i < 5; i++)
128-
{
129-
var value = i;
130-
prediction = engine.Predict(new SsaChangePointData(value));
131-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
132-
}
133-
134-
// Now stream data points that reflect a change in trend.
135-
for (int i = 0; i < 5; i++)
136-
{
137-
var value = (i + 1) * 100;
138-
prediction = engine.Predict(new SsaChangePointData(value));
139-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
140-
}
141-
142-
// Now we demonstrate saving and loading the model.
143-
144-
// Save the model that exists within the prediction engine.
145-
// The engine has been updating this model with every new data point.
146-
var modelPath = "model.zip";
147-
engine.CheckPoint(ml, modelPath);
148-
149-
// Load the model.
150-
using (var file = File.OpenRead(modelPath))
151-
model = ml.Model.Load(file);
152-
153-
// We must create a new prediction engine from the persisted model.
154-
engine = model.CreateTimeSeriesPredictionFunction<SsaChangePointData, ChangePointPrediction>(ml);
155-
156-
// Run predictions on the loaded model.
157-
for (int i = 0; i < 5; i++)
158-
{
159-
var value = (i + 1) * 100;
160-
prediction = engine.Predict(new SsaChangePointData(value));
161-
Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", value, prediction.Prediction[0], prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
162-
}
163-
164-
// Output from ChangePoint predictions on new data:
165-
// Data Alert Score P-Value Martingale value
166-
// 0 0 - 1.01 0.50 0.00
167-
// 1 0 - 0.24 0.22 0.00
168-
// 2 0 - 0.31 0.30 0.00
169-
// 3 0 0.44 0.01 0.00
170-
// 4 0 2.16 0.00 0.24
171-
// 100 0 86.23 0.00 2076098.24
172-
// 200 0 171.38 0.00 809668524.21
173-
// 300 1 256.83 0.01 22130423541.93 <-- alert is on, note that delay is expected
174-
// 400 0 326.55 0.04 241162710263.29
175-
// 500 0 364.82 0.08 597660527041.45 <-- saved to disk
176-
// 100 0 - 58.58 0.15 1096021098844.34 <-- loaded from disk and running new predictions
177-
// 200 0 - 41.24 0.20 97579154688.98
178-
// 300 0 - 30.61 0.24 95319753.87
179-
// 400 0 58.87 0.38 14.24
180-
// 500 0 219.28 0.36 0.05
181-
}
18289
}
18390
}

0 commit comments

Comments
 (0)