using System; using System.Collections.Generic; using System.IO; using Microsoft.ML; using Microsoft.ML.Transforms.TimeSeries; using Newtonsoft.Json; namespace AnomalyDetectionUsingSrCnn { class Program { static void Main(string[] args) { var mlContext = new MLContext(); //Load Json DataSet and Create an IEnumerable object List dataPoints = LoadJson(); IEnumerable enumerableDataSet = dataPoints; //Convert the IEnumerable Object into IDataView IDataView dataView = mlContext.Data.LoadFromEnumerable(enumerableDataSet); SrCnnAnomalyEstimator srCnnAnomalyEstimator = mlContext.Transforms.DetectAnomalyBySrCnn(nameof(DataPointPredictionModel.Prediction), nameof(DataPointModel.DataPoint), 10, 5, 5, 3, 5, 0.1); ITransformer srCnnAnomalyDetector = srCnnAnomalyEstimator.Fit(dataView); var transformedData = srCnnAnomalyDetector.Transform(dataView); var predictions = mlContext.Data.CreateEnumerable(transformedData, reuseRowObject: false); Console.WriteLine("Data\tAlert\tScore\tMag"); var k = 0; foreach (var p in predictions) { var results = ""; results += $"{dataPoints[k++].DataPoint}\t{p.Prediction[0]}\t{p.Prediction[1]:f2}\t{p.Prediction[2]:F2}"; if (p.Prediction[0] == 1) { results += "<------anomaly detected"; } Console.WriteLine(results); } Console.WriteLine(""); } static IDataView CreateEmptyDataView(MLContext mlContext) { // Create empty DataView. We just need the schema to call Fit() for the time series transforms IEnumerable enumerableData = new List(); return mlContext.Data.LoadFromEnumerable(enumerableData); } public static List LoadJson() { using (var r = new StreamReader("/root/.config/JetBrains/Rider2021.2/scratches/scratch.json")) { var json = r.ReadToEnd(); return JsonConvert.DeserializeObject>(json); } } } }