Beispiel #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();
        }
Beispiel #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();
        }
Beispiel #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();
        }
        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();
            }
        }
Beispiel #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();
        }
Beispiel #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();
     }
 }
Beispiel #7
0
    static void Main(string[] args)
    {
        // http://plplot.org/examples.php?demo=02
        using (var pl = new PLStream())
        {
            pl.sdev("png");
            pl.sfnam("imagerect%n.png");

            pl.sfam(1, 1, 10);
            // pl.gfam(out int p_fam, out int p_num, out int p_bmax);

            pl.init();

            demo1(pl);
            demo2(pl);
        }
    }
Beispiel #8
0
        void Draw()
        {
            int       width   = 400;
            int       height  = 400;
            const int RGBsize = 3;

            byte[] buffer = new byte[width * height * RGBsize];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 0xff;
            }

            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try
            {
                IntPtr pBuf = handle.AddrOfPinnedObject();

                using (var pl = new PLStream())
                {
                    pl.sdev("mem");
                    pl.smem(width, height, pBuf);

                    pl.init();

                    pl.env(0, 10.0, 0, 10.0, 0, 0);
                    pl.join(1.0, 2.0, 7.0, 8.0);
                }

                Bitmap bmp = new Bitmap(width, height, 3 * width, PixelFormat.Format24bppRgb, pBuf);
                this.pictureBox1.Width  = width;
                this.pictureBox1.Height = height;
                this.pictureBox1.Image  = bmp;

                // bmp.Save("test.bmp");
            }
            finally
            {
                handle.Free();
            }
        }
Beispiel #9
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();
            }
        }
Beispiel #11
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();
        }
Beispiel #12
0
        public string generateNNGraph(string matrixRef, int NumberInputsNodes, int NumberHiddenNodes, int NumberOutputNodes,
                                      int NumberOfEpochs, double LearningRate_eta, string title, int oneCol, string oneLab, int twoCol, string twoLab, string fileName)
        {
            var matrixComp = Value(matrixRef);

            if (matrixComp.GetType() == typeof(string))
            {
                return("Could not find variable " + matrixRef);
            }
            MatrixData _graphData = (MatrixData)matrixComp;

            double[] x  = _graphData.GetColumnCopy <double>(0);
            double[] y  = _graphData.GetColumnCopy <double>(1);
            double[] y1 = _graphData.GetColumnCopy <double>(2);

            double xMax = _graphData.Max(0);

            var plot = new PLStream();

            plot.width(1);
            plot.sdev("svg");
            plot.sfnam(fileName + ".svg");
            plot.scolbg(255, 255, 255);
            plot.init();

            plot.env(0, xMax, 0, 105, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes);

            Dictionary <int, string> cols = new Dictionary <int, string>();

            cols.Add(0, "black");
            cols.Add(1, "red");
            cols.Add(2, "yellow");
            cols.Add(3, "green");
            cols.Add(4, "aquamarine");
            cols.Add(5, "pink");
            cols.Add(6, "wheat");
            cols.Add(7, "grey");
            cols.Add(8, "brown");
            cols.Add(9, "blue");
            cols.Add(10, "BlueViolet");
            cols.Add(11, "cyan");
            cols.Add(12, "turquoise");
            cols.Add(13, "magenta");
            cols.Add(14, "salmon");
            cols.Add(15, "white");


            plot.col0(1);
            plot.lab("Epoch", "Accuracy %", title);
            plot.ptex(xMax - 10, 25, 1.0, 0, 1, "Input Nodes: " + NumberInputsNodes);
            plot.ptex(xMax - 10, 20, 1.0, 0, 1, "Hidden Nodes: " + NumberHiddenNodes);
            plot.ptex(xMax - 10, 15, 1.0, 0, 1, "Output Nodes: " + NumberOutputNodes);
            plot.ptex(xMax - 10, 10, 1.0, 0, 1, "Epochs: " + NumberOfEpochs);
            plot.ptex(xMax - 10, 5, 1.0, 0, 1, "Learning Rate: " + LearningRate_eta);
            plot.col0(1);

            plot.col0(oneCol);
            plot.line(x, y);
            plot.ptex(xMax - 10, 35, 1.0, 0, 1, cols[oneCol] + ": " + oneLab);
            plot.col0(oneCol);

            plot.col0(twoCol);
            plot.line(x, y1);
            plot.ptex(xMax - 10, 30, 1.0, 0, 1, cols[twoCol] + ": " + twoLab);
            plot.col0(twoCol);

            plot.eop();

            return(Directory.GetCurrentDirectory() + "\\" + fileName + ".svg");
        }
Beispiel #13
0
        public string GenerateGraph()
        {
            double[] x  = _graphData.GetColumnCopy <double>(0);
            double[] y  = _graphData.GetColumnCopy <double>(1);
            double[] y1 = _graphData.GetColumnCopy <double>(2);

            double xMax = _graphData.Max(0);
            //double yMax = _graphData.Max(1);
            //yMax = (_graphData.Max(2) > yMax)? _graphData.Max(2) : yMax;
            //double yMin = _graphData.Min(1);
            //yMin = (_graphData.Min(2) < yMin)? _graphData.Min(2): yMin;

            var plot = new PLStream();

            plot.width(1);
            plot.sdev("svg");
            plot.sfnam("Test.svg");
            plot.scolbg(255, 255, 255);
            plot.init();

            plot.env(0, xMax, 0, 105, AxesScale.Independent, AxisBox.BoxTicksLabelsAxes);

            //####### PLPlot colour guide:
            //0	black (default background)
            //1	red (default foreground)
            //2	yellow
            //3	green
            //4	aquamarine
            //5	pink
            //6	wheat
            //7	grey
            //8	brown
            //9	blue
            //10 BlueViolet
            //11 cyan
            //12 turquoise
            //13 magenta
            //14 salmon
            //15 white


            plot.col0(1);
            if (!(_testingData == null))
            {
                plot.lab("Epoch", "Accuracy %", "Test vs Train Accuracy");
            }
            else
            {
                plot.lab("Epoch", "Accuracy %", "Test vs Val Accuracy");
            }
            plot.ptex(xMax - 10, 25, 1.0, 0, 1, "Input Nodes: " + NumberInputsNodes);
            plot.ptex(xMax - 10, 20, 1.0, 0, 1, "Hidden Nodes: " + NumberHiddenNodes);
            plot.ptex(xMax - 10, 15, 1.0, 0, 1, "Output Nodes: " + NumberOutputNodes);
            plot.ptex(xMax - 10, 10, 1.0, 0, 1, "Epochs: " + NumberOfEpochs);
            plot.ptex(xMax - 10, 5, 1.0, 0, 1, "Learning Rate: " + LearningRate_eta);
            plot.col0(1);

            plot.col0(9);
            plot.line(x, y);
            plot.ptex(xMax - 10, 35, 1.0, 0, 1, "Blue: Train");
            plot.col0(9);

            plot.col0(3);
            plot.line(x, y1);
            if (!(_testingData == null))
            {
                plot.ptex(xMax - 10, 30, 1.0, 0, 1, "Green: Test");
            }
            else
            {
                plot.ptex(xMax - 10, 30, 1.0, 0, 1, "Green: Val");
            }
            plot.col0(3);

            plot.eop();

            return("Saved \"Test.svg\"");
        }
Beispiel #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 <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");
        }
Beispiel #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");
        }
Beispiel #16
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();
        }
Beispiel #17
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();
        }
Beispiel #18
0
        public static void CreateCovidChart(CovidParser.Country[] countries, CovidChartTypes type, int startDay = 0, int endDay = 2147483647)
        {
            if (startDay < 0)
            {
                Debug.LogWarning("Start day is lower than 0, fixing");
                startDay = 0;
            }

            int xMin = startDay;
            int xMax = countries[0].data.Count;
            int yMin = 0;

            CovidParser.Country.Data c = FindMax(countries, type, startDay, endDay);
            long yMax = 0;

            string title = "COVID-19 in ";

            for (int i = 0; i < countries.Length; i++)
            {
                title += countries[i].name + " ";
            }
            switch (type)
            {
            case CovidChartTypes.deaths:
                yMax   = c.deaths;
                title += " deaths";
                break;

            case CovidChartTypes.confirmed:
                yMax   = c.confirmed;
                title += " confirmed";
                break;

            case CovidChartTypes.recovered:
                yMax   = c.recovered;
                title += " recovered";
                break;

            case CovidChartTypes.all:
                yMax = c.confirmed;
                break;

            default:
                break;
            }



            List <double>[] values = null; //new List<double>();
            List <double>   keys   = new List <double>();

            for (int j = 0; j < countries.Length; j++)
            {
                for (int i = startDay; i < countries[j].data.Count && i < endDay; i++)
                {
                    if (j == 0)
                    {
                        keys.Add(i);
                    }
                    switch (type)
                    {
                    case CovidChartTypes.deaths:
                        if (values == null || values[j] == null)
                        {
                            values    = new List <double> [countries.Length];
                            values[j] = new List <double>();
                        }
                        values[j].Add(countries[j].data[i].deaths);

                        break;

                    case CovidChartTypes.confirmed:
                        if (values == null || values[j] == null)
                        {
                            values    = new List <double> [countries.Length];
                            values[j] = new List <double>();
                        }
                        values[j].Add(countries[j].data[i].confirmed);

                        break;

                    case CovidChartTypes.recovered:
                        if (values == null || values[j] == null)
                        {
                            values    = new List <double> [countries.Length];
                            values[j] = new List <double>();
                        }
                        values[j].Add(countries[j].data[i].recovered);

                        break;

                    case CovidChartTypes.all:
                        //Debug.LogError("All is not supported yet");
                        if (values == null)
                        {
                            values = new List <double> [countries.Length * 3];
                        }
                        if (values[j * 3] == null)
                        {
                            values[j * 3]     = new List <double>();
                            values[j * 3 + 1] = new List <double>();
                            values[j * 3 + 2] = new List <double>();
                        }
                        values[j * 3].Add(countries[j].data[i].confirmed);
                        values[j * 3 + 1].Add(countries[j].data[i].deaths);
                        values[j * 3 + 2].Add(countries[j].data[i].recovered);
                        break;

                    default:
                        break;
                    }
                }
            }
            var pl = new PLStream();

            pl.sdev("pngcairo");
            pl.sfnam("covid.png");

            pl.init();
            pl.col0(15);

            // Set to use 10000 instead of 1 * 10^5
            pl.syax(10, 10);

            if (endDay < xMax)
            {
                xMax = endDay - 1;
            }


            pl.env(xMin, xMax, yMin, yMax, AxesScale.Independent, AxisBox.BoxTicksLabels);

            //pl.setcontlabelformat(10, 10);
            pl.col0(15);
            pl.lab("Days", "Cases", title);



            Pattern[]     lg_patterns = new Pattern[values.Length];
            double[]      lg_scales   = new double[values.Length];
            string[]      lg_texts    = new string[values.Length];
            int[]         lg_lcolors  = new int[values.Length];
            double[]      lg_lwidths  = new double[values.Length];
            int[]         lg_scolors  = new int[values.Length];
            int[]         lg_snumbers = new int[values.Length];
            string[]      lg_symbols  = new string[values.Length];
            LineStyle[]   lg_lstyles  = new LineStyle[values.Length];
            LegendEntry[] lg_entries  = new LegendEntry[values.Length];

            double lg_spacing = 2.8 / (countries.Length + 0.5);
            double lg_tscale  = 1.4 / (countries.Length + 0.5);
            double lg_toffset = 0.5;



            switch (type)
            {
            case CovidChartTypes.deaths:
                pl.scmap0(new int[] { 255, 255 }, new int[] { 255, 0 }, new int[] { 0, 0 });
                break;

            case CovidChartTypes.confirmed:
                break;

            case CovidChartTypes.recovered:
                break;

            case CovidChartTypes.all:
                pl.scmap0(new int[] { 255, 255, 0, 204, 128, 0 }, new int[] { 255, 0, 255, 153, 0, 128 }, new int[] { 0, 0, 0, 0, 0, 0 });
                break;

            default:
                break;
            }

            for (int i = 0; i < values.Length; i++)
            {
                pl.col0(i);

                pl.line(keys.ToArray(), values[i].ToArray());


                lg_entries[i]  = LegendEntry.Line;
                lg_lstyles[i]  = LineStyle.Continuous;
                lg_lcolors[i]  = i;
                lg_scolors[i]  = i;
                lg_snumbers[i] = i;
                lg_lwidths[i]  = 2;
                lg_texts[i]    = countries[i / 3].name;
                if (i % 3 == 0)
                {
                    lg_texts[i] += " confirmed";
                }
                else if (i % 3 == 1)
                {
                    lg_texts[i] += " deaths";
                }
                else
                {
                    lg_texts[i] += " recovered";
                }

                lg_scales[i] = 1;
            }



            pl.legend(out double width, out double height, Legend.BoundingBox, Position.Left | Position.Top, 0, 0, 0.1, 0, 1, LineStyle.Continuous, 0, 0, lg_entries, lg_toffset, lg_tscale, lg_spacing, 1.0,
                      lg_lcolors, lg_texts, lg_lcolors, lg_patterns, lg_scales, lg_lwidths, lg_lcolors, lg_lstyles, lg_lwidths, lg_scolors, lg_scales, lg_snumbers, lg_symbols);

            pl.eop();

            pl.ResetOpts();

            IDisposable disp = (IDisposable)pl;

            disp.Dispose();

            //pl.gver(out var varText);
            //Debug.Log("Plplot version: " + varText);
        }
        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();
        }
Beispiel #20
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();
        }
Beispiel #21
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);
        }