예제 #1
0
        public void ChangePointDetectionWithSeasonalityPredictionEngine()
        {
            const int ChangeHistorySize         = 10;
            const int SeasonalitySize           = 10;
            const int NumberOfSeasonsInTraining = 5;
            const int MaxTrainingSize           = NumberOfSeasonsInTraining * SeasonalitySize;

            List <Data> data = new List <Data>();

            var ml       = new MLContext(seed: 1, conc: 1);
            var dataView = ml.CreateStreamingDataView(data);

            for (int j = 0; j < NumberOfSeasonsInTraining; j++)
            {
                for (int i = 0; i < SeasonalitySize; i++)
                {
                    data.Add(new Data(i));
                }
            }

            for (int i = 0; i < ChangeHistorySize; i++)
            {
                data.Add(new Data(i * 100));
            }


            // Pipeline.
            var pipeline = ml.Transforms.Text.FeaturizeText("Text", "Text_Featurized")
                           .Append(new SsaChangePointEstimator(ml, new SsaChangePointDetector.Arguments()
            {
                Confidence          = 95,
                Source              = "Value",
                Name                = "Change",
                ChangeHistoryLength = ChangeHistorySize,
                TrainingWindowSize  = MaxTrainingSize,
                SeasonalWindowSize  = SeasonalitySize
            }));

            // Train.
            var model = pipeline.Fit(dataView);

            //Model 1: Prediction #1.
            var engine     = model.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml);
            var prediction = engine.Predict(new Data(1));

            Assert.Equal(0, prediction.Change[0], precision: 7);                      // Alert
            Assert.Equal(1.1661833524703979, prediction.Change[1], precision: 5);     // Raw score
            Assert.Equal(0.5, prediction.Change[2], precision: 7);                    // P-Value score
            Assert.Equal(5.1200000000000114E-08, prediction.Change[3], precision: 7); // Martingale score

            //Model 1: Checkpoint.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            //Model 1: Prediction #2
            prediction = engine.Predict(new Data(1));
            Assert.Equal(0, prediction.Change[0], precision: 7);                      // Alert
            Assert.Equal(0.12216401100158691, prediction.Change[1], precision: 5);    // Raw score
            Assert.Equal(0.14823824685192111, prediction.Change[2], precision: 5);    // P-Value score
            Assert.Equal(1.5292508189989167E-07, prediction.Change[3], precision: 7); // Martingale score

            // Load Model 1.
            ITransformer model2 = null;

            using (var file = File.OpenRead(modelPath))
                model2 = TransformerChain.LoadFrom(ml, file);

            //Predict and expect the same result after checkpointing(Prediction #2).
            engine     = model2.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml);
            prediction = engine.Predict(new Data(1));
            Assert.Equal(0, prediction.Change[0], precision: 7);                      // Alert
            Assert.Equal(0.12216401100158691, prediction.Change[1], precision: 5);    // Raw score
            Assert.Equal(0.14823824685192111, prediction.Change[2], precision: 5);    // P-Value score
            Assert.Equal(1.5292508189989167E-07, prediction.Change[3], precision: 5); // Martingale score
        }
예제 #2
0
        // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
        // The estimator is applied then to identify spiking points in the series.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a spike
            const int Size = 10;
            var       data = new List <TimeSeriesData>(Size + 1)
            {
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),

                // This is a spike.
                new TimeSeriesData(10),

                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
            };

            // Convert data to IDataView.
            var dataView = ml.Data.LoadFromEnumerable(data);

            // Setup IidSpikeDetector arguments
            string outputColumnName = nameof(IidSpikePrediction.Prediction);
            string inputColumnName  = nameof(TimeSeriesData.Value);

            // The transformed model.
            ITransformer model = ml.Transforms.DetectIidSpike(outputColumnName, inputColumnName, 95, Size).Fit(dataView);

            // Create a time series prediction engine from the model.
            var engine = model.CreateTimeSeriesPredictionFunction <TimeSeriesData, IidSpikePrediction>(ml);

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

            // Prediction column obtained post-transformation.
            // Data Alert   Score   P-Value

            // Create non-anomalous data and check for anomaly.
            for (int index = 0; index < 5; index++)
            {
                // Anomaly spike detection.
                PrintPrediction(5, engine.Predict(new TimeSeriesData(5)));
            }

            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50

            // Spike.
            PrintPrediction(10, engine.Predict(new TimeSeriesData(10)));

            // 10     1      10.00    0.00  <-- alert is on, predicted spike (check-point model)

            // Checkpoint the model.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = ml.Model.Load(file, out DataViewSchema schema);

            for (int index = 0; index < 5; index++)
            {
                // Anomaly spike detection.
                PrintPrediction(5, engine.Predict(new TimeSeriesData(5)));
            }

            // 5      0       5.00    0.26  <-- load model from disk.
            // 5      0       5.00    0.26
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
        }
예제 #3
0
        public void ChangePointDetectionWithSeasonalityPredictionEngineNoColumn()
        {
            const int ChangeHistorySize         = 10;
            const int SeasonalitySize           = 10;
            const int NumberOfSeasonsInTraining = 5;
            const int MaxTrainingSize           = NumberOfSeasonsInTraining * SeasonalitySize;

            List <Data> data = new List <Data>();

            var ml       = new MLContext(seed: 1, conc: 1);
            var dataView = ml.CreateStreamingDataView(data);

            for (int j = 0; j < NumberOfSeasonsInTraining; j++)
            {
                for (int i = 0; i < SeasonalitySize; i++)
                {
                    data.Add(new Data(i));
                }
            }

            for (int i = 0; i < ChangeHistorySize; i++)
            {
                data.Add(new Data(i * 100));
            }


            // Pipeline.
            var pipeline = ml.Transforms.Text.FeaturizeText("Text", "Text_Featurized")
                           .Append(new SsaChangePointEstimator(ml, new SsaChangePointDetector.Arguments()
            {
                Confidence          = 95,
                Source              = "Value",
                Name                = "Change",
                ChangeHistoryLength = ChangeHistorySize,
                TrainingWindowSize  = MaxTrainingSize,
                SeasonalWindowSize  = SeasonalitySize
            }));

            // Train.
            var model = pipeline.Fit(dataView);

            //Create prediction function.
            var engine = model.CreateTimeSeriesPredictionFunction <Data, Prediction1>(ml);

            //Checkpoint with no inputs passed at prediction.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            //Load time series model and we will use this to pass two inputs and compare the raw score
            //with "engine".
            ITransformer model2 = null;

            using (var file = File.OpenRead(modelPath))
                model2 = TransformerChain.LoadFrom(ml, file);

            //Raw score after state gets updated with two inputs.
            var engine2     = model2.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml);
            var prediction2 = engine2.Predict(new Data(1));

            //Raw score after first input.
            Assert.Equal(1.1661833524703979, prediction2.Change[1], precision: 5); // Raw score
            prediction2 = engine2.Predict(new Data(1));
            //Raw score after second input.
            Assert.Equal(0.12216401100158691, prediction2.Change[1], precision: 5); // Raw score

            //Even though time series column is not requested it will
            // pass the observation through time series transform and update the state with the first input.
            var prediction = engine.Predict(new Data(1));

            Assert.Equal(-1, prediction.Random);

            //Save the model with state updated with just one input.
            engine.CheckPoint(ml, modelPath + 1);
            ITransformer model3 = null;

            using (var file = File.OpenRead(modelPath + 1))
                model3 = TransformerChain.LoadFrom(ml, file);

            //Load the model with state updated with just one input, then pass in the second input
            //and raw score should match the raw score obtained by passing the two input in the first model.
            var engine3     = model3.CreateTimeSeriesPredictionFunction <Data, Prediction>(ml);
            var prediction3 = engine3.Predict(new Data(1));

            Assert.Equal(0.12216401100158691, prediction2.Change[1], precision: 5); // Raw score
        }
예제 #4
0
        // This example shows change point detection as above, but demonstrates how to train a model
        // that can run predictions on streaming data, and how to persist the trained model and then re-load it.
        public static void SsaChangePointDetectorPrediction()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a recurring pattern
            const int SeasonalitySize = 5;
            const int TrainingSeasons = 3;
            const int TrainingSize    = SeasonalitySize * TrainingSeasons;
            var       data            = new List <SsaChangePointData>();

            for (int i = 0; i < TrainingSeasons; i++)
            {
                for (int j = 0; j < SeasonalitySize; j++)
                {
                    data.Add(new SsaChangePointData(j));
                }
            }

            // Convert data to IDataView.
            var dataView = ml.Data.LoadFromEnumerable(data);

            // Setup SsaChangePointDetector arguments
            var inputColumnName  = nameof(SsaChangePointData.Value);
            var outputColumnName = nameof(ChangePointPrediction.Prediction);

            // Train the change point detector.
            ITransformer model = ml.Transforms.SsaChangePointEstimator(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView);

            // Create a prediction engine from the model for feeding new data.
            var engine = model.CreateTimeSeriesPredictionFunction <SsaChangePointData, ChangePointPrediction>(ml);

            // Start streaming new data points with no change point to the prediction engine.
            Console.WriteLine($"Output from ChangePoint predictions on new data:");
            Console.WriteLine("Data\tAlert\tScore\tP-Value\tMartingale value");
            ChangePointPrediction prediction = null;

            for (int i = 0; i < 5; i++)
            {
                var value = i;
                prediction = engine.Predict(new SsaChangePointData(value));
                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]);
            }

            // Now stream data points that reflect a change in trend.
            for (int i = 0; i < 5; i++)
            {
                var value = (i + 1) * 100;
                prediction = engine.Predict(new SsaChangePointData(value));
                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]);
            }

            // Now we demonstrate saving and loading the model.

            // Save the model that exists within the prediction engine.
            // The engine has been updating this model with every new data point.
            var modelPath = "model.zip";

            engine.CheckPoint(ml, modelPath);

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = TransformerChain.LoadFrom(ml, file);

            // We must create a new prediction engine from the persisted model.
            engine = model.CreateTimeSeriesPredictionFunction <SsaChangePointData, ChangePointPrediction>(ml);

            // Run predictions on the loaded model.
            for (int i = 0; i < 5; i++)
            {
                var value = (i + 1) * 100;
                prediction = engine.Predict(new SsaChangePointData(value));
                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]);
            }

            // Output from ChangePoint predictions on new data:
            // Data    Alert   Score   P-Value Martingale value
            // 0       0     - 1.01    0.50    0.00
            // 1       0     - 0.24    0.22    0.00
            // 2       0     - 0.31    0.30    0.00
            // 3       0       0.44    0.01    0.00
            // 4       0       2.16    0.00    0.24
            // 100     0      86.23    0.00    2076098.24
            // 200     0     171.38    0.00    809668524.21
            // 300     1     256.83    0.01    22130423541.93    <-- alert is on, note that delay is expected
            // 400     0     326.55    0.04    241162710263.29
            // 500     0     364.82    0.08    597660527041.45   <-- saved to disk
            // 100     0    - 58.58    0.15    1096021098844.34  <-- loaded from disk and running new predictions
            // 200     0    - 41.24    0.20    97579154688.98
            // 300     0    - 30.61    0.24    95319753.87
            // 400     0      58.87    0.38    14.24
            // 500     0     219.28    0.36    0.05
        }
예제 #5
0
        // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
        // IidChangePointDetector is applied then to identify points where data distribution changed using time series
        // prediction engine. The engine is checkpointed and then loaded back from disk into memory and used for prediction.
        public static void IidChangePointDetectorPrediction()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a change
            const int Size = 16;
            var       data = new List <IidChangePointData>(Size);

            for (int i = 0; i < Size / 2; i++)
            {
                data.Add(new IidChangePointData(5));
            }
            // This is a change point
            for (int i = 0; i < Size / 2; i++)
            {
                data.Add(new IidChangePointData(7));
            }

            // Convert data to IDataView.
            var dataView = ml.Data.ReadFromEnumerable(data);

            // Setup IidSpikeDetector arguments
            string outputColumnName = nameof(ChangePointPrediction.Prediction);
            string inputColumnName  = nameof(IidChangePointData.Value);

            // Time Series model.
            ITransformer model = ml.Transforms.IidChangePointEstimator(outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView);

            // Create a time series prediction engine from the model.
            var engine = model.CreateTimeSeriesPredictionFunction <IidChangePointData, ChangePointPrediction>(ml);

            for (int index = 0; index < 8; index++)
            {
                // Anomaly change point detection.
                var prediction = engine.Predict(new IidChangePointData(5));
                Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 5, prediction.Prediction[0],
                                  prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
            }

            // Change point
            var changePointPrediction = engine.Predict(new IidChangePointData(7));

            Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, changePointPrediction.Prediction[0],
                              changePointPrediction.Prediction[1], changePointPrediction.Prediction[2], changePointPrediction.Prediction[3]);

            // Checkpoint the model.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            // Reference to current time series engine because in the next step "engine" will point to the
            // checkpointed model being loaded from disk.
            var timeseries1 = engine;

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = TransformerChain.LoadFrom(ml, file);

            // Create a time series prediction engine from the checkpointed model.
            engine = model.CreateTimeSeriesPredictionFunction <IidChangePointData, ChangePointPrediction>(ml);
            for (int index = 0; index < 8; index++)
            {
                // Anomaly change point detection.
                var prediction = engine.Predict(new IidChangePointData(7));
                Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0],
                                  prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
            }

            // Prediction from the original time series engine should match the prediction from
            // check pointed model.
            engine = timeseries1;
            for (int index = 0; index < 8; index++)
            {
                // Anomaly change point detection.
                var prediction = engine.Predict(new IidChangePointData(7));
                Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}\t{4:0.00}", 7, prediction.Prediction[0],
                                  prediction.Prediction[1], prediction.Prediction[2], prediction.Prediction[3]);
            }

            // Data Alert      Score   P-Value Martingale value
            // 5       0       5.00    0.50    0.00       <-- Time Series 1.
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 7       1       7.00    0.00    10298.67   <-- alert is on, predicted changepoint (and model is checkpointed).

            // 7       0       7.00    0.13    33950.16   <-- Time Series 2 : Model loaded back from disk and prediction is made.
            // 7       0       7.00    0.26    60866.34
            // 7       0       7.00    0.38    78362.04
            // 7       0       7.00    0.50    0.01
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00

            // 7       0       7.00    0.13    33950.16   <-- Time Series 1 and prediction is made.
            // 7       0       7.00    0.26    60866.34
            // 7       0       7.00    0.38    78362.04
            // 7       0       7.00    0.50    0.01
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00
        }
예제 #6
0
        public static void IidSpikeDetectorPrediction()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a spike
            const int Size = 10;
            var       data = new List <IidSpikeData>(Size);

            for (int i = 0; i < Size / 2; i++)
            {
                data.Add(new IidSpikeData(5));
            }
            // This is a spike
            data.Add(new IidSpikeData(10));
            for (int i = 0; i < Size / 2; i++)
            {
                data.Add(new IidSpikeData(5));
            }

            // Convert data to IDataView.
            var dataView = ml.Data.ReadFromEnumerable(data);

            // Setup IidSpikeDetector arguments
            string outputColumnName = nameof(IidSpikePrediction.Prediction);
            string inputColumnName  = nameof(IidSpikeData.Value);
            // The transformed model.
            ITransformer model = ml.Transforms.IidChangePointEstimator(outputColumnName, inputColumnName, 95, Size).Fit(dataView);

            // Create a time series prediction engine from the model.
            var engine = model.CreateTimeSeriesPredictionFunction <IidSpikeData, IidSpikePrediction>(ml);

            for (int index = 0; index < 5; index++)
            {
                // Anomaly spike detection.
                var prediction = engine.Predict(new IidSpikeData(5));
                Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 5, prediction.Prediction[0],
                                  prediction.Prediction[1], prediction.Prediction[2]);
            }

            // Spike.
            var spikePrediction = engine.Predict(new IidSpikeData(10));

            Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 10, spikePrediction.Prediction[0],
                              spikePrediction.Prediction[1], spikePrediction.Prediction[2]);

            // Checkpoint the model.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = TransformerChain.LoadFrom(ml, file);

            for (int index = 0; index < 5; index++)
            {
                // Anomaly spike detection.
                var prediction = engine.Predict(new IidSpikeData(5));
                Console.WriteLine("{0}\t{1}\t{2:0.00}\t{3:0.00}", 5, prediction.Prediction[0],
                                  prediction.Prediction[1], prediction.Prediction[2]);
            }

            // Data Alert   Score   P-Value
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 10     1      10.00    0.00  <-- alert is on, predicted spike (check-point model)
            // 5      0       5.00    0.26  <-- load model from disk.
            // 5      0       5.00    0.26
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
            // 5      0       5.00    0.50
        }
예제 #7
0
        // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
        // The estimator is applied then to identify spiking points in the series.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with an anomaly
            var data = new List <TimeSeriesData>();

            for (int index = 0; index < 20; index++)
            {
                data.Add(new TimeSeriesData(5));
            }
            data.Add(new TimeSeriesData(10));
            for (int index = 0; index < 5; index++)
            {
                data.Add(new TimeSeriesData(5));
            }

            // Convert data to IDataView.
            var dataView = ml.Data.LoadFromEnumerable(data);

            // Setup the estimator arguments
            string outputColumnName = nameof(SrCnnAnomalyDetection.Prediction);
            string inputColumnName  = nameof(TimeSeriesData.Value);

            // The transformed model.
            ITransformer model = ml.Transforms.DetectAnomalyBySrCnn(outputColumnName, inputColumnName, 16, 5, 5, 3, 8, 0.35).Fit(dataView);

            // Create a time series prediction engine from the model.
            var engine = model.CreateTimeSeriesPredictionFunction <TimeSeriesData, SrCnnAnomalyDetection>(ml);

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

            // Prediction column obtained post-transformation.
            // Data	Alert	Score	Mag

            // Create non-anomalous data and check for anomaly.
            for (int index = 0; index < 20; index++)
            {
                // Anomaly detection.
                PrintPrediction(5, engine.Predict(new TimeSeriesData(5)));
            }

            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.00    0.00
            //5   0   0.03    0.18
            //5   0   0.03    0.18
            //5   0   0.03    0.18
            //5   0   0.03    0.18
            //5   0   0.03    0.18

            // Anomaly.
            PrintPrediction(10, engine.Predict(new TimeSeriesData(10)));

            //10	1	0.47	0.93    <-- alert is on, predicted anomaly

            // Checkpoint the model.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = ml.Model.Load(file, out DataViewSchema schema);

            for (int index = 0; index < 5; index++)
            {
                // Anomaly detection.
                PrintPrediction(5, engine.Predict(new TimeSeriesData(5)));
            }

            //5   0   0.31    0.50
            //5   0   0.05    0.30
            //5   0   0.01    0.23
            //5   0   0.00    0.21
            //5   0   0.01    0.25
        }
예제 #8
0
        // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
        // The estimator is applied then to identify spiking points in the series.
        // This estimator can account for temporal seasonality in the data.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a recurring pattern
            const int SeasonalitySize = 5;
            const int TrainingSeasons = 3;
            const int TrainingSize    = SeasonalitySize * TrainingSeasons;
            var       data            = new List <TimeSeriesData>()
            {
                new TimeSeriesData(0),
                new TimeSeriesData(1),
                new TimeSeriesData(2),
                new TimeSeriesData(3),
                new TimeSeriesData(4),

                new TimeSeriesData(0),
                new TimeSeriesData(1),
                new TimeSeriesData(2),
                new TimeSeriesData(3),
                new TimeSeriesData(4),

                new TimeSeriesData(0),
                new TimeSeriesData(1),
                new TimeSeriesData(2),
                new TimeSeriesData(3),
                new TimeSeriesData(4),
            };

            // Convert data to IDataView.
            var dataView = ml.Data.LoadFromEnumerable(data);

            // Setup IidSpikeDetector arguments
            var inputColumnName  = nameof(TimeSeriesData.Value);
            var outputColumnName = nameof(SsaSpikePrediction.Prediction);

            // Train the change point detector.
            ITransformer model = ml.Transforms.DetectSpikeBySsa(outputColumnName, inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(dataView);

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

            // Start streaming new data points with no change point to the prediction engine.
            Console.WriteLine($"Output from spike predictions on new data:");
            Console.WriteLine("Data\tAlert\tScore\tP-Value");

            // Output from spike predictions on new data:
            // Data    Alert   Score   P-Value

            for (int j = 0; j < 2; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    PrintPrediction(i, engine.Predict(new TimeSeriesData(i)));
                }
            }

            // 0       0      -1.01    0.50
            // 1       0      -0.24    0.22
            // 2       0      -0.31    0.30
            // 3       0       0.44    0.01
            // 4       0       2.16    0.00
            // 0       0      -0.78    0.27
            // 1       0      -0.80    0.30
            // 2       0      -0.84    0.31
            // 3       0       0.33    0.31
            // 4       0       2.21    0.07

            // Now send a data point that reflects a spike.
            PrintPrediction(100, engine.Predict(new TimeSeriesData(100)));

            // 100     1      86.17    0.00   <-- alert is on, predicted spike

            // Now we demonstrate saving and loading the model.
            // Save the model that exists within the prediction engine.
            // The engine has been updating this model with every new data point.
            var modelPath = "model.zip";

            engine.CheckPoint(ml, modelPath);

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = ml.Model.Load(file, out DataViewSchema schema);

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

            // Run predictions on the loaded model.
            for (int i = 0; i < 5; i++)
            {
                PrintPrediction(i, engine.Predict(new TimeSeriesData(i)));
            }

            // 0       0      -2.74    0.40   <-- saved to disk, re-loaded, and running new predictions
            // 1       0      -1.47    0.42
            // 2       0     -17.50    0.24
            // 3       0     -30.82    0.16
            // 4       0     -23.24    0.28
        }
예제 #9
0
        // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
        // It demonstrates stateful prediction engine that updates the state of the model and allows for saving/reloading.
        // The estimator is applied then to identify points where data distribution changed.
        // This estimator can account for temporal seasonality in the data.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a recurring pattern
            const int SeasonalitySize = 5;
            const int TrainingSeasons = 3;
            const int TrainingSize    = SeasonalitySize * TrainingSeasons;
            var       data            = new List <TimeSeriesData>()
            {
                new TimeSeriesData(0),
                new TimeSeriesData(1),
                new TimeSeriesData(2),
                new TimeSeriesData(3),
                new TimeSeriesData(4),

                new TimeSeriesData(0),
                new TimeSeriesData(1),
                new TimeSeriesData(2),
                new TimeSeriesData(3),
                new TimeSeriesData(4),

                new TimeSeriesData(0),
                new TimeSeriesData(1),
                new TimeSeriesData(2),
                new TimeSeriesData(3),
                new TimeSeriesData(4),
            };

            // Convert data to IDataView.
            var dataView = ml.Data.LoadFromEnumerable(data);

            // Setup SsaChangePointDetector arguments
            var inputColumnName     = nameof(TimeSeriesData.Value);
            var outputColumnName    = nameof(ChangePointPrediction.Prediction);
            int confidence          = 95;
            int changeHistoryLength = 8;

            // Train the change point detector.
            ITransformer model = ml.Transforms.DetectChangePointBySsa(outputColumnName, inputColumnName, confidence, changeHistoryLength, TrainingSize, SeasonalitySize + 1).Fit(dataView);

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

            // Start streaming new data points with no change point to the prediction engine.
            Console.WriteLine($"Output from ChangePoint predictions on new data:");
            Console.WriteLine("Data\tAlert\tScore\tP-Value\tMartingale value");

            // Output from ChangePoint predictions on new data:
            // Data    Alert   Score   P-Value Martingale value

            for (int i = 0; i < 5; i++)
            {
                PrintPrediction(i, engine.Predict(new TimeSeriesData(i)));
            }

            // 0       0      -1.01    0.50    0.00
            // 1       0      -0.24    0.22    0.00
            // 2       0      -0.31    0.30    0.00
            // 3       0       0.44    0.01    0.00
            // 4       0       2.16    0.00    0.24

            // Now stream data points that reflect a change in trend.
            for (int i = 0; i < 5; i++)
            {
                int value = (i + 1) * 100;
                PrintPrediction(value, engine.Predict(new TimeSeriesData(value)));
            }
            // 100     0      86.23    0.00    2076098.24
            // 200     0     171.38    0.00    809668524.21
            // 300     1     256.83    0.01    22130423541.93    <-- alert is on, note that delay is expected
            // 400     0     326.55    0.04    241162710263.29
            // 500     0     364.82    0.08    597660527041.45   <-- saved to disk

            // Now we demonstrate saving and loading the model.

            // Save the model that exists within the prediction engine.
            // The engine has been updating this model with every new data point.
            byte[] modelBytes;
            using (var stream = new MemoryStream())
            {
                engine.CheckPoint(ml, stream);
                modelBytes = stream.ToArray();
            }

            // Load the model.
            using (var stream = new MemoryStream(modelBytes))
                model = ml.Model.Load(stream, out DataViewSchema schema);

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

            // Run predictions on the loaded model.
            for (int i = 0; i < 5; i++)
            {
                int value = (i + 1) * 100;
                PrintPrediction(value, engine.Predict(new TimeSeriesData(value)));
            }

            // 100     0     -58.58    0.15    1096021098844.34  <-- loaded from disk and running new predictions
            // 200     0     -41.24    0.20    97579154688.98
            // 300     0     -30.61    0.24    95319753.87
            // 400     0      58.87    0.38    14.24
            // 500     0     219.28    0.36    0.05
        }
        // This example creates a time series (list of Data with the i-th element corresponding to the i-th time slot).
        // The estimator is applied then to identify points where data distribution changed.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for exception tracking and logging,
            // as well as the source of randomness.
            var ml = new MLContext();

            // Generate sample series data with a change
            const int Size = 16;
            var       data = new List <TimeSeriesData>(Size)
            {
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),
                new TimeSeriesData(5),

                //Change point data.
                new TimeSeriesData(7),
                new TimeSeriesData(7),
                new TimeSeriesData(7),
                new TimeSeriesData(7),
                new TimeSeriesData(7),
                new TimeSeriesData(7),
                new TimeSeriesData(7),
                new TimeSeriesData(7),
            };

            // Convert data to IDataView.
            var dataView = ml.Data.LoadFromEnumerable(data);

            // Setup IidSpikeDetector arguments
            string outputColumnName = nameof(ChangePointPrediction.Prediction);
            string inputColumnName  = nameof(TimeSeriesData.Value);

            // Time Series model.
            ITransformer model = ml.Transforms.DetectIidChangePoint(outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView);

            // Create a time series prediction engine from the model.
            var engine = model.CreateTimeSeriesPredictionFunction <TimeSeriesData, ChangePointPrediction>(ml);

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

            // Data Alert      Score   P-Value Martingale value

            // Create non-anomalous data and check for change point.
            for (int index = 0; index < 8; index++)
            {
                // Anomaly change point detection.
                PrintPrediction(5, engine.Predict(new TimeSeriesData(5)));
            }

            // 5       0       5.00    0.50    0.00       <-- Time Series 1.
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00
            // 5       0       5.00    0.50    0.00

            // Change point
            PrintPrediction(7, engine.Predict(new TimeSeriesData(7)));

            // 7       1       7.00    0.00    10298.67   <-- alert is on, predicted changepoint (and model is checkpointed).

            // Checkpoint the model.
            var modelPath = "temp.zip";

            engine.CheckPoint(ml, modelPath);

            // Reference to current time series engine because in the next step "engine" will point to the
            // checkpointed model being loaded from disk.
            var timeseries1 = engine;

            // Load the model.
            using (var file = File.OpenRead(modelPath))
                model = ml.Model.Load(file, out DataViewSchema schema);

            // Create a time series prediction engine from the checkpointed model.
            engine = model.CreateTimeSeriesPredictionFunction <TimeSeriesData, ChangePointPrediction>(ml);
            for (int index = 0; index < 8; index++)
            {
                // Anomaly change point detection.
                PrintPrediction(7, engine.Predict(new TimeSeriesData(7)));
            }

            // 7       0       7.00    0.13    33950.16   <-- Time Series 2 : Model loaded back from disk and prediction is made.
            // 7       0       7.00    0.26    60866.34
            // 7       0       7.00    0.38    78362.04
            // 7       0       7.00    0.50    0.01
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00

            // Prediction from the original time series engine should match the prediction from
            // check pointed model.
            engine = timeseries1;
            for (int index = 0; index < 8; index++)
            {
                // Anomaly change point detection.
                PrintPrediction(7, engine.Predict(new TimeSeriesData(7)));
            }

            // 7       0       7.00    0.13    33950.16   <-- Time Series 1 and prediction is made.
            // 7       0       7.00    0.26    60866.34
            // 7       0       7.00    0.38    78362.04
            // 7       0       7.00    0.50    0.01
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00
            // 7       0       7.00    0.50    0.00
        }