コード例 #1
0
 private static void exportdata(List <double> states, string name, string unit, bool Exportimage = false, List <int> second = null)
 {
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     sb.AppendLine($"x");
     foreach (var item in states)
     {
         sb.AppendLine(item.ToString());
     }
     Directory.CreateDirectory(System.IO.Path.Combine("/D/", "acc"));
     Directory.CreateDirectory(System.IO.Path.Combine("/D/", "acc_export"));
     System.IO.File.WriteAllText(
         System.IO.Path.Combine("/D/", "acc", $"linacc.csv"),
         sb.ToString());
     if (Exportimage)
     {
         var test = states.Min(x => x);
         var pl   = new PLStream();
         pl.sdev("pngcairo");                // png rendering
         pl.sfnam($"{name}.png");            // output filename
         pl.spal0("cmap0_alternate.pal");    // alternate color palette
         pl.init();
         pl.env(
             0, states.Count,                        // x-axis range
             states.Min(x => x), states.Max(x => x), // y-axis range
             AxesScale.Independent,                  // scale x and y independently
             AxisBox.BoxTicksLabelsAxes);            // draw box, ticks, and num ticks
         pl.lab(
             "Sample",                               // x-axis label
             unit,                                   // y-axis label
             name);                                  // plot title
         pl.line(
             (from x in Enumerable.Range(0, states.Count()) select(double) x).ToArray(),
             (from p in states select(double) p).ToArray()
             );
         if (second != null)
         {
             pl.col0(4);
             var Roots = GetListFromIndices(second, states);
             pl.poin(
                 (from x in second select(double) x).ToArray(),
                 (from p in Roots select(double) p).ToArray(),
                 '!'
                 );
         }
         string csv = String.Join(",", states.Select(x => x.ToString()).ToArray());
         pl.eop();
     }
 }
コード例 #2
0
        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();
        }
コード例 #3
0
        public static void PlotRegressionChart(PlotChartGeneratorModel generationModel)
        {
            using (var pl = new PLStream())
            {
                pl.sdev("pngcairo");
                pl.sfnam(generationModel.ImageName);

                // use white background with black foreground
                pl.spal0("cmap0_alternate.pal");

                // Initialize plplot
                pl.init();

                // set axis limits
                pl.env(generationModel.MinLimitX, generationModel.MaxLimitX, generationModel.MinLimitY, generationModel.MaxLimitY,
                       AxesScale.Independent, AxisBox.CustomXYBoxTicksLabels);

                // Set scaling for mail title text 125% size of default
                //pl.schr(0, 1.25);

                // The main title
                pl.lab(generationModel.LabelX, generationModel.LabelY, generationModel.Title);

                // 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
                char code = (char)9;

                double yTotal       = 0;
                double xTotal       = 0;
                double xyMultiTotal = 0;
                double xSquareTotal = 0;

                var totalNumber = 0;

                generationModel.PointsList.ForEach(pointsList =>
                {
                    double y0    = 0;
                    double x0    = 0;
                    totalNumber += pointsList.Points.Count();

                    // plot using other color
                    pl.col0(pointsList.Color);
                    pointsList.Points.ForEach(point =>
                    {
                        var x = new double[1];
                        var y = new double[1];

                        x[0] = point.X;
                        y[0] = point.Y;

                        if (pointsList.PaintDots)
                        {
                            // Paint a dot
                            pl.poin(x, y, code);
                        }
                        else
                        {
                            if (!generationModel.DrawRegressionLine)
                            {
                                // Draw lines between points
                                pl.join(x0, y0, point.X, point.Y);

                                x0 = point.X;
                                y0 = point.Y;
                            }
                        }


                        xTotal       += point.X;
                        yTotal       += point.Y;
                        xyMultiTotal += point.X * point.Y;
                        xSquareTotal += point.X * point.X;
                    });
                });

                // Regression Line calculation explanation:
                // https://www.khanacademy.org/math/statistics-probability/describing-relationships-quantitative-data/more-on-regression/v/regression-line-example

                if (generationModel.DrawRegressionLine)
                {
                    if (generationModel.RegressionPointsList != null)
                    {
                        var xArray = generationModel.RegressionPointsList.Points.Select(dp => dp.X).ToArray();
                        var yArray = generationModel.RegressionPointsList.Points.Select(dp => dp.Y).ToArray();

                        pl.col0(generationModel.RegressionPointsList.Color);
                        pl.line(xArray, yArray);
                    }
                    else
                    {
                        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);
                        double x1 = generationModel.MinLimitX;
                        //Function for Y1 in the line
                        double y1 = (m * x1) + b;

                        double x2 = generationModel.MaxLimitX;
                        //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);
                    }
                }

                if (generationModel.DashedPoint != null)
                {
                    pl.col0(CommonConstants.PPLplotColorGreen);
                    pl.width(2);
                    // Horizontal Line should go from (0,DashedPoint.Y) to (DashedPoint.X, DashedPoint.Y)
                    DrawDashedLined(pl, 0, generationModel.DashedPoint.Y, generationModel.DashedPoint.X, generationModel.DashedPoint.Y);
                    // Vertical Line should go from (DashedPoint.X,0) to (DashedPoint.X, DashedPoint.Y)
                    DrawDashedLined(pl, generationModel.DashedPoint.X, 0, generationModel.DashedPoint.X, generationModel.DashedPoint.Y);
                }

                // end page (writes output to disk)
                pl.eop();

                // output version of PLplot
                pl.gver(out var 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 = @".\" + generationModel.ImageName;

            p.StartInfo = new ProcessStartInfo(chartFileNamePath)
            {
                UseShellExecute = true
            };
            p.Start();
        }
コード例 #4
0
        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();
        }