1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
3
using Microsoft . ML . Data ;
5
- using Microsoft . ML . Transforms . TimeSeries ;
6
4
7
5
namespace Microsoft . ML . Samples . Dynamic
8
6
{
@@ -25,9 +23,8 @@ public SsaChangePointData(float value)
25
23
}
26
24
27
25
// 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.
31
28
public static void Example ( )
32
29
{
33
30
// 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()
49
46
// Convert data to IDataView.
50
47
var dataView = ml . Data . LoadFromEnumerable ( data ) ;
51
48
52
- // Setup SsaChangePointDetector arguments
49
+ // Setup estimator arguments
53
50
var inputColumnName = nameof ( SsaChangePointData . Value ) ;
54
51
var outputColumnName = nameof ( ChangePointPrediction . Prediction ) ;
55
52
56
53
// 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 ) ;
58
55
59
56
// Getting the data of the newly created column as an IEnumerable of ChangePointPrediction.
60
57
var predictionColumn = ml . Data . CreateEnumerable < ChangePointPrediction > ( transformedData , reuseRowObject : false ) ;
@@ -89,95 +86,5 @@ public static void Example()
89
86
// 300 0 270.40 0.01 3578470.47
90
87
// 400 0 357.11 0.03 45298370.86
91
88
}
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\t Alert\t Score\t P-Value\t Martingale 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
- }
182
89
}
183
90
}
0 commit comments