コード例 #1
2
        public void zero_inliers_test()
        {
            // Fix the random number generator
            Accord.Math.Random.Generator.Seed = 0;

            double[,] data = // This is the same data used in the RANSAC sample app
            {
                {  1.0,  0.79 }, {    3,  2.18 }, {    5,  5.99 }, {  7.0,  7.65 },
                {  9.0,  9.55 }, {   11, 11.89 }, {   13, 13.73 }, { 15.0, 14.77 },
                { 17.0, 18.00 }, {  1.2,  1.45 }, {  1.5,  1.18 }, {  1.8,  1.92 },
                {  2.1,  1.47 }, {  2.4,  2.41 }, {  2.7,  2.35 }, {  3.0,  3.41 },
                {  3.3,  3.78 }, {  3.6,  3.21 }, {  3.9,  4.76 }, {  4.2,  5.03 },
                {  4.5,  4.19 }, {  4.8,  3.81 }, {  5.1,  6.07 }, {  5.4,  5.74 },
                {  5.7,  6.39 }, {    6,  6.11 }, {  6.3,  6.86 }, {  6.6,  6.35 },
                {  6.9,   7.9 }, {  7.2,  8.04 }, {  7.5,  8.48 }, {  7.8,  8.07 },
                {  8.1,  8.22 }, {  8.4,  8.41 }, {  8.7,   9.4 }, {    9,   8.8 },
                {  9.3,  8.44 }, {  9.6,  9.32 }, {  9.9,  9.18 }, { 10.2,  9.86 },
                { 10.5, 10.16 }, { 10.8, 10.28 }, { 11.1, 11.07 }, { 11.4, 11.66 },
                { 11.7, 11.13 }, {   12, 11.55 }, { 12.3, 12.62 }, { 12.6, 12.27 },
                { 12.9, 12.33 }, { 13.2, 12.37 }, { 13.5, 12.75 }, { 13.8, 14.44 },
                { 14.1, 14.71 }, { 14.4, 13.72 }, { 14.7, 14.54 }, {   15, 14.67 },
                { 15.3, 16.04 }, { 15.6, 15.21 }, {    1,   3.9 }, {    2,  11.5 },
                {  3.0,  13.0 }, {    4,   0.9 }, {    5,   5.5 }, {    6,  16.2 },
                {  7.0,   0.8 }, {    8,   9.4 }, {    9,   9.5 }, {   10,  17.5 },
                { 11.0,   6.3 }, {   12,  12.6 }, {   13,   1.5 }, {   14,   1.5 },
                {  2.0,    10 }, {    3,     9 }, {   15,     2 }, { 15.5,   1.2 },
            };


            // First, fit simple linear regression directly for comparison reasons.
            double[] x = data.GetColumn(0); // Extract the independent variable
            double[] y = data.GetColumn(1); // Extract the dependent variable

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

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

            // Estimate a line passing through the (x, y) points
            double sumOfSquaredErrors = regression.Regress(x, y);

            // Now, compute the values predicted by the
            // regression for the original input points
            double[] commonOutput = regression.Compute(x);

            // Now, fit simple linear regression using RANSAC
            int    maxTrials      = 1000;
            int    minSamples     = 20;
            double probability    = 0.950;
            double errorThreshold = 1000;

            int count = 0;

            // Create a RANSAC algorithm to fit a simple linear regression
            var ransac = new RANSAC <SimpleLinearRegression>(minSamples)
            {
                Probability    = probability,
                Threshold      = errorThreshold,
                MaxEvaluations = maxTrials,

                // Define a fitting function
                Fitting = delegate(int[] sample)
                {
                    // Retrieve the training data
                    double[] inputs  = x.Submatrix(sample);
                    double[] outputs = y.Submatrix(sample);

                    // Build a Simple Linear Regression model
                    var r = new SimpleLinearRegression();
                    r.Regress(inputs, outputs);
                    return(r);
                },

                // Define a check for degenerate samples
                Degenerate = delegate(int[] sample)
                {
                    // In this case, we will not be performing such checks.
                    return(false);
                },

                // Define a inlier detector function
                Distances = delegate(SimpleLinearRegression r, double threshold)
                {
                    count++;

                    List <int> inliers = new List <int>();

                    // Generate 0 inliers twice, then proceed as normal
                    if (count > 2)
                    {
                        for (int i = 0; i < x.Length; i++)
                        {
                            // Compute error for each point
                            double error = r.Compute(x[i]) - y[i];

                            // If the squared error is below the given threshold,
                            //  the point is considered to be an inlier.
                            if (error * error < threshold)
                            {
                                inliers.Add(i);
                            }
                        }
                    }

                    return(inliers.ToArray());
                }
            };


            // Now that the RANSAC hyperparameters have been specified, we can
            // compute another regression model using the RANSAC algorithm:

            int[] inlierIndices;
            SimpleLinearRegression robustRegression = ransac.Compute(data.Rows(), out inlierIndices);


            // Compute the output of the model fitted by RANSAC
            double[] ransacOutput = robustRegression.Compute(x);

            Assert.AreEqual(ransac.TrialsNeeded, 0);
            Assert.AreEqual(ransac.TrialsPerformed, 3);

            string a = inlierIndices.ToCSharp();
            string b = ransacOutput.ToCSharp();

            int[]    expectedInliers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75 };
            double[] expectedOutput  = new double[] { 4.62124895918799, 5.37525473445784, 6.12926050972769, 6.88326628499754, 7.63727206026739, 8.39127783553724, 9.14528361080709, 9.89928938607694, 10.6532951613468, 4.69664953671498, 4.80975040300545, 4.92285126929593, 5.03595213558641, 5.14905300187689, 5.26215386816736, 5.37525473445784, 5.48835560074832, 5.6014564670388, 5.71455733332927, 5.82765819961975, 5.94075906591023, 6.05385993220071, 6.16696079849118, 6.28006166478166, 6.39316253107214, 6.50626339736262, 6.61936426365309, 6.73246512994357, 6.84556599623405, 6.95866686252453, 7.071767728815, 7.18486859510548, 7.29796946139596, 7.41107032768644, 7.52417119397691, 7.63727206026739, 7.75037292655787, 7.86347379284835, 7.97657465913882, 8.0896755254293, 8.20277639171978, 8.31587725801026, 8.42897812430073, 8.54207899059121, 8.65517985688169, 8.76828072317216, 8.88138158946264, 8.99448245575312, 9.1075833220436, 9.22068418833408, 9.33378505462455, 9.44688592091503, 9.55998678720551, 9.67308765349599, 9.78618851978646, 9.89928938607694, 10.0123902523674, 10.1254911186579, 4.62124895918799, 4.99825184682292, 5.37525473445784, 5.75225762209277, 6.12926050972769, 6.50626339736262, 6.88326628499754, 7.26026917263247, 7.63727206026739, 8.01427494790232, 8.39127783553724, 8.76828072317216, 9.14528361080709, 9.52228649844202, 4.99825184682292, 5.37525473445784, 9.89928938607694, 10.0877908298944 };

            Assert.IsTrue(inlierIndices.IsEqual(expectedInliers));
            Assert.IsTrue(ransacOutput.IsEqual(expectedOutput, 1e-10));
        }
コード例 #2
0
        public void RegressTest()
        {
            // Let's say we have some univariate, continuous sets of input data,
            // and a corresponding univariate, continuous set of output data, such
            // as a set of points in R². A simple linear regression is able to fit
            // a line relating the input variables to the output variables in which
            // the minimum-squared-error of the line and the actual output points
            // is minimum.

            // Declare some sample test data.
            double[] inputs  = { 80, 60, 10, 20, 30 };
            double[] outputs = { 20, 40, 30, 50, 60 };

            // Create a new simple linear regression
            SimpleLinearRegression regression = new SimpleLinearRegression();

            // Compute the linear regression
            regression.Regress(inputs, outputs);

            // Compute the output for a given input. The
            double y = regression.Compute(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;
            double c = regression.Intercept;

            // Expected slope and intercept
            double eSlope     = -0.264706;
            double eIntercept = 50.588235;

            Assert.AreEqual(28.0882352941192, y);
            Assert.AreEqual(eSlope, s, 0.0001);
            Assert.AreEqual(eIntercept, c, 0.0001);
        }
コード例 #3
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();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Tormasan/ConsoleMachine_0
        static void Main(string[] args)
        {
            DataTable tableAttHp = new ExcelReader("HsAttHp.xlsx").GetWorksheet("Sheet1");

            double[][] tableAttHpMatrix = tableAttHp.ToArray <double>();

            DataTable tableCost = new ExcelReader("HsCost.xlsx").GetWorksheet("Sheet1");

            double[] tableCostMatrix = tableCost.Columns[0].ToArray <double>();

            //double[,] scores = Accord.Statistics.Tools.ZScores(tableAttHpMatrix);

            //double[,] centered = Accord.Statistics.Tools.Center(tableAttHpMatrix);

            //double[,] standard = Accord.Statistics.Tools.Standardize(tableAttHpMatrix);

            //foreach (double i  in scores ) { Console.WriteLine(i); }
            //Console.ReadKey();
            //foreach (double i in centered) { Console.WriteLine(i); }
            //Console.ReadKey();
            //foreach (double i in standard) { Console.WriteLine(i); }

            // Plot the data
            //ScatterplotBox.Show("Hs", tableAttHpMatrix, tableCostMatrix).Hold();

            var target = new MultipleLinearRegression(2, true);

            double error = target.Regress(tableAttHpMatrix, tableCostMatrix);

            double a = target.Coefficients[0]; // a = 0
            double b = target.Coefficients[1]; // b = 0
            double c = target.Coefficients[2]; // c = 1


            Console.WriteLine(a + " " + b + " " + c);
            Console.ReadKey();

            double[] inputs  = { 2005, 2006, 2007, 2008, 2009, 2010, 2011 };
            double[] outputs = { 12, 19, 29, 37, 45, 23, 33 };

            // Create a new simple linear regression
            SimpleLinearRegression regression = new SimpleLinearRegression();

            // Compute the linear regression
            regression.Regress(inputs, outputs);

            // Compute the output for a given input. The
            double y = regression.Compute(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;
            double cut = regression.Intercept;

            Console.WriteLine(s + "x+" + cut);

            Console.ReadKey();
        }
コード例 #5
0
        public static object TestRegression(double[] x, double[] y)
        {
            SimpleLinearRegression slr = new SimpleLinearRegression();

            double err = slr.Regress(x, y);

            double[] values = slr.Compute(x);

            object[,] ret = new object[values.Length + 3, 2];
            ret[0, 0]     = "R^2";
            ret[0, 1]     = slr.CoefficientOfDetermination(x, y);
            ret[1, 0]     = "Slope";
            ret[1, 1]     = slr.Slope;
            ret[2, 0]     = "Error";
            ret[2, 1]     = err;
            for (int i = 0; i < values.Length; ++i)
            {
                ret[i + 3, 0] = x[i];
                ret[i + 3, 1] = values[i];
            }

            return(ret);
        }
コード例 #6
0
        private void btnCompute_Click(object sender, EventArgs e)
        {
            DataTable dataTable = dgvAnalysisSource.DataSource as DataTable;

            if (dataTable == null)
            {
                return;
            }

            // Gather the available data
            double[][] data = dataTable.ToArray();


            // First, fit simple linear regression directly for comparison reasons.
            double[] x = data.GetColumn(0); // Extract the independent variable
            double[] y = data.GetColumn(1); // Extract the dependent variable

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

            // Estimate a line passing through the (x, y) points
            double sumOfSquaredErrors = regression.Regress(x, y);

            // Now, compute the values predicted by the
            // regression for the original input points
            double[] commonOutput = regression.Compute(x);


            // Now, fit simple linear regression using RANSAC
            int    maxTrials      = (int)numMaxTrials.Value;
            int    minSamples     = (int)numSamples.Value;
            double probability    = (double)numProbability.Value;
            double errorThreshold = (double)numThreshold.Value;

            // Create a RANSAC algorithm to fit a simple linear regression
            var ransac = new RANSAC <SimpleLinearRegression>(minSamples)
            {
                Probability    = probability,
                Threshold      = errorThreshold,
                MaxEvaluations = maxTrials,

                // Define a fitting function
                Fitting = delegate(int[] sample)
                {
                    // Retrieve the training data
                    double[] inputs  = x.Submatrix(sample);
                    double[] outputs = y.Submatrix(sample);

                    // Build a Simple Linear Regression model
                    var r = new SimpleLinearRegression();
                    r.Regress(inputs, outputs);
                    return(r);
                },

                // Define a check for degenerate samples
                Degenerate = delegate(int[] sample)
                {
                    // In this case, we will not be performing such checks.
                    return(false);
                },

                // Define a inlier detector function
                Distances = delegate(SimpleLinearRegression r, double threshold)
                {
                    List <int> inliers = new List <int>();
                    for (int i = 0; i < x.Length; i++)
                    {
                        // Compute error for each point
                        double error = r.Compute(x[i]) - y[i];

                        // If the squared error is below the given threshold,
                        //  the point is considered to be an inlier.
                        if (error * error < threshold)
                        {
                            inliers.Add(i);
                        }
                    }

                    return(inliers.ToArray());
                }
            };


            // Now that the RANSAC hyperparameters have been specified, we can
            // compute another regression model using the RANSAC algorithm:

            int[] inlierIndices;
            SimpleLinearRegression robustRegression = ransac.Compute(data.Length, out inlierIndices);


            if (robustRegression == null)
            {
                lbStatus.Text = "RANSAC failed. Please try again after adjusting its parameters.";
                return; // the RANSAC algorithm did not find any inliers and no model was created
            }



            // Compute the output of the model fitted by RANSAC
            double[] ransacOutput = robustRegression.Compute(x);

            // Create scatter plot comparing the outputs from the standard
            //  linear regression and the RANSAC-fitted linear regression.
            CreateScatterplot(graphInput, x, y, commonOutput, ransacOutput,
                              x.Submatrix(inlierIndices), y.Submatrix(inlierIndices));

            lbStatus.Text = "Regression created! Please compare the RANSAC "
                            + "regression (blue) with the simple regression (in red).";
        }
コード例 #7
0
 public override double Predict(double predictor)
 {
     return(regression.Compute(predictor));
 }
コード例 #8
0
        private void btnSampleRunAnalysis_Click(object sender, EventArgs e)
        {
            DataTable dataTable = dgvAnalysisSource.DataSource as DataTable;

            if (dataTable == null)
            {
                return;
            }

            // Gather the available data
            double[][] data = dataTable.ToArray();


            // First, fit simple linear regression directly for comparison reasons.
            double[] x = data.GetColumn(0); // Extract the independent variable
            double[] y = data.GetColumn(1); // Extract the dependent variable

            // Create a simple linear regression
            SimpleLinearRegression slr = new SimpleLinearRegression();

            slr.Regress(x, y);

            // Compute the simple linear regression output
            double[] slrY = slr.Compute(x);


            // Now, fit simple linear regression using RANSAC
            int    maxTrials      = (int)numMaxTrials.Value;
            int    minSamples     = (int)numSamples.Value;
            double probability    = (double)numProbability.Value;
            double errorThreshold = (double)numThreshold.Value;

            // Create a RANSAC algorithm to fit a simple linear regression
            var ransac = new RANSAC <SimpleLinearRegression>(minSamples);

            ransac.Probability    = probability;
            ransac.Threshold      = errorThreshold;
            ransac.MaxEvaluations = maxTrials;

            // Set the RANSAC functions to evaluate and test the model

            ransac.Fitting = // Define a fitting function
                             delegate(int[] sample)
            {
                // Retrieve the training data
                double[] inputs  = x.Submatrix(sample);
                double[] outputs = y.Submatrix(sample);

                // Build a Simple Linear Regression model
                var r = new SimpleLinearRegression();
                r.Regress(inputs, outputs);
                return(r);
            };

            ransac.Degenerate = // Define a check for degenerate samples
                                delegate(int[] sample)
            {
                // In this case, we will not be performing such checks.
                return(false);
            };

            ransac.Distances = // Define a inlier detector function
                               delegate(SimpleLinearRegression r, double threshold)
            {
                List <int> inliers = new List <int>();
                for (int i = 0; i < x.Length; i++)
                {
                    // Compute error for each point
                    double error = r.Compute(x[i]) - y[i];

                    // If the squared error is below the given threshold,
                    //  the point is considered to be an inlier.
                    if (error * error < threshold)
                    {
                        inliers.Add(i);
                    }
                }
                return(inliers.ToArray());
            };


            // Finally, try to fit the regression model using RANSAC
            int[] idx; SimpleLinearRegression rlr = ransac.Compute(data.Length, out idx);



            // Check if RANSAC was able to build a consistent model
            if (rlr == null)
            {
                return; // RANSAC was unsucessful, just return.
            }
            else
            {
                // Compute the output of the model fitted by RANSAC
                double[] rlrY = rlr.Compute(x);

                // Create scatterplot comparing the outputs from the standard
                //  linear regression and the RANSAC-fitted linear regression.
                CreateScatterplot(graphInput, x, y, slrY, rlrY, x.Submatrix(idx), y.Submatrix(idx));
            }
        }