コード例 #1
0
        public void learn_test_2()
        {
#if NETCORE
            var culture = CultureInfo.CreateSpecificCulture("en-US");
            CultureInfo.CurrentCulture = culture;
#endif

            #region doc_learn
            // Let's say we would like to learn 2nd degree polynomial that
            // can map the first column X into its second column Y. We have
            // 5 examples of those (x,y) pairs that we can use to learn this
            // function:

            double[,] data =
            {
                // X       Y
                { 12,  144 },    // example #1
                { 15,  225 },    // example #2
                { 20,  400 },    // example #3
                { 25,  625 },    // example #4
                { 35, 1225 },    // example #5
            };

            // Let's retrieve the input and output data:
            double[] inputs  = data.GetColumn(0); // X
            double[] outputs = data.GetColumn(1); // Y

            // We can create a learning algorithm
            var ls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            // Now, we can use the algorithm to learn a polynomial
            PolynomialRegression poly = ls.Learn(inputs, outputs);

            // The learned polynomial will be given by
            string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0"

            // Where its weights can be accessed using
            double[] weights   = poly.Weights;   // { 1.0000000000000024, -1.2407665029287351E-13 }
            double   intercept = poly.Intercept; // 1.5652369518855253E-12

            // Finally, we can use this polynomial
            // to predict values for the input data
            double[] pred = poly.Transform(inputs);

            // Where the mean-squared-error (MSE) should be
            double error = new SquareLoss(outputs).Loss(pred); // 0.0
            #endregion

            Assert.AreEqual(0, error, 1e-10);

            string   ex       = weights.ToCSharp();
            double[] expected = { 1, 0 };

            Assert.AreEqual("y(x) = 1.0x^2 + 0.0x^1 + 0.0", str);
            Assert.IsTrue(weights.IsEqual(expected, 1e-6));
            Assert.AreEqual(0, intercept, 1e-6);
        }
コード例 #2
0
        private PolynomialRegression PRLearning(double[] independentVariables, double[] dependentVariables)
        {
            var polyTeacher = new PolynomialLeastSquares()
            {
                Degree = 6
            };

            PolynomialRegression objRegressionLocal = polyTeacher.Learn(independentVariables, dependentVariables);

            double[] prediction = objRegressionLocal.Transform(independentVariables);

            predictionPL[indexPredictionPL] = prediction;

            //PredictedData[1] = prediction;

            errorPR += new SquareLoss(independentVariables).Loss(prediction);

            double[] coefOfFunction = new double[objRegressionLocal.Weights.Length + 1];
            coefOfFunction[0] = objRegressionLocal.Intercept;

            int index = objRegressionLocal.Weights.Length - 1;

            for (int i = 1; i <= objRegressionLocal.Weights.Length; i++)
            {
                coefOfFunction[i] = objRegressionLocal.Weights[index];
                index--;
            }

            double func(double x)
            {
                double y = 0;

                for (int i = 0; i <= polyTeacher.Degree; i++)
                {
                    y += coefOfFunction[i] * Math.Pow(x, i);
                }
                return(y);
            }

            double min = NormalizedInputData.GetColumn(indexPredictionPL).Min(),
                   max = NormalizedInputData.GetColumn(indexPredictionPL).Max();

            XYpairPL[indexPredictionPL]    = new double[2][];
            XYpairPL[indexPredictionPL][0] = new double[100];
            XYpairPL[indexPredictionPL][1] = new double[100];

            index = 0;
            for (double i = min; i <= max; i += 0.01)
            {
                XYpairPL[indexPredictionPL][0][index] = i;
                XYpairPL[indexPredictionPL][1][index] = func(i);
                index++;
            }

            indexPredictionPL++;

            return(objRegressionLocal);
        }
コード例 #3
0
        public void Learn(IList <XtoY> dsLearn)
        {
            double [] inputs  = dsLearn.Select(i => i.X).ToArray();
            double [] outputs = dsLearn.Select(i => i.Y).ToArray();
            var       pls     = new PolynomialLeastSquares()
            {
                Degree = _degree, IsRobust = _isRobust
            };

            _polynomialRegression = pls.Learn(inputs, outputs);
        }
コード例 #4
0
        public void PolyRegression()
        {
            if (dgvTestingSource.DataSource == null)
            {
                MessageBox.Show("Please Select a data set");
                return;
            }


            // Creates a matrix from the source data table
            double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix();

            double[,] data = table;

            // Let's retrieve the input and output data:
            double[] inputs  = data.GetColumn(0); // X
            double[] outputs = data.GetColumn(1); // Y

            // We can create a learning algorithm
            var ls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            // Now, we can use the algorithm to learn a polynomial
            PolynomialRegression poly = ls.Learn(inputs, outputs);

            // The learned polynomial will be given by
            string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0"

            // Where its weights can be accessed using
            double[] weights   = poly.Weights;   // { 1.0000000000000024, -1.2407665029287351E-13 }
            double   intercept = poly.Intercept; // 1.5652369518855253E-12

            // Finally, we can use this polynomial
            // to predict values for the input data
            double[] pred = poly.Transform(inputs);

            // Where the mean-squared-error (MSE) should be
            double error = new SquareLoss(outputs).Loss(pred); // 0.0

            double[][] tmpInputs = new double[inputs.Length][];
            for (int i = 0; i < inputs.Length; i++)
            {
                tmpInputs[i] = new double[1] {
                    inputs[i]
                };
            }
            CreateResultScatterplot(zedGraphControl1, tmpInputs, outputs, pred);
        }
コード例 #5
0
        public void Learn(IList <XtoY> dsLearn)
        {
            List <double> data = new List <double>(mylist.Count);

            for (int i = 0; i < mylist.Count; i++)
            {
                data.Add(mylist[i].Cases);
            }

            double[] inputs = dsLearn.Select(i => i.X).ToArray();
            var      pls    = new PolynomialLeastSquares()
            {
                Degree = _degree, IsRobust = _isRobust
            };

            _polynomialRegression = pls.Learn(inputs, data.ToArray());
        }
        public static PointPairList PolynomialRegresion(Dictionary <double, double> variablePair, int degree) // y = DOUBLE_Array[1]*x + DOUBLE_Array[0];
        {
            var polyTeacher = new PolynomialLeastSquares()
            {
                Degree = 3
            };

            PolynomialRegression objRegression = polyTeacher.Learn(variablePair.Keys.ToArray(), variablePair.Values.ToArray());

            double[] coefOfFunction = new double[objRegression.Weights.Length + 1];
            coefOfFunction[0] = objRegression.Intercept;

            int index = objRegression.Weights.Length - 1;

            for (int i = 1; i <= objRegression.Weights.Length; i++)
            {
                coefOfFunction[i] = objRegression.Weights[index];
                index--;
            }

            double func(double x)
            {
                double y = 0;

                for (int i = 0; i <= degree; i++)
                {
                    y += coefOfFunction[i] * Math.Pow(x, i);
                }
                return(y);
            }

            double[] independentValueArray = new double[variablePair.Count],
            dependentValueArray = new double[variablePair.Count];
            index = 0;

            foreach (var pair in variablePair)
            {
                independentValueArray[index] = pair.Key;
                dependentValueArray[index]   = func(pair.Key);
                index++;
            }

            return(new PointPairList(independentValueArray, dependentValueArray));
        }
コード例 #7
0
        public static void test3()
        {
            var poly2 = CreateFunc(1, 1);

            Random rnd = new Random();


            var pos = Enumerable.Range(0, 20).Select(x => new double [] { x, poly2(x) + rnd.NextDouble() }).ToArray();

            double[] inputs  = pos.Select(x => x[0]).ToArray();
            double[] outputs = pos.Select(x => x[1]).ToArray();

            var ls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            PolynomialRegression poly = ls.Learn(inputs, outputs);

            double a = poly.Weights[0];            // a = 0
            double b = poly.Weights[1];            // b = 0
            double c = poly.Intercept;             // c = 1

            double[] predicted = poly.Transform(inputs);

            double error = new SquareLoss(outputs).Loss(predicted);



            var ols = new OrdinaryLeastSquares();

            SimpleLinearRegression mul = ols.Learn(inputs, outputs);
            double a1 = mul.Slope;             // a = 0
            double b1 = mul.Intercept;         // b = 0

            double[] simplepredict = mul.Transform(inputs);

            double erroe2 = new SquaredHingeLoss(outputs).Loss(simplepredict);

            Console.WriteLine("Done");
        }
コード例 #8
0
        private PolynomialRegression GenerateRegressionFitting(SortedDictionary <double, double> values, char result)
        {
            // Extract inputs and outputs
            double[] inputs  = values.Keys.ToArray();
            double[] outputs = values.Values.ToArray();

            // We can create a learning algorithm
            PolynomialLeastSquares ls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            // Now, we can use the algorithm to learn a polynomial
            PolynomialRegression poly = ls.Learn(inputs, outputs);

            // The learned polynomial will be given by
#pragma warning disable IDE0059               // Unnecessary assignment of a value
            string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0"

            // Where its weights can be accessed using
            double[] weights   = poly.Weights;   // { 1.0000000000000024, -1.2407665029287351E-13 }
            double   intercept = poly.Intercept; // 1.5652369518855253E-12
#pragma warning restore IDE0059                  // Unnecessary assignment of a value

            // Finally, we can use this polynomial
            // to predict values for the input data
            double[] prediction = poly.Transform(inputs);

            double r2 = new RSquaredLoss(outputs.Length, outputs).Loss(prediction); // should be > 0.85 (close to 1 is ok)
            //LastGamesMetric:   0.77 0.81 0.08
            //GoalsScoredMetric: 0.75 0.85 0.02
            if (r2 == 1.0)
            {
                r2 = 0.0;
            }

            r2Values_.Add(result, r2);

            return(poly);
        }
コード例 #9
0
        public void learn_test()
        {
            double[] inputs  = { 15.2, 229.7, 3500 };
            double[] outputs = { 0.51, 105.66, 1800 };

            var ls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            PolynomialRegression target = ls.Learn(inputs, outputs);

            double[] expected = { 8.003175717e-6, 4.882498125e-1, -6.913246203 };
            double[] actual;

            actual = target.Weights;

            Assert.AreEqual(2, actual.Length);
            Assert.AreEqual(expected[0], actual[0], 1e-3);
            Assert.AreEqual(expected[1], actual[1], 1e-3);
            Assert.AreEqual(expected[2], target.Intercept, 1e-3);
        }
コード例 #10
0
        public void learn_ToStringTest()
        {
            var x = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var y = new double[] { 1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321 };

            var pls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            PolynomialRegression poly = pls.Learn(x, y);

            {
                string expected = "y(x) = 3x^2 + 1.99999999999998x^1 + 1.00000000000005";
                expected = expected.Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
                string actual = poly.ToString();
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = "y(x) = 3x^2 + 1.99999999999998x^1 + 1.00000000000005";
                string actual   = poly.ToString(null, System.Globalization.CultureInfo.GetCultureInfo("en-US"));
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = "y(x) = 3.0x^2 + 2.0x^1 + 1.0";
                string actual   = poly.ToString("N1", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = "y(x) = 3,00x^2 + 2,00x^1 + 1,00";
                string actual   = poly.ToString("N2", System.Globalization.CultureInfo.GetCultureInfo("pt-BR"));
                Assert.AreEqual(expected, actual);
            }
        }
コード例 #11
0
        private void button1_Click(object sender, EventArgs e)
        {
            // Declare some sample test data.
            double[] inputs  = { 80, 60, 10, 20, 30 };
            double[] outputs = { 20, 40, 30, 50, 60 };

            //// Use Ordinary Least Squares to learn the regression
            //OrdinaryLeastSquares ols = new OrdinaryLeastSquares();

            //// Use OLS to learn the simple linear regression
            //SimpleLinearRegression regression = ols.Learn(inputs, outputs);

            //// Compute the output for a given input:
            ////double y = regression.Transform(85); // The answer will be 28.088

            //// We can also extract the slope and the intercept term
            //// for the line. Those will be -0.26 and 50.5, respectively.
            //double s = regression.Slope;     // -0.264706
            //double c = regression.Intercept; // 50.588235

            //double[] x= new double[20];
            //double[] y = new double[20];
            //for(int i = 0; i < 20; i++)
            //{
            //    x[i] = 5 + (i * 5);
            //    y[i] = s*x[i] + c;
            //}

            // We can create a learning algorithm
            var ls = new PolynomialLeastSquares()
            {
                Degree = 2
            };

            // Now, we can use the algorithm to learn a polynomial
            PolynomialRegression poly = ls.Learn(inputs, outputs);

            // The learned polynomial will be given by
            string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0"

            Console.WriteLine(str);
            // Where its weights can be accessed using
            double[] weights   = poly.Weights;   // { 1.0000000000000024, -1.2407665029287351E-13 }
            double   intercept = poly.Intercept; // 1.5652369518855253E-12

            Console.WriteLine("{0},{1}", weights[0], intercept);
            // Finally, we can use this polynomial
            // to predict values for the input data
            double[] pred  = poly.Transform(inputs);
            double   error = new SquareLoss(outputs).Loss(pred);

            Console.WriteLine(error);

            double[] x = new double[20];
            double[] y = new double[20];
            for (int i = 0; i < 20; i++)
            {
                x[i] = 5 + (i * 5);
                y[i] = weights[0] * x[i] * x[i] + weights[1] * x[i] + intercept;
            }
            GraphPane myPane = this.zedGraphControl1.GraphPane;

            myPane.CurveList.Clear();

            // Set the titles
            myPane.Title.IsVisible            = false;
            myPane.Chart.Border.IsVisible     = false;
            myPane.XAxis.Title.Text           = "X";
            myPane.YAxis.Title.Text           = "Y";
            myPane.XAxis.IsAxisSegmentVisible = true;
            myPane.YAxis.IsAxisSegmentVisible = true;
            myPane.XAxis.MinorGrid.IsVisible  = false;
            myPane.YAxis.MinorGrid.IsVisible  = false;
            myPane.XAxis.MinorTic.IsOpposite  = false;
            myPane.XAxis.MajorTic.IsOpposite  = false;
            myPane.YAxis.MinorTic.IsOpposite  = false;
            myPane.YAxis.MajorTic.IsOpposite  = false;
            myPane.XAxis.Scale.MinGrace       = 0;
            myPane.XAxis.Scale.MaxGrace       = 0;
            myPane.XAxis.Scale.Max            = 90;
            //myPane.XAxis.Scale.Min = -10;
            myPane.YAxis.Scale.MinGrace = 0;
            myPane.YAxis.Scale.MaxGrace = 0;
            //myPane.YAxis.Scale.Min = -10;
            myPane.YAxis.Scale.Max = 70;

            PointPairList list1 = new PointPairList(inputs, outputs);
            PointPairList list2 = new PointPairList(x, y);
            LineItem      myCurve;

            // Add the curves
            myCurve = myPane.AddCurve("points", list1, Color.Blue, SymbolType.Circle);
            myCurve.Line.IsVisible = false;
            myCurve.Symbol.Fill    = new Fill(Color.Blue);

            myCurve = myPane.AddCurve("Simple", list2, Color.Red, SymbolType.Circle);
            myCurve.Line.IsAntiAlias = true;
            myCurve.Line.IsVisible   = true;
            myCurve.Symbol.IsVisible = false;

            this.zedGraphControl1.AxisChange();
            this.zedGraphControl1.Invalidate();
        }
コード例 #12
0
        public void weight_test_square()
        {
            PolynomialRegression reference;
            double referenceR2;

            {
                double[][] data =
                {
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, // 5 times weight 1
                    new[] { 1.0, 12.5, 3.6 },
                    new[] { 1.0, 43.2, 7.6 },
                    new[] { 1.0, 10.2, 1.1 },
                };

                double[] x = data.GetColumn(1);
                double[] y = data.GetColumn(2);

                var ols = new PolynomialLeastSquares()
                {
                    Degree   = 2,
                    IsRobust = true
                };

                reference = ols.Learn(x, y);

                Assert.AreEqual(1, reference.NumberOfInputs);
                Assert.AreEqual(1, reference.NumberOfOutputs);

                referenceR2 = reference.CoefficientOfDetermination(x, y);
            }

            PolynomialRegression target;
            double targetR2;

            {
                double[][] data =
                {
                    new[] { 5.0, 10.7, 2.4 }, // 1 times weight 5
                    new[] { 1.0, 12.5, 3.6 },
                    new[] { 1.0, 43.2, 7.6 },
                    new[] { 1.0, 10.2, 1.1 },
                };

                double[] weights = data.GetColumn(0);
                double[] x       = data.GetColumn(1);
                double[] y       = data.GetColumn(2);

                Assert.AreEqual(1, reference.NumberOfInputs);
                Assert.AreEqual(1, reference.NumberOfOutputs);

                var ols = new PolynomialLeastSquares()
                {
                    Degree   = 2,
                    IsRobust = true
                };

                target   = ols.Learn(x, y, weights);
                targetR2 = target.CoefficientOfDetermination(x, y, weights);
            }

            Assert.IsTrue(reference.Weights.IsEqual(target.Weights, 1e-5));
            Assert.AreEqual(reference.Intercept, target.Intercept, 1e-8);
            Assert.AreEqual(-0.023044161067521208, target.Weights[0], 1e-6);
            Assert.AreEqual(-10.192933942631839, target.Intercept, 1e-6);

            Assert.AreEqual(referenceR2, targetR2, 1e-8);
            Assert.AreEqual(0.97660035938038947, targetR2);
        }
コード例 #13
0
        public void weight_test_linear()
        {
            PolynomialRegression reference;
            double referenceR2;

            {
                double[][] data =
                {
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, //
                    new[] { 1.0, 10.7, 2.4 }, // 5 times weight 1
                    new[] { 1.0, 12.5, 3.6 },
                    new[] { 1.0, 43.2, 7.6 },
                    new[] { 1.0, 10.2, 1.1 },
                };

                double[] x = data.GetColumn(1);
                double[] y = data.GetColumn(2);

                var ols = new PolynomialLeastSquares();
                reference = ols.Learn(x, y);

                Assert.AreEqual(1, reference.NumberOfInputs);
                Assert.AreEqual(1, reference.NumberOfOutputs);

                referenceR2 = reference.CoefficientOfDetermination(x, y);
            }

            PolynomialRegression target;
            double targetR2;

            {
                double[][] data =
                {
                    new[] { 5.0, 10.7, 2.4 }, // 1 times weight 5
                    new[] { 1.0, 12.5, 3.6 },
                    new[] { 1.0, 43.2, 7.6 },
                    new[] { 1.0, 10.2, 1.1 },
                };

                double[] weights = data.GetColumn(0);
                double[] x       = data.GetColumn(1);
                double[] y       = data.GetColumn(2);

                Assert.AreEqual(1, reference.NumberOfInputs);
                Assert.AreEqual(1, reference.NumberOfOutputs);

                var ols = new PolynomialLeastSquares();
                target   = ols.Learn(x, y, weights);
                targetR2 = target.CoefficientOfDetermination(x, y, weights);
            }

            Assert.IsTrue(reference.Weights.IsEqual(target.Weights));
            Assert.AreEqual(reference.Intercept, target.Intercept, 1e-8);
            Assert.AreEqual(0.16387475666214069, target.Weights[0], 1e-6);
            Assert.AreEqual(0.59166925681755056, target.Intercept, 1e-6);

            Assert.AreEqual(referenceR2, targetR2, 1e-8);
            Assert.AreEqual(0.91476129548901486, targetR2);
        }