Example #1
0
        static void PlotData(MLContext context, IDataView data)
        {
            var transactions = context.Data.CreateEnumerable <CreditCardData>(data, reuseRowObject: false).ToArray();
            // plot median house value by longitude
            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, 5000,                     // x-axis range
                0, 2500,                     // y-axis range
                AxesScale.Independent,       // scale x and y independently
                AxisBox.BoxTicksLabelsAxes); // draw box, ticks, and num ticks
            pl.lab(
                "Time",                      // x-axis label
                "Amount",                    // y-axis label
                "Transactions by Time");     // plot title
            pl.sym(
                transactions.Select(txn => (double)txn.Time).ToArray(),
                transactions.Select(txn => (double)txn.Amount).ToArray(),
                (char)218
                );
            pl.eop();
        }
Example #2
0
        static void ProcessData(MLContext context, IDataView data)
        {
            var houses = context.Data.CreateEnumerable <HouseBlockData>(data, reuseRowObject: false).ToArray();
            // plot median house value by longitude
            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, 10,                          // x-axis range
                0, 600000,                      // y-axis range
                AxesScale.Independent,          // scale x and y independently
                AxisBox.BoxTicksLabelsAxes);    // draw box, ticks, and num ticks
            pl.lab(
                "Median Income",                // x-axis label
                "Median House Value",           // y-axis label
                "House value by longitude");    // plot title
            pl.sym(
                houses.Select(h => (double)h.MedianIncome).ToArray(),
                houses.Select(h => (double)h.MedianHouseValue).ToArray(),
                (char)218
                );
            pl.eop();
        }
Example #3
0
        private static void GenerateHistogram1(MLContext context, IDataView data)
        {
            // get an array of housing data
            var houses = context.Data.CreateEnumerable <HousingData>(data, reuseRowObject: false).ToArray();

            // plot median house value by longitude
            var pl = new PLStream();

            pl.sdev("pngcairo");                // png rendering
            pl.sfnam("data1.png");              // output filename
            pl.spal0("cmap0_alternate.pal");    // alternate color palette
            pl.init();
            pl.env(
                0, 10,                       // x-axis range
                0, 600000,                   // y-axis range
                AxesScale.Independent,       // scale x and y independently
                AxisBox.BoxTicksLabelsAxes); // draw box, ticks, and num ticks
            pl.lab(
                "Rooms Per Person",          // x-axis label
                "Count",                     // y-axis label
                "House value by longitude"); // plot title

            //var grouped = houses.Select(c => new { RoomsPerPerson = (double)c.TotalRooms / (double)c.Population, Data = c }).GroupBy(g => g.RoomsPerPerson)
            //    .Select(s => new { RoomsPerPerson = s.Key, Count = s.Sum().Count } );


            //pl.line(
            //    grouped.Select(h => (double)h.RoomsPerPerson).ToArray(),
            //    grouped.Select(h => (double)h.Count).ToArray()
            //);
            //pl.eop();
        }
Example #4
0
        private static void PlotToFile(string fileName, Action <PLStream> action)
        {
            using (var pl = new PLStream())
            {
                pl.sdev("pngcairo");
                pl.sfnam(fileName);
                pl.spal0("cmap0_alternate.pal");
                pl.init();

                action(pl);

                pl.eop();
            }
        }
Example #5
0
        private void Graph(string[] args,
                           double[] x1, double[] y1,
                           double[] x2, double[] y2,
                           double[] x3, double[] y3)
        {
            string chartFileName = string.Empty;
            var    pl            = new PLStream();

            if (args.Length == 1 && args[0] == "svg")
            {
                chartFileName = @"Logistic.svg";
                pl.sdev("svg");
                pl.sfnam("Logistic.svg");
            }
            else
            {
                chartFileName = @"Logistic.png";
                pl.sdev("pngcairo");
                pl.sfnam("Logistic.png");
            }
            pl.spal0("cmap0_alternate.pal");
            pl.init();

            const int xMin = -4;
            const int xMax = 4;
            const int yMin = -5;
            const int yMax = 20;

            pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabels);
            // Set scaling for mail title text 125% size of default
            pl.lab("X1", "X2", "Title");
            pl.col0(3);
            pl.sym(x1, y1, (char)228);
            pl.col0(9);
            pl.sym(x2, y2, (char)228);
            pl.col0(2);
            pl.line(x3, y3);
            pl.eop();
            pl.gver(out var verText);
            var    p = new Process();
            string chartFileNamePath = @".\" + chartFileName;

            p.StartInfo = new ProcessStartInfo(chartFileNamePath)
            {
                UseShellExecute = true
            };
            p.Start();
        }
Example #6
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();
     }
 }
Example #7
0
        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();
            }
        }
Example #9
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();
        }
Example #10
0
        /// <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");
        }
Example #11
0
        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 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();
        }
Example #13
0
        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();
        }
Example #14
0
        /// <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();
        }
Example #15
0
        /// <summary>
        /// The main program entry point.
        /// </summary>
        /// <param name="args">The command line arguments</param>
        static void Main(string[] args)
        {
            // create the machine learning context
            var context = new MLContext();

            // load the dataset
            Console.WriteLine("Loading data...");
            var data = context.Data.LoadFromTextFile <HouseBlockData>(
                path: dataPath,
                hasHeader: true,
                separatorChar: ',');

            // keep only records with a median house value < 500,000
            data = context.Data.FilterRowsByColumn(
                data,
                "MedianHouseValue",
                upperBound: 499_999
                );

            // get an array of housing data
            var houses = context.Data.CreateEnumerable <HouseBlockData>(data, reuseRowObject: false).ToArray();

            // plot median house value by longitude
            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, 10,                          // x-axis range
                0, 600000,                      // y-axis range
                AxesScale.Independent,          // scale x and y independently
                AxisBox.BoxTicksLabelsAxes);    // draw box, ticks, and num ticks
            pl.lab(
                "Median Income",                // x-axis label
                "Median House Value",           // y-axis label
                "House value by longitude");    // plot title
            pl.sym(
                houses.Select(h => (double)h.MedianIncome).ToArray(),
                houses.Select(h => (double)h.MedianHouseValue).ToArray(),
                (char)218
                );
            pl.eop();

            // build a data loading pipeline
            // step 1: divide the median house value by 1000
            var pipeline = context.Transforms.CustomMapping <HouseBlockData, ToMedianHouseValue>(
                (input, output) => { output.NormalizedMedianHouseValue = input.MedianHouseValue / 1000; },
                contractName: "MedianHouseValue"
                );

            // get a 10-record preview of the transformed data
            // var model = pipeline.Fit(data);
            // var transformedData = model.Transform(data);
            // var preview = transformedData.Preview(maxRows: 10);

            // show the preview
            // WritePreview(preview);

            // step 2: bin the longitude
            var pipeline2 = pipeline.Append(context.Transforms.NormalizeBinning(
                                                inputColumnName: "Longitude",
                                                outputColumnName: "BinnedLongitude",
                                                maximumBinCount: 10
                                                ))

                            // step 3: bin the latitude
                            .Append(context.Transforms.NormalizeBinning(
                                        inputColumnName: "Latitude",
                                        outputColumnName: "BinnedLatitude",
                                        maximumBinCount: 10
                                        ))

                            // step 4: one-hot encode the longitude
                            .Append(context.Transforms.Categorical.OneHotEncoding(
                                        inputColumnName: "BinnedLongitude",
                                        outputColumnName: "EncodedLongitude"
                                        ))

                            // step 5: one-hot encode the latitude
                            .Append(context.Transforms.Categorical.OneHotEncoding(
                                        inputColumnName: "BinnedLatitude",
                                        outputColumnName: "EncodedLatitude"
                                        ));

            // step 6: cross the two one-hot encoded columns
            var pipeline3 = pipeline2.Append(context.Transforms.CustomMapping <FromLocation, ToLocation>(
                                                 (input, output) =>
            {
                output.Location = new float[input.EncodedLongitude.Length * input.EncodedLatitude.Length];
                var index       = 0;
                for (var i = 0; i < input.EncodedLongitude.Length; i++)
                {
                    for (var j = 0; j < input.EncodedLatitude.Length; j++)
                    {
                        output.Location[index++] = input.EncodedLongitude[i] * input.EncodedLatitude[j];
                    }
                }
            },
                                                 contractName: "Location"
                                                 ))

                            // step 7: remove all the columns we don't need anymore
                            .Append(context.Transforms.DropColumns(
                                        "MedianHouseValue",
                                        "Longitude",
                                        "Latitude",
                                        "BinnedLongitude",
                                        "BinnedLatitude",
                                        "EncodedLongitude",
                                        "EncodedLatitude"
                                        ));

            // get a 10-record preview of the transformed data
            var model           = pipeline3.Fit(data);
            var transformedData = model.Transform(data);
            var preview         = transformedData.Preview(maxRows: 10);

            // show the location vector
            //WritePreview(preview);
            WritePreviewColumn(preview, "Location");
        }
Example #16
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();
        }