private static void Graph(string[] args, double[] x, double[] y) { string chartFileName = string.Empty; var pl = new PLStream(); if (args.Length == 1 && args[0] == "svg") { chartFileName = @"KNN.svg"; pl.sdev("svg"); pl.sfnam("KNN.svg"); } else { chartFileName = @"KNN.png"; pl.sdev("pngcairo"); pl.sfnam("KNN.png"); } pl.spal0("cmap0_alternate.pal"); pl.init(); const int xMin = 0; const int xMax = 1; const int yMin = 0; const int yMax = 1; pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes); pl.schr(0, 0.75); pl.lab("X-axis", "Y-axis", "Title"); pl.col0(3); pl.sym(x, y, (char)210); pl.eop(); pl.gver(out var verText); var p = new Process(); string chartFileNamePath = @".\" + chartFileName; p.StartInfo = new ProcessStartInfo(chartFileNamePath) { UseShellExecute = true }; p.Start(); }
private static void DrawPlot(IList <DayInfo> days, IList <DayInfo> anomalies) { days = days.Where(x => x.Date >= new DateTime(2017, 9, 1) && x.Date <= new DateTime(2017, 9, 30)).ToList(); anomalies = anomalies.Where(x => x.Date >= new DateTime(2017, 9, 1) && x.Date <= new DateTime(2017, 9, 30)).ToList(); using (var plot = new PLStream()) { plot.sdev("pngcairo"); // png rendering plot.sfnam("data.png"); // output filename plot.spal0("cmap0_alternate.pal"); // alternate color palette plot.init(); plot.env( 1, // x-axis range days.Count, 0, // y-axis range 150, AxesScale.Independent, // scale x and y independently AxisBox.BoxTicksLabelsAxes); // draw box, ticks, and num ticks plot.lab( "Date", // x-axis label "Count", // y-axis label "Press releases September 2017"); // plot title plot.line( (from x in Enumerable.Range(1, days.Count) select(double) x).ToArray(), (from p in days select(double) p.Count).ToArray()); // plot the spikes plot.col0(2); // blue color plot.schr(3, 3); // scale characters plot.string2( (from s in anomalies select(double) days.ToList().FindIndex(x => x.Date == s.Date) + 1).ToArray(), (from s in anomalies select(double) s.Count + 15).ToArray(), "↓"); plot.eop(); } }
private static void PlotRegressionChart(MLContext mlContext, string testDataSetPath, int numberOfRecordsToRead, string[] args) { ITransformer trainedModel; using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { trainedModel = mlContext.Model.Load(stream, out var modelInputSchema); } // Create prediction engine related to the loaded trained model var predFunction = mlContext.Model.CreatePredictionEngine <TaxiTrip, TaxiTripFarePredictionWithContribution>(trainedModel); string chartFileName = ""; using (var pl = new PLStream()) { if (args.Length == 1 && args[0] == "svg") { pl.sdev("svg"); chartFileName = "TaxiRegressionDistribution.svg"; pl.sfnam(chartFileName); } else { pl.sdev("pngcairo"); chartFileName = "TaxiRegressionDistribution.png"; pl.sfnam(chartFileName); } // use white background with black foreground pl.spal0("cmap0_alternate.pal"); // Initialize plplot pl.init(); // set axis limits const int xMinLimit = 0; const int xMaxLimit = 35; //Rides larger than $35 are not shown in the chart const int yMinLimit = 0; const int yMaxLimit = 35; //Rides larger than $35 are not shown in the chart pl.env(xMinLimit, xMaxLimit, yMinLimit, yMaxLimit, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes); // Set scaling for mail title text 125% size of default pl.schr(0, 1.25); // The main title pl.lab("Actual", "Predicted", "Distribution of Taxi Fare Prediction"); // plot using different colors // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices pl.col0(1); int totalNumber = numberOfRecordsToRead; var testData = new TaxiTripCsvReader().GetDataFromCsv(testDataSetPath, totalNumber).ToList(); //This code is the symbol to paint char code = (char)9; // plot using other color //pl.col0(9); //Light Green //pl.col0(4); //Red pl.col0(2); //Blue double yTotal = 0; double xTotal = 0; double xyMultiTotal = 0; double xSquareTotal = 0; for (int i = 0; i < testData.Count; i++) { var x = new double[1]; var y = new double[1]; //Make Prediction var FarePrediction = predFunction.Predict(testData[i]); x[0] = testData[i].FareAmount; y[0] = FarePrediction.FareAmount; //Paint a dot pl.poin(x, y, code); xTotal += x[0]; yTotal += y[0]; double multi = x[0] * y[0]; xyMultiTotal += multi; double xSquare = x[0] * x[0]; xSquareTotal += xSquare; double ySquare = y[0] * y[0]; Console.WriteLine($"-------------------------------------------------"); Console.WriteLine($"Predicted : {FarePrediction.FareAmount}"); Console.WriteLine($"Actual: {testData[i].FareAmount}"); Console.WriteLine($"-------------------------------------------------"); } // Regression Line calculation explanation: // https://www.khanacademy.org/math/statistics-probability/describing-relationships-quantitative-data/more-on-regression/v/regression-line-example double minY = yTotal / totalNumber; double minX = xTotal / totalNumber; double minXY = xyMultiTotal / totalNumber; double minXsquare = xSquareTotal / totalNumber; double m = ((minX * minY) - minXY) / ((minX * minX) - minXsquare); double b = minY - (m * minX); //Generic function for Y for the regression line // y = (m * x) + b; double x1 = 1; //Function for Y1 in the line double y1 = (m * x1) + b; double x2 = 39; //Function for Y2 in the line double y2 = (m * x2) + b; var xArray = new double[2]; var yArray = new double[2]; xArray[0] = x1; yArray[0] = y1; xArray[1] = x2; yArray[1] = y2; pl.col0(4); pl.line(xArray, yArray); // end page (writes output to disk) pl.eop(); // output version of PLplot pl.gver(out var verText); Console.WriteLine("PLplot version " + verText); } // the pl object is disposed here // Open Chart File In Microsoft Photos App (Or default app, like browser for .svg) Console.WriteLine("Showing chart..."); var p = new Process(); string chartFileNamePath = @".\" + chartFileName; p.StartInfo = new ProcessStartInfo(chartFileNamePath) { UseShellExecute = true }; p.Start(); }
/// <summary> /// The main program entry point. /// </summary> /// <param name="args">The command line parameters.</param> static void Main() { // create the machine learning context var context = new MLContext(); // load the data file Console.WriteLine("Loading data..."); var dataView = context.Data.LoadFromTextFile <SalesRecord>(path: dataPath, hasHeader: true, separatorChar: ','); // get an array of data points var sales = context.Data.CreateEnumerable <SalesRecord>(dataView, reuseRowObject: false).ToArray(); // plot the data var pl = new PLStream(); pl.sdev("pngcairo"); // png rendering pl.sfnam("data.png"); // output filename pl.spal0("cmap0_alternate.pal"); // alternate color palette pl.init(); pl.env( 0, 36, // x-axis range 0, 800, // y-axis range AxesScale.Independent, // scale x and y independently AxisBox.BoxTicksLabelsAxes); // draw box, ticks, and num ticks pl.lab( "Date", // x-axis label "Sales", // y-axis label "Shampoo sales over time"); // plot title pl.line( (from x in Enumerable.Range(0, sales.Count()) select(double) x).ToArray(), (from p in sales select(double) p.Sales).ToArray() ); // build a training pipeline for detecting spikes var pipeline = context.Transforms.DetectIidSpike( outputColumnName: nameof(SalesPrediction.Prediction), inputColumnName: nameof(SalesRecord.Sales), confidence: 95, pvalueHistoryLength: sales.Count() / 4); // 25% of x-range // train the model Console.WriteLine("Detecting spikes..."); var model = pipeline.Fit(dataView); // predict spikes in the data var transformed = model.Transform(dataView); var predictions = context.Data.CreateEnumerable <SalesPrediction>(transformed, reuseRowObject: false).ToArray(); // find the spikes in the data var spikes = (from i in Enumerable.Range(0, predictions.Count()) where predictions[i].Prediction[0] == 1 select(Day: i, Sales: sales[i].Sales)); // plot the spikes pl.col0(2); // blue color pl.schr(3, 3); // scale characters pl.string2( (from s in spikes select(double) s.Day).ToArray(), (from s in spikes select(double) s.Sales + 40).ToArray(), "!"); // build a training pipeline for detecting change points var pipeline2 = context.Transforms.DetectIidChangePoint( outputColumnName: nameof(SalesPrediction.Prediction), inputColumnName: nameof(SalesRecord.Sales), confidence: 95, changeHistoryLength: sales.Count() / 4); // 25% of x-range // train the model Console.WriteLine("Detecting change points..."); var model2 = pipeline2.Fit(dataView); // get predictions transformed = model2.Transform(dataView); predictions = context.Data.CreateEnumerable <SalesPrediction>(transformed, reuseRowObject: false).ToArray(); // find the change points in the data var changes = (from i in Enumerable.Range(0, predictions.Count()) where predictions[i].Prediction[0] == 1 select(Day: i, Sales: sales[i].Sales)); // plot the change points as vertical red lines pl.col0(3); foreach (var c in changes) { pl.line(new double[] { c.Day, c.Day }, new double[] { 0, 800 }); } pl.eop(); Console.WriteLine("Saved output file: data.png"); }
private static void Main(string[] args) { // generate data for plotting const double sineFactor = 0.012585; const int exampleCount = 1000; var phaseOffset = 125; var x0 = new double[exampleCount]; var y0 = new double[exampleCount]; for (var j = 0; j < exampleCount; j++) { x0[j] = j; y0[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } var x1 = new double[exampleCount]; var y1 = new double[exampleCount]; phaseOffset = 250; for (var j = 0; j < exampleCount; j++) { x1[j] = j; y1[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } var x2 = new double[exampleCount]; var y2 = new double[exampleCount]; phaseOffset = 375; for (var j = 0; j < exampleCount; j++) { x2[j] = j; y2[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } var x3 = new double[exampleCount]; var y3 = new double[exampleCount]; phaseOffset = 500; for (var j = 0; j < exampleCount; j++) { x3[j] = j; y3[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } // create PLplot object var pl = new PLStream(); // use SVG backend and write to SineWaves.svg in current directory if (args.Length == 1 && args[0] == "svg") { pl.sdev("svg"); pl.sfnam("SineWaves.svg"); } else { pl.sdev("pngcairo"); pl.sfnam("SineWaves.png"); } // use white background with black foreground pl.spal0("cmap0_alternate.pal"); // Initialize plplot pl.init(); // set axis limits const int xMin = 0; const int xMax = 1000; const int yMin = -1; const int yMax = 1; pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes); // Set scaling for mail title text 125% size of default pl.schr(0, 1.25); // The main title pl.lab("X", "Y", "PLplot demo of four sine waves"); // plot using different colors // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices pl.col0(9); pl.line(x0, y0); pl.col0(1); pl.line(x1, y1); pl.col0(2); pl.line(x2, y2); pl.col0(4); pl.line(x3, y3); // end page (writes output to disk) pl.eop(); // output version pl.gver(out var verText); Console.WriteLine("PLplot version " + verText); }
public static void Main(string[] args) { Storage(); // generate data for plotting const double sineFactor = 0.012585; const int exampleCount = 1000; var phaseOffset = 125; var x0 = new double[exampleCount]; var y0 = new double[exampleCount]; for (var j = 0; j < exampleCount; j++) { x0[j] = j; y0[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } var x1 = new double[exampleCount]; var y1 = new double[exampleCount]; phaseOffset = 250; for (var j = 0; j < exampleCount; j++) { x1[j] = j; y1[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } var x2 = new double[exampleCount]; var y2 = new double[exampleCount]; phaseOffset = 375; for (var j = 0; j < exampleCount; j++) { x2[j] = j; y2[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } var x3 = new double[exampleCount]; var y3 = new double[exampleCount]; phaseOffset = 500; for (var j = 0; j < exampleCount; j++) { x3[j] = j; y3[j] = (double)(System.Math.Sin(sineFactor * (j + phaseOffset)) * 1); } // create PLplot object var pl = new PLStream(); // use SVG backend and write to SineWaves.svg in current directory if (args.Length == 1 && args[0] == "svg") { pl.sdev("svg"); pl.sfnam("SineWaves.svg"); } else { pl.sdev("pngcairo"); pl.sfnam("SineWaves.png"); } // use white background with black foreground pl.spal0("cmap0_alternate.pal"); // Initialize plplot pl.init(); // set axis limits const int xMin = 0; const int xMax = 1000; const int yMin = -1; const int yMax = 1; pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes); // Set scaling for mail title text 125% size of default pl.schr(0, 1.25); // The main title pl.lab("X", "Y", "PLplot demo of four sine waves"); // plot using different colors // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices pl.col0(9); pl.line(x0, y0); pl.col0(1); pl.line(x1, y1); pl.col0(2); pl.line(x2, y2); pl.col0(4); pl.line(x3, y3); // end page (writes output to disk) pl.eop(); // output version pl.gver(out var verText); Console.WriteLine("PLplot version " + verText); ////////histogram///////////////////// double[] y00 = { 5.0, 15.0, 12.0, 24.0, 28.0, 30.0, 20.0, 8.0, 12.0, 3.0 }; double[] pos = { 0.0, 0.25, 0.5, 0.75, 1.0 }; double[] red = { 0.0, 0.25, 0.5, 1.0, 1.0 }; double[] green = { 1.0, 0.5, 0.5, 0.5, 1.0 }; double[] blue = { 1.0, 1.0, 0.5, 0.25, 0.0 }; PLStream pls = new PLStream(); pls.sdev("pngcairo"); pls.sfnam("Histogram.png"); pls.spal0("cmap0_alternate.pal"); pls.init(); pls.adv(0); pls.vsta(); pls.wind(1980.0, 1990.0, 0.0, 35.0); pls.box("bc", 1.0, 0, "bcnv", 10.0, 0); pls.col0(2); pls.lab("Year", "Widget Sales (millions)", "#frPLplot Example 12"); for (int i = 0; i < 10; i++) { // pls.col0(i + 1); pls.col1(i / 9.0); pls.psty(0); double[] x = new double[4]; double[] y = new double[4]; x[0] = 1980.0 + i; y[0] = 0.0; x[1] = 1980.0 + i; y[1] = y00[i]; x[2] = 1980.0 + i + 1.0; y[2] = y00[i]; x[3] = 1980.0 + i + 1.0; y[3] = 0.0; pls.fill(x, y); pls.col0(1); pls.lsty(LineStyle.Continuous); pls.line(x, y); // sprintf(string, "%.0f", y0[i]); String text = ((int)(y00[i] + 0.5)).ToString(); pls.ptex((1980.0 + i + .5), (y00[i] + 1.0), 1.0, 0.0, .5, text); // sprintf(string, "%d", 1980 + i); String text1 = (1980 + i).ToString(); pls.mtex("b", 1.0, ((i + 1) * .1 - .05), 0.5, text1); } pls.eop(); Console.ReadLine(); }
/// <summary> /// The main program entry point. /// </summary> /// <param name="args">The command line parameters.</param> static void Main() { // create the machine learning context var context = new MLContext(); // load the data file Console.WriteLine("Loading data..."); var dataView = context.Data.LoadFromTextFile <MeterData>(path: dataPath, hasHeader: true, separatorChar: ','); // get an array of data points var values = context.Data.CreateEnumerable <MeterData>(dataView, reuseRowObject: false).ToArray(); // plot the data var pl = new PLStream(); pl.sdev("pngcairo"); // png rendering pl.sfnam("data.png"); // output filename pl.spal0("cmap0_alternate.pal"); // alternate color palette pl.init(); pl.env( 0, 90, // x-axis range 0, 5000, // y-axis range AxesScale.Independent, // scale x and y independently AxisBox.BoxTicksLabelsAxes); // draw box, ticks, and num ticks pl.lab( "Day", // x-axis label "Power consumption", // y-axis label "Power consumption over time"); // plot title pl.line( (from x in Enumerable.Range(0, values.Count()) select(double) x).ToArray(), (from p in values select(double) p.Consumption).ToArray() ); // build a training pipeline for detecting spikes var pipeline = context.Transforms.SsaSpikeEstimator( nameof(SpikePrediction.Prediction), nameof(MeterData.Consumption), confidence: 98, pvalueHistoryLength: 30, trainingWindowSize: 90, seasonalityWindowSize: 30); // train the model Console.WriteLine("Detecting spikes..."); var model = pipeline.Fit(dataView); // predict spikes in the data var transformed = model.Transform(dataView); var predictions = context.Data.CreateEnumerable <SpikePrediction>(transformed, reuseRowObject: false).ToArray(); // find the spikes in the data var spikes = (from i in Enumerable.Range(0, predictions.Count()) where predictions[i].Prediction[0] == 1 select(Day: i, Consumption: values[i].Consumption)); // plot the spikes pl.col0(2); // blue color pl.schr(3, 3); // scale characters pl.string2( (from s in spikes select(double) s.Day).ToArray(), (from s in spikes select(double) s.Consumption + 200).ToArray(), "↓"); pl.eop(); }
public static void PlotRegressionChart(MLContext mlContext, string testDataSetPath, string simulationPath) { ITransformer trainedModel; using (var stream = new FileStream(simulationPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { trainedModel = mlContext.Model.Load(stream, out var modelInputSchema); } // Create prediction engine related to the loaded trained model var predFunction = mlContext.Model.CreatePredictionEngine <ExecutionModel, ExecutionModelPrediction>(trainedModel); string chartFileName; using (var pl = new PLStream()) { pl.sdev("pngcairo"); chartFileName = "RegressionDistribution.png"; pl.sfnam(chartFileName); // use white background with black foreground pl.spal0("cmap0_alternate.pal"); // Initialize plplot pl.init(); //var totalNumber = numberOfRecordsToRead; var testData = new CsvReader().GetDataFromCsv(testDataSetPath).ToList(); var totalNumber = testData.Count; var(xMinLimit, xMaxLimit) = GetMinMax(testData); // set axis limits pl.env(xMinLimit, xMaxLimit, xMinLimit, xMaxLimit, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes); // Set scaling for mail title text 125% size of default pl.schr(0, 1.25); // The main title pl.lab("Measured", "Predicted", "Distribution of TotalTime Prediction"); // plot using different colors // see http://plplot.sourceforge.net/examples.php?demo=02 for palette indices pl.col0(1); //This code is the symbol to paint const char code = (char)9; // plot using other color //pl.col0(9); //Light Green //pl.col0(4); //Red pl.col0(2); //Blue double yTotal = 0; double xTotal = 0; double xyMultiTotal = 0; double xSquareTotal = 0; foreach (var t in testData) { var x = new double[1]; var y = new double[1]; //Make Prediction var prediction = predFunction.Predict(t); x[0] = t.TotalTime; y[0] = prediction.TotalTime; //Paint a dot pl.poin(x, y, code); xTotal += x[0]; yTotal += y[0]; var multi = x[0] * y[0]; xyMultiTotal += multi; var xSquare = x[0] * x[0]; xSquareTotal += xSquare; //Console.WriteLine($"-------------------------------------------------"); //Console.WriteLine($"Predicted : {prediction.TotalTime}"); //Console.WriteLine($"Actual: {testData[i].TotalTime}"); //Console.WriteLine($"-------------------------------------------------"); } var minY = yTotal / totalNumber; var minX = xTotal / totalNumber; var minXy = xyMultiTotal / totalNumber; var minXsquare = xSquareTotal / totalNumber; var m = ((minX * minY) - minXy) / ((minX * minX) - minXsquare); var b = minY - (m * minX); //Generic function for Y for the regression line // y = (m * x) + b; const int x1 = 1; var y1 = (m * x1) + b; var x2 = xMaxLimit; //Function for Y2 in the line var y2 = (m * x2) + b; var xArray = new double[2]; var yArray = new double[2]; xArray[0] = x1; yArray[0] = y1; xArray[1] = x2; yArray[1] = y2; pl.col0(4); pl.line(xArray, yArray); // end page (writes output to disk) pl.eop(); // output version of PLplot pl.gver(out var verText); //Console.WriteLine("PLplot version " + verText); } // the pl object is disposed here // Open Chart File Console.WriteLine("Showing chart..."); var p = new Process(); var chartFileNamePath = @".\" + chartFileName; p.StartInfo = new ProcessStartInfo(chartFileNamePath) { UseShellExecute = true }; p.Start(); }