|
4 | 4 | using Microsoft.ML.Runtime.Data;
|
5 | 5 | using Microsoft.ML.Runtime.Api;
|
6 | 6 | using Microsoft.ML.Runtime.TimeSeriesProcessing;
|
| 7 | +using Microsoft.ML.Core.Data; |
| 8 | +using Microsoft.ML.TimeSeries; |
| 9 | +using System.IO; |
| 10 | +using Microsoft.ML.Data; |
7 | 11 |
|
8 | 12 | namespace Microsoft.ML.Samples.Dynamic
|
9 | 13 | {
|
@@ -34,26 +38,26 @@ public static void IidChangePointDetectorTransform()
|
34 | 38 | var ml = new MLContext();
|
35 | 39 |
|
36 | 40 | // Generate sample series data with a change
|
37 |
| - const int size = 16; |
38 |
| - var data = new List<IidChangePointData>(size); |
39 |
| - for (int i = 0; i < size / 2; i++) |
| 41 | + const int Size = 16; |
| 42 | + var data = new List<IidChangePointData>(Size); |
| 43 | + for (int i = 0; i < Size / 2; i++) |
40 | 44 | data.Add(new IidChangePointData(5));
|
41 | 45 | // This is a change point
|
42 |
| - for (int i = 0; i < size / 2; i++) |
| 46 | + for (int i = 0; i < Size / 2; i++) |
43 | 47 | data.Add(new IidChangePointData(7));
|
44 | 48 |
|
45 | 49 | // Convert data to IDataView.
|
46 | 50 | var dataView = ml.CreateStreamingDataView(data);
|
47 | 51 |
|
48 | 52 | // Setup IidSpikeDetector arguments
|
49 |
| - string outputColumnName = "Prediction"; |
50 |
| - string inputColumnName = "Value"; |
| 53 | + string outputColumnName = nameof(ChangePointPrediction.Prediction); |
| 54 | + string inputColumnName = nameof(IidChangePointData.Value); |
51 | 55 | var args = new IidChangePointDetector.Arguments()
|
52 | 56 | {
|
53 | 57 | Source = inputColumnName,
|
54 | 58 | Name = outputColumnName,
|
55 | 59 | Confidence = 95, // The confidence for spike detection in the range [0, 100]
|
56 |
| - ChangeHistoryLength = size / 4, // The length of the sliding window on p-values for computing the martingale score. |
| 60 | + ChangeHistoryLength = Size / 4, // The length of the sliding window on p-values for computing the martingale score. |
57 | 61 | };
|
58 | 62 |
|
59 | 63 | // The transformed data.
|
@@ -88,5 +92,116 @@ public static void IidChangePointDetectorTransform()
|
88 | 92 | // 7 0 7.00 0.50 0.00
|
89 | 93 | // 7 0 7.00 0.50 0.00
|
90 | 94 | }
|
| 95 | + |
| 96 | + // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot). |
| 97 | + // IidChangePointDetector is applied then to identify points where data distribution changed using time series |
| 98 | + // prediction engine. The engine is checkpointed and then loaded back from disk into memory and used for prediction. |
| 99 | + public static void IidChangePointDetectorPrediction() |
| 100 | + { |
| 101 | + // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging, |
| 102 | + // as well as the source of randomness. |
| 103 | + var ml = new MLContext(); |
| 104 | + |
| 105 | + // Generate sample series data with a change |
| 106 | + const int Size = 16; |
| 107 | + var data = new List<IidChangePointData>(Size); |
| 108 | + for (int i = 0; i < Size / 2; i++) |
| 109 | + data.Add(new IidChangePointData(5)); |
| 110 | + // This is a change point |
| 111 | + for (int i = 0; i < Size / 2; i++) |
| 112 | + data.Add(new IidChangePointData(7)); |
| 113 | + |
| 114 | + // Convert data to IDataView. |
| 115 | + var dataView = ml.CreateStreamingDataView(data); |
| 116 | + |
| 117 | + // Setup IidSpikeDetector arguments |
| 118 | + string outputColumnName = nameof(ChangePointPrediction.Prediction); |
| 119 | + string inputColumnName = nameof(IidChangePointData.Value); |
| 120 | + var args = new IidChangePointDetector.Arguments() |
| 121 | + { |
| 122 | + Source = inputColumnName, |
| 123 | + Name = outputColumnName, |
| 124 | + Confidence = 95, // The confidence for spike detection in the range [0, 100] |
| 125 | + ChangeHistoryLength = Size / 4, // The length of the sliding window on p-values for computing the martingale score. |
| 126 | + }; |
| 127 | + |
| 128 | + // Time Series model. |
| 129 | + ITransformer model = new IidChangePointEstimator(ml, args).Fit(dataView); |
| 130 | + |
| 131 | + // Create a time series prediction engine from the model. |
| 132 | + var engine = model.CreateTimeSeriesPredictionFunction<IidChangePointData, ChangePointPrediction>(ml); |
| 133 | + for(int index = 0; index < 8; index++) |
| 134 | + { |
| 135 | + // Anomaly change point detection. |
| 136 | + var prediction = engine.Predict(new IidChangePointData(5)); |
| 137 | + Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 5, prediction.Prediction[0], |
| 138 | + prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); |
| 139 | + } |
| 140 | + |
| 141 | + // Change point |
| 142 | + var changePointPrediction = engine.Predict(new IidChangePointData(7)); |
| 143 | + Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, changePointPrediction.Prediction[0], |
| 144 | + changePointPrediction.Prediction[1], changePointPrediction.Prediction[2], changePointPrediction.Prediction[3]); |
| 145 | + |
| 146 | + // Checkpoint the model. |
| 147 | + var modelPath = "temp.zip"; |
| 148 | + engine.CheckPoint(ml, modelPath); |
| 149 | + |
| 150 | + // Reference to current time series engine because in the next step "engine" will point to the |
| 151 | + // checkpointed model being loaded from disk. |
| 152 | + var timeseries1 = engine; |
| 153 | + |
| 154 | + // Load the model. |
| 155 | + using (var file = File.OpenRead(modelPath)) |
| 156 | + model = TransformerChain.LoadFrom(ml, file); |
| 157 | + |
| 158 | + // Create a time series prediction engine from the checkpointed model. |
| 159 | + engine = model.CreateTimeSeriesPredictionFunction<IidChangePointData, ChangePointPrediction>(ml); |
| 160 | + for (int index = 0; index < 8; index++) |
| 161 | + { |
| 162 | + // Anomaly change point detection. |
| 163 | + var prediction = engine.Predict(new IidChangePointData(7)); |
| 164 | + Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0], |
| 165 | + prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); |
| 166 | + } |
| 167 | + |
| 168 | + // Prediction from the original time series engine should match the prediction from |
| 169 | + // check pointed model. |
| 170 | + engine = timeseries1; |
| 171 | + for (int index = 0; index < 8; index++) |
| 172 | + { |
| 173 | + // Anomaly change point detection. |
| 174 | + var prediction = engine.Predict(new IidChangePointData(7)); |
| 175 | + Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0], |
| 176 | + prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]); |
| 177 | + } |
| 178 | + |
| 179 | + // Data Alert Score P-Value Martingale value |
| 180 | + // 5 0 5.00 0.50 0.00 <-- Time Series 1. |
| 181 | + // 5 0 5.00 0.50 0.00 |
| 182 | + // 5 0 5.00 0.50 0.00 |
| 183 | + // 5 0 5.00 0.50 0.00 |
| 184 | + // 5 0 5.00 0.50 0.00 |
| 185 | + // 5 0 5.00 0.50 0.00 |
| 186 | + // 5 0 5.00 0.50 0.00 |
| 187 | + // 5 0 5.00 0.50 0.00 |
| 188 | + // 7 1 7.00 0.00 10298.67 <-- alert is on, predicted changepoint (and model is checkpointed). |
| 189 | + |
| 190 | + // 7 0 7.00 0.13 33950.16 <-- Time Series 2 : Model loaded back from disk and prediction is made. |
| 191 | + // 7 0 7.00 0.26 60866.34 |
| 192 | + // 7 0 7.00 0.38 78362.04 |
| 193 | + // 7 0 7.00 0.50 0.01 |
| 194 | + // 7 0 7.00 0.50 0.00 |
| 195 | + // 7 0 7.00 0.50 0.00 |
| 196 | + // 7 0 7.00 0.50 0.00 |
| 197 | + |
| 198 | + // 7 0 7.00 0.13 33950.16 <-- Time Series 1 and prediction is made. |
| 199 | + // 7 0 7.00 0.26 60866.34 |
| 200 | + // 7 0 7.00 0.38 78362.04 |
| 201 | + // 7 0 7.00 0.50 0.01 |
| 202 | + // 7 0 7.00 0.50 0.00 |
| 203 | + // 7 0 7.00 0.50 0.00 |
| 204 | + // 7 0 7.00 0.50 0.00 |
| 205 | + } |
91 | 206 | }
|
92 | 207 | }
|
0 commit comments