public void kernel_matrix_success()
        {
            var actual = new KernelPrincipalComponentAnalysis(new Linear(), PrincipalComponentMethod.KernelMatrix);
            var expected = new KernelPrincipalComponentAnalysis(data, new Linear(), AnalysisMethod.Standardize);

            Linear kernel = new Linear();
            double[][] K = kernel.ToJagged(data.ZScores().ToJagged());

            // Compute
            actual.Learn(K);
            expected.Compute();

            // Transform
            double[][] actualTransform = actual.Transform(K);
            double[][] expectedTransform1 = expected.Transform(data).ToJagged();
            double[][] expectedTransform2 = expected.Transform(data.ToJagged());


            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(actualTransform, expectedTransform1, 0.01));
            Assert.IsTrue(Matrix.IsEqual(actualTransform, expectedTransform2, 0.01));
        }
        public void learn_success()
        {
            #region doc_learn_1
            // Reproducing Lindsay Smith's "Tutorial on Principal Component Analysis"
            // using the framework's default method. The tutorial can be found online
            // at http://www.sccg.sk/~haladova/principal_components.pdf

            // Step 1. Get some data
            // ---------------------

            double[][] data = 
            {
                new double[] { 2.5,  2.4 },
                new double[] { 0.5,  0.7 },
                new double[] { 2.2,  2.9 },
                new double[] { 1.9,  2.2 },
                new double[] { 3.1,  3.0 },
                new double[] { 2.3,  2.7 },
                new double[] { 2.0,  1.6 },
                new double[] { 1.0,  1.1 },
                new double[] { 1.5,  1.6 },
                new double[] { 1.1,  0.9 }
            };


            // Step 2. Subtract the mean
            // -------------------------
            //   Note: The framework does this automatically. By default, the framework
            //   uses the "Center" method, which only subtracts the mean. However, it is
            //   also possible to remove the mean *and* divide by the standard deviation
            //   (thus performing the correlation method) by specifying "Standardize"
            //   instead of "Center" as the AnalysisMethod.

            var method = PrincipalComponentMethod.Center; // PrincipalComponentMethod.Standardize


            // Step 3. Compute the covariance matrix
            // -------------------------------------
            //   Note: Accord.NET does not need to compute the covariance
            //   matrix in order to compute PCA. The framework uses the SVD
            //   method which is more numerically stable, but may require
            //   more processing or memory. In order to replicate the tutorial
            //   using covariance matrices, please see the next unit test.

            // Create the analysis using the selected method
            var pca = new KernelPrincipalComponentAnalysis(new Linear(), method);

            // Compute it
            pca.Learn(data);


            // Step 4. Compute the eigenvectors and eigenvalues of the covariance matrix
            // -------------------------------------------------------------------------
            //   Note: Since Accord.NET uses the SVD method rather than the Eigendecomposition
            //   method, the Eigenvalues are computed from the singular values. However, it is
            //   not the Eigenvalues themselves which are important, but rather their proportion:

            // Those are the expected eigenvalues, in descending order:
            double[] eigenvalues = { 1.28402771, 0.0490833989 };

            // And this will be their proportion:
            double[] proportion = eigenvalues.Divide(eigenvalues.Sum());

            
            Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9));
            Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues.Divide(data.GetLength(0) - 1), rtol: 1e-5));

            // Step 5. Deriving the new data set
            // ---------------------------------

            double[][] actual = pca.Transform(data);

            // transformedData shown in pg. 18
            double[,] expected = new double[,]
            {
                {  0.827970186, -0.175115307 },
                { -1.77758033,   0.142857227 },
                {  0.992197494,  0.384374989 },
                {  0.274210416,  0.130417207 },
                {  1.67580142,  -0.209498461 },
                {  0.912949103,  0.175282444 },
                { -0.099109437, -0.349824698 },
                { -1.14457216,   0.046417258 },
                { -0.438046137,  0.017764629 },
                { -1.22382056,  -0.162675287 },
            }.Multiply(-1);

            // Everything is correct (up to 8 decimal places)
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));

            // Finally, we can project all the data
            double[][] output1 = pca.Transform(data);

            // Or just its first components by setting 
            // NumberOfOutputs to the desired components:
            pca.NumberOfOutputs = 1;

            // And then calling transform again:
            double[][] output2 = pca.Transform(data);

            // We can also limit to 80% of explained variance:
            pca.ExplainedVariance = 0.8;

            // And then call transform again:
            double[][] output3 = pca.Transform(data);
            #endregion

            actual = pca.Transform(data);

            // transformedData shown in pg. 18
            expected = new double[,]
            {
                {  0.827970186 },
                { -1.77758033, },
                {  0.992197494 },
                {  0.274210416 },
                {  1.67580142, },
                {  0.912949103 },
                { -0.099109437 },
                { -1.14457216, },
                { -0.438046137 },
                { -1.22382056, },
            }.Multiply(-1);

            // Everything is correct (up to 8 decimal places)
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));


            // Create the analysis using the selected method
            pca = new KernelPrincipalComponentAnalysis()
            {
                Kernel = new Linear(),
                Method = method,
                NumberOfOutputs = 1
            };

            // Compute it
            pca.Learn(data);

            actual = pca.Transform(data);

            // transformedData shown in pg. 18
            expected = new double[,]
            {
                {  0.827970186 },
                { -1.77758033, },
                {  0.992197494 },
                {  0.274210416 },
                {  1.67580142, },
                {  0.912949103 },
                { -0.099109437 },
                { -1.14457216, },
                { -0.438046137 },
                { -1.22382056, },
            }.Multiply(-1);

            // Everything is correct (up to 8 decimal places)
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));

        }
        public void transform_more_columns_than_samples_new_interface()
        {
            // Lindsay's tutorial data
            double[,] datat = data.Transpose();

            var target = new KernelPrincipalComponentAnalysis(new Linear());

            // Compute
            target.Learn(datat);

            // Transform
            double[,] actual = target.Transform(datat);

            // Assert the scores equals the transformation of the input
            double[,] result = target.Result;

            double[,] expected = new double[,]
            {
                {  0.50497524691810358 },
                { -0.504975246918104  }
            }.Multiply(-1);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.01));
            Assert.IsTrue(Matrix.IsEqual(result, actual, 0.01));
        }
        public void RevertTest2_new_method()
        {
            string path = @"Resources\examples.xls";

            // Create a new reader, opening a given path
            ExcelReader reader = new ExcelReader(path);

            // Afterwards, we can query the file for all
            // worksheets within the specified workbook:
            string[] sheets = reader.GetWorksheetList();

            // Finally, we can request an specific sheet:
            DataTable table = reader.GetWorksheet("Wikipedia");

            // Now, we have loaded the Excel file into a DataTable. We
            // can go further and transform it into a matrix to start
            // running other algorithms on it: 

            double[][] matrix = table.ToArray();

            IKernel kernel = new Gaussian(5);

            // Create analysis
            var target = new KernelPrincipalComponentAnalysis(kernel)
            { 
                Method = PrincipalComponentMethod.Center, 
                Center = true // Center in feature space
            };

            var regression  = target.Learn(matrix);

            double[][] forward = regression.Transform(matrix);

            double[][] reversion = target.Revert(forward);

            Assert.IsTrue(!reversion.HasNaN());
        }
        public void test_kernlab_new_method()
        {
            // Tested against R's kernlab

            // test <-   c(16, 117, 94, 132, 13, 73, 68, 129, 91, 50, 56, 12, 145, 105, 35, 53, 38, 51, 85, 116)
            int[] test = { 15, 116, 93, 131, 12, 72, 67, 128, 90, 49, 55, 11, 144, 104, 34, 52, 37, 50, 84, 115 };

            // data(iris)
            double[,] iris = create_iris();


            // kpc <- kpca(~.,data=iris[-test,-5],kernel="rbfdot",kpar=list(sigma=0.2),features=2)
            var data = iris.Remove(test, new[] { 4 });
            var kernel = new Gaussian() { Gamma = 1 };
            var kpc = new KernelPrincipalComponentAnalysis(kernel);

            kpc.NumberOfOutputs = 2;
            var transform = kpc.Learn(data);

            var rotated = kpc.Result;
            var pcv = kpc.ComponentMatrix;
            var eig = kpc.Eigenvalues;

            double[] expected_eig = { 28.542404060412132, 15.235596653653861 };
            double[,] expected_pcv = expected_r();

            Assert.IsTrue(Matrix.IsEqual(expected_eig, eig, 1e-10));
            Assert.IsTrue(Matrix.IsEqual(expected_pcv, pcv, 1e-10));

            double[,] irisSubset = iris_sub();

            var testing = iris.Submatrix(test, new[] { 0, 1, 2, 3 });

            Assert.IsTrue(Matrix.IsEqual(irisSubset, testing));

            double[,] expectedProjection = expected_p();

            double[,] proj = kpc.Transform(testing);
            Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6));

            double[][] proj2 = kpc.Transform(testing.ToJagged());
            Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6));

            double[][] proj3 = transform.Transform(testing.ToJagged());
            Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6));
        }
        public void learn_whiten_success()
        {
            double[,] data = 
            {
                { 2.5,  2.4 },
                { 0.5,  0.7 },
                { 2.2,  2.9 },
                { 1.9,  2.2 },
                { 3.1,  3.0 },
                { 2.3,  2.7 },
                { 2.0,  1.6 },
                { 1.0,  1.1 },
                { 1.5,  1.6 },
                { 1.1,  0.9 }
            };

            var method = PrincipalComponentMethod.Center; // PrincipalComponentMethod.Standardize
            var pca = new KernelPrincipalComponentAnalysis(new Linear(), method, whiten: true);

            pca.Learn(data);

            double[] eigenvalues = { 1.28402771, 0.0490833989 };
            double[] proportion = eigenvalues.Divide(eigenvalues.Sum());
            double[,] eigenvectors =
            {
                { 0.19940687993951403, -1.1061252858739095 },
                { 0.21626410214440508,  1.0199057073792104 }
            };

            // Everything is alright (up to the 9 decimal places shown in the tutorial)
            // Assert.IsTrue(eigenvectors.IsEqual(pca.ComponentMatrix, rtol: 1e-9));
            Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9));
            Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues.Divide(data.GetLength(0) - 1), rtol: 1e-5));

            double[,] actual = pca.Transform(data);

            double[][] expected = new double[][]
            {
                new double[] {  0.243560157209023,  -0.263472650637184  },
                new double[] { -0.522902576315494,   0.214938218565977  },
                new double[] {  0.291870144299372,   0.578317788814594  },
                new double[] {  0.0806632088164338,  0.19622137941132   },
                new double[] {  0.492962746459375,  -0.315204397734004  },
                new double[] {  0.268558011864442,   0.263724118751361  },
                new double[] { -0.0291545644762578, -0.526334573603598  },
                new double[] { -0.336693495487974,   0.0698378585807067 },
                new double[] { -0.128858004446015,   0.0267280693333571 },
                new double[] { -0.360005627922904,  -0.244755811482527  } 
            }.Multiply(-1);

            // Everything is correct (up to 8 decimal places)
            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));
        }
Exemplo n.º 7
0
        /// <summary>
        ///   Launched when the user clicks the "Run analysis" button.
        /// </summary>
        /// 
        private void btnCompute_Click(object sender, EventArgs e)
        {
            // Save any pending changes 
            dgvAnalysisSource.EndEdit();

            if (dgvAnalysisSource.DataSource == null)
            {
                MessageBox.Show("Please load some data using File > Open!");
                return;
            }

            // Create a matrix from the source data table
            double[][] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToArray(out columnNames);

            // Create and compute a new Simple Descriptive Analysis
            sda = new DescriptiveAnalysis()
            {
                ColumnNames = columnNames
            };

            sda.Learn(sourceMatrix);

            // Show the descriptive analysis on the screen
            dgvDistributionMeasures.DataSource = sda.Measures;


            // Create the kernel function
            IKernel kernel = createKernel();


            // Get the input values (the two first columns)
            double[][] inputs = sourceMatrix.GetColumns(0, 1);

            // Get only the associated labels (last column)
            int[] outputs = sourceMatrix.GetColumn(2).ToInt32();


            var method = (PrincipalComponentMethod)cbMethod.SelectedValue;

            // Creates the Kernel Principal Component Analysis of the given source
            kpca = new KernelPrincipalComponentAnalysis()
            {
                Kernel = kernel,
                Method = method
            };

            // Whether to center in space
            kpca.Center = cbCenter.Checked;


            var classifier = kpca.Learn(inputs); // Finally, compute the analysis!


            double[][] result = kpca.Transform(inputs);
            double[][] reduced;

            if (kpca.Components.Count >= 2)
            {
                // Perform the transformation of the data using two components 
                kpca.NumberOfOutputs = 2;
                reduced = kpca.Transform(inputs);
            }
            else
            {
                kpca.NumberOfOutputs = 1;
                reduced = kpca.Transform(inputs);
                reduced = reduced.InsertColumn(Vector.Zeros(reduced.GetLength(0)));
            }

            // Create a new plot with the original Z column
            double[][] points = reduced.InsertColumn(sourceMatrix.GetColumn(2));

            // Create output scatter plot
            outputScatterplot.DataSource = points;
            CreateScatterplot(graphMapFeature, points);

            // Create output table
            dgvProjectionResult.DataSource = new ArrayDataView(points, columnNames);
            dgvReversionSource.DataSource = new ArrayDataView(result);


            // Populates components overview with analysis data
            dgvFeatureVectors.DataSource = new ArrayDataView(kpca.ComponentVectors);
            dgvPrincipalComponents.DataSource = kpca.Components;
            cumulativeView.DataSource = kpca.Components;
            distributionView.DataSource = kpca.Components;

            numNeighbor.Maximum = result.Rows();
            numNeighbor.Value = System.Math.Min(10, numNeighbor.Maximum);

            lbStatus.Text = "Good! Feel free to browse the other tabs to see what has been found.";
        }