Example #1
0
        public void ToStringTest()
        {
            // Issue 51:
            SimpleLinearRegression regression = new SimpleLinearRegression();
            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 };

            regression.Regress(x, y);

            {
                string expected = "y(x) = 32x + -44";
                expected = expected.Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
                string actual = regression.ToString();
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = "y(x) = 32x + -44";
                string actual   = regression.ToString(null, System.Globalization.CultureInfo.GetCultureInfo("en-US"));
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = "y(x) = 32.0x + -44.0";
                string actual   = regression.ToString("N1", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
                Assert.AreEqual(expected, actual);
            }

            {
                string expected = "y(x) = 32,00x + -44,00";
                string actual   = regression.ToString("N2", System.Globalization.CultureInfo.GetCultureInfo("pt-BR"));
                Assert.AreEqual(expected, actual);
            }
        }
        public void LogarithmRegressionRegressTest()
        {
            // This is the same data from the example available at
            // http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm

            // Declare your inputs and output data
            double[] inputs  = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };

            // Transform inputs to logarithms
            double[] logx = Matrix.Log(inputs);

            // Compute a simple linear regression
            var lr = new SimpleLinearRegression();

            // Compute with the log-transformed data
            double error = lr.Regress(logx, outputs);

            // Get an expression representing the learned regression model
            // We just have to remember that 'x' will actually mean 'log(x)'
            string result = lr.ToString("N4", CultureInfo.InvariantCulture);

            // Result will be "y(x) = 6.1082x + 6.0993"

            Assert.AreEqual(2.8760006026675797, error);
            Assert.AreEqual(6.1081800414945704, lr.Slope);
            Assert.AreEqual(6.0993411396126653, lr.Intercept);
            Assert.AreEqual("y(x) = 6.1082x + 6.0993", result);
        }
Example #3
0
        public RegressionResult PerformRegression(double[] trainX, double[] trainY, double[] testX, double[] testY)
        {
            OrdinaryLeastSquares   ols        = new OrdinaryLeastSquares();
            SimpleLinearRegression regression = ols.Learn(trainX, trainY);

            return(new RegressionResult
            {
                FormulaUsed = regression.ToString(),
                PredictionOnTestSet = testX.Select(regression.Transform).ToArray(),
                PredictionOnTrainingSet = trainX.Select(regression.Transform).ToArray(),
                Regression = regression
            });
        }
Example #4
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            //将Input放在X轴,OutPut放在Y轴
            var GraphPane = zedGraph.GraphPane;

            GraphPane.CurveList.Clear();
            GraphPane.XAxis.Title.Text = cmbInputField.Text;
            GraphPane.YAxis.Title.Text = cmbOutputField.Text;
            //获得Input,Output列表
            double[] inliersX = new double[mongoCol.Count()];
            double[] inliersY = new double[mongoCol.Count()];
            int      Cnt      = 0;

            foreach (var item in mongoCol.FindAllAs <BsonDocument>())
            {
                inliersX[Cnt] = item[cmbInputField.Text].AsInt32;
                inliersY[Cnt] = item[cmbOutputField.Text].AsInt32;
                Cnt++;
            }
            var myCurve = GraphPane.AddCurve("Point", new PointPairList(inliersX, inliersY), Color.Blue, SymbolType.Default);

            myCurve.Line.IsVisible = false;
            myCurve.Symbol.Fill    = new Fill(Color.Blue);
            //线性回归
            // Create a new simple linear regression
            SimpleLinearRegression regression = new SimpleLinearRegression();

            // Compute the linear regression
            regression.Regress(inliersX, inliersY);

            double[] InputX  = new double[2];
            double[] OutputY = new double[2];

            InputX[0] = 0;
            InputX[1] = inliersX.Max();

            OutputY[0]             = regression.Compute(0);
            OutputY[1]             = regression.Compute(inliersX.Max());
            myCurve                = GraphPane.AddCurve("Regression:" + regression.ToString(), new PointPairList(InputX, OutputY), Color.Blue, SymbolType.Default);
            myCurve.Line.IsVisible = true;
            myCurve.Line.Color     = Color.Red;

            //更新坐标轴和图表
            zedGraph.AxisChange();
            zedGraph.Invalidate();
        }
        public void logarithm_learn()
        {
            #region doc_learn
            // This is the same data from the example available at
            // http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm

            // Declare your inputs and output data
            double[] inputs  = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };

            // Transform inputs to logarithms
            double[] logx = Matrix.Log(inputs);

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

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

            // Compute predicted values for inputs
            double[] predicted = lr.Transform(logx);

            // Get an expression representing the learned regression model
            // We just have to remember that 'x' will actually mean 'log(x)'
            string result = lr.ToString("N4", CultureInfo.InvariantCulture);

            // Result will be "y(x) = 6.1082x + 6.0993"

            // The mean squared error between the expected and the predicted is
            double error = new SquareLoss(outputs).Loss(predicted); // 0.261454
            #endregion

            Assert.AreEqual(0.26145460024250794, error, 1e-8);
            Assert.AreEqual(6.1081800414945704, lr.Slope, 1e-8);
            Assert.AreEqual(6.0993411396126653, lr.Intercept, 1e-8);
            Assert.AreEqual("y(x) = 6.1082x + 6.0993", result);
        }