示例#1
0
        private void btnRegress_Click(object sender, EventArgs e)
        {
            if (pls == null || dgvRegressionInput.DataSource == null)
            {
                MessageBox.Show("Please compute the analysis first.");
                return;
            }

            int components = (int)numComponentsRegression.Value;

            regression = pls.CreateRegression(components);

            DataTable table       = dgvRegressionInput.DataSource as DataTable;
            DataTable inputTable  = table.DefaultView.ToTable(false, inputColumnNames);
            DataTable outputTable = table.DefaultView.ToTable(false, outputColumnNames);


            double[][] sourceInput  = Matrix.ToArray(inputTable as DataTable);
            double[][] sourceOutput = Matrix.ToArray(outputTable as DataTable);


            double[,] result = Matrix.ToMatrix(regression.Compute(sourceInput));

            double[] rSquared = regression.CoefficientOfDetermination(sourceInput, sourceOutput, cbAdjusted.Checked);

            dgvRegressionOutput.DataSource = new ArrayDataView(result, outputColumnNames);
            dgvRSquared.DataSource         = new ArrayDataView(rSquared, outputColumnNames);
        }
        public void SimplsRegressionTest()
        {
            double[,] inputs;
            double[,] outputs;

            var pls = CreateWineExample(out inputs, out outputs);

            MultivariateLinearRegression regression = pls.CreateRegression();

            // test regression intercepts
            double[] intercepts = regression.Intercepts;
            double[] expectedI  = { 60.717, -8.509, -4.362 };

            Assert.IsTrue(intercepts.IsEqual(expectedI, 0.01));


            // test regression coefficients
            double[,] coefficients = regression.Coefficients;
            double[,] expectedC    =
            {
                { -1.6981, -0.0566, 0.07075 },
                {  1.2735,  0.2924, 0.57193 },
                { -4.0000,  1.0000, 0.50000 },
                {  1.1792,  0.1226, 0.15919 }
            };

            Assert.IsTrue(coefficients.IsEqual(expectedC, 0.01));


            // Test computation
            double[][] aY = regression.Compute(inputs.ToArray());

            for (int i = 0; i < outputs.GetLength(0); i++)
            {
                for (int j = 0; j < outputs.GetLength(1); j++)
                {
                    double actualOutput   = aY[i][j];
                    double expectedOutput = outputs[i, j];

                    double delta = System.Math.Abs(actualOutput - expectedOutput);
                    double tol   = 0.21 * expectedOutput;

                    Assert.IsTrue(delta <= tol);
                }
            }
        }
示例#3
0
        public override ConfusionMatrix Execute()
        {
            //The Least Squares algorithm
            //It uses a PartialLeastSquaresAnalysis library object using a non-linear iterative partial least squares algorithm
            //and runs on the mean-centered and standardized data

            //Create an analysis
            var pls = new PartialLeastSquaresAnalysis(trainingSet,
                                                      trainingOutput,
                                                      AnalysisMethod.Standardize,
                                                      PartialLeastSquaresAlgorithm.NIPALS);

            pls.Compute();

            //After computing the analysis
            //create a linear model to predict new variables
            MultivariateLinearRegression regression = pls.CreateRegression();

            //This will hold the result of the classifications
            var predictedLifted = new int[testSet.GetLength(0)][];

            for (int i = 0; i < predictedLifted.Length; ++i)
            {
                predictedLifted[i] = regression
                                     .Compute(testSet.GetRow(i))      //Retrieve the row vector of the test set
                                     .Select(x => Convert.ToInt32(x)) // Convert the result to int
                                     .ToArray();
            }

            //Unlift the prediction vector
            var predicted = predictedLifted
                            .SelectMany(x => x)
                            .ToArray();

            //Create a new confusion matrix with the calculated parameters
            ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, expected, POSITIVE, NEGATIVE);

            return(cmatrix);
        }
        private static PartialLeastSquaresAnalysis CreateWineExample(out double[,] inputs, out double[,] outputs)
        {
            // References: http://www.utdallas.edu/~herve/Abdi-PLSR2007-pretty.pdf

            // Following the small example by Hervé Abdi (Hervé Abdi, Partial Least Square Regression),
            // we will create a simple example where the goal is to predict the subjective evaluation of
            // a set of 5 wines. The dependent variables that we want to predict for each wine are its
            // likeability, and how well it goes with meat, or dessert (as rated by a panel of experts).
            // The predictors are the price, the sugar, alcohol, and acidity content of each wine.


            // Here we will list the inputs, or characteristics we would like to use in order to infer
            // information from our wines. Each row denotes a different wine and lists its corresponding
            // observable characteristics. The inputs are usually denoted by X in the literature.

            inputs = new double[, ]
            {
                // Wine | Price | Sugar | Alcohol | Acidity
                { 7, 7, 13, 7 },
                { 4, 3, 14, 7 },
                { 10, 5, 12, 5 },
                { 16, 7, 11, 3 },
                { 13, 3, 10, 3 },
            };


            // Here we will list our dependent variables. Dependent variables are the outputs, or what we
            // would like to infer or predict from our available data, given a new observation. The outputs
            // are usually denotes as Y in the literature.

            outputs = new double[, ]
            {
                // Wine | Hedonic | Goes with meat | Goes with dessert
                { 14, 7, 8 },
                { 10, 7, 6 },
                { 8, 5, 5 },
                { 2, 4, 7 },
                { 6, 2, 4 },
            };


            // Next, we will create our Partial Least Squares Analysis passing the inputs (values for
            // predictor variables) and the associated outputs (values for dependent variables).

            // We will also be using the using the Covariance Matrix/Center method (data will only
            // be mean centered but not normalized) and the NIPALS algorithm.
            PartialLeastSquaresAnalysis pls = new PartialLeastSquaresAnalysis(inputs, outputs,
                                                                              AnalysisMethod.Center, PartialLeastSquaresAlgorithm.SIMPLS);

            // Compute the analysis with all factors. The number of factors
            // could also have been specified in a overload of this method.

            pls.Compute();

            // After computing the analysis, we can create a linear regression model in order
            // to predict new variables. To do that, we may call the CreateRegression() method.

            MultivariateLinearRegression regression = pls.CreateRegression();

            // After the regression has been created, we will be able to classify new instances.
            // For example, we will compute the outputs for the first input sample:

            double[] y = regression.Compute(new double[] { 7, 7, 13, 7 });

            // The y output will be very close to the corresponding output used as reference.
            // In this case, y is a vector of length 3 with values { 14.00, 7.00, 7.75 }.

            Assert.AreEqual(14.00, y[0], 1e-2);
            Assert.AreEqual(+7.00, y[1], 1e-2);
            Assert.AreEqual(+7.75, y[2], 1e-2);

            return(pls);
        }