2
2
using System ;
3
3
using System . IO ;
4
4
using Microsoft . ML ;
5
+ using System . Collections . Generic ;
5
6
// </SnippetAddUsings>
6
7
7
8
namespace ProductSalesAnomalyDetection
@@ -44,17 +45,17 @@ static void DetectSpike(MLContext mlContext, int docSize, IDataView productSales
44
45
var iidSpikeEstimator = mlContext . Transforms . DetectIidSpike ( outputColumnName : nameof ( ProductSalesPrediction . Prediction ) , inputColumnName : nameof ( ProductSalesData . numSales ) , confidence : 95 , pvalueHistoryLength : docSize / 4 ) ;
45
46
// </SnippetAddSpikeTrainer>
46
47
47
- // STEP 3:Train the model by fitting the dataview
48
- // Create and train the model based on the dataset that has been loaded, transformed.
48
+ // STEP 3: Create the transform
49
+ // Create the spike detection transform
49
50
Console . WriteLine ( "=============== Training the model ===============" ) ;
50
51
// <SnippetTrainModel1>
51
- ITransformer trainedModel = iidSpikeEstimator . Fit ( productSales ) ;
52
+ ITransformer iidSpikeTransform = iidSpikeEstimator . Fit ( CreateEmptyDataView ( mlContext ) ) ;
52
53
// </SnippetTrainModel1>
53
54
54
55
Console . WriteLine ( "=============== End of training process ===============" ) ;
55
56
//Apply data transformation to create predictions.
56
57
// <SnippetTransformData1>
57
- IDataView transformedData = trainedModel . Transform ( productSales ) ;
58
+ IDataView transformedData = iidSpikeTransform . Transform ( productSales ) ;
58
59
// </SnippetTransformData1>
59
60
60
61
// <SnippetCreateEnumerable1>
@@ -90,16 +91,16 @@ static void DetectChangepoint(MLContext mlContext, int docSize, IDataView produc
90
91
var iidChangePointEstimator = mlContext . Transforms . DetectIidChangePoint ( outputColumnName : nameof ( ProductSalesPrediction . Prediction ) , inputColumnName : nameof ( ProductSalesData . numSales ) , confidence : 95 , changeHistoryLength : docSize / 4 ) ;
91
92
// </SnippetAddChangePointTrainer>
92
93
93
- //STEP 3:Train the model by fitting the dataview
94
+ //STEP 3: Create the transform
94
95
Console . WriteLine ( "=============== Training the model Using Change Point Detection Algorithm===============" ) ;
95
96
// <SnippetTrainModel2>
96
- var trainedModel = iidChangePointEstimator . Fit ( productSales ) ;
97
+ var iidChangePointTransform = iidChangePointEstimator . Fit ( CreateEmptyDataView ( mlContext ) ) ;
97
98
// </SnippetTrainModel2>
98
99
Console . WriteLine ( "=============== End of training process ===============" ) ;
99
100
100
101
//Apply data transformation to create predictions.
101
102
// <SnippetTransformData2>
102
- IDataView transformedData = trainedModel . Transform ( productSales ) ;
103
+ IDataView transformedData = iidChangePointTransform . Transform ( productSales ) ;
103
104
// </SnippetTransformData2>
104
105
105
106
// <SnippetCreateEnumerable2>
@@ -124,5 +125,13 @@ static void DetectChangepoint(MLContext mlContext, int docSize, IDataView produc
124
125
Console . WriteLine ( "" ) ;
125
126
// </SnippetDisplayResults2>
126
127
}
128
+
129
+ // <SnippetCreateEmptyDataView>
130
+ static IDataView CreateEmptyDataView ( MLContext mlContext ) {
131
+ // Create empty DataView. We just need the schema to call Fit() for the time series transforms
132
+ IEnumerable < ProductSalesData > enumerableData = new List < ProductSalesData > ( ) ;
133
+ return mlContext . Data . LoadFromEnumerable ( enumerableData ) ;
134
+ }
135
+ // </SnippetCreateEmptyDataView>
127
136
}
128
137
}
0 commit comments