public void new_method()
        {
            #region doc_learn
            // Create some sample input data instances.

            double[][] inputs =
            {
                // Class 0
                new double[] {  4, 1 },
                new double[] {  2, 4 },
                new double[] {  2, 3 },

                // Class 1
                new double[] {  5, 5 },
                new double[] {  5, 6 },

                // Class 2
                new double[] { 10, 8 }
            };

            int[] output =
            {
                0, 0, 0, // The first three are from class 0
                1, 1,    // The second two are from class 1
                2        // The last is from class 2
            };

            // We will create a MMDC object for the data
            var mmdc = new MinimumMeanDistanceClassifier();

            // Compute the analysis and create a classifier
            mmdc.Learn(inputs, output);

            // Now we can project the data into mean distance space:
            double[][] projection = mmdc.Scores(inputs);

            // Or perform classification using:
            int[] results = mmdc.Decide(inputs);
            #endregion

            Assert.IsTrue(results.IsEqual(output));

            string str = projection.ToCSharp();

            double[][] expected = new double[][]
            {
                new double[] { -4.55555555555556, -21.25, -85 },
                new double[] { -2.22222222222222, -11.25, -80 },
                new double[] { -0.555555555555555, -15.25, -89 },
                new double[] { -10.8888888888889, -0.25, -34 },
                new double[] { -16.5555555555556, -0.25, -29 },
                new double[] { -82.2222222222222, -31.25, 0 }
            };

            Assert.IsTrue(expected.IsEqual(projection, 1e-6));
        }
        public void ComputeTest()
        {
            double[][] inputs =
            {
                // Class 0
                new double[] {  4,  1 },
                new double[] {  2,  4 },
                new double[] {  2,  3 },
                new double[] {  3,  6 },
                new double[] {  4,  4 },

                // Class 1
                new double[] {  9, 10 },
                new double[] {  6,  8 },
                new double[] {  9,  5 },
                new double[] {  8,  7 },
                new double[] { 10,  8 }
            };

            int[] output =
            {
                0, 0, 0, 0, 0, // The first five are from class 0
                1, 1, 1, 1, 1  // The last five are from class 1
            };

            // Then, we will create a LDA for the given instances.
            var mmdc = new MinimumMeanDistanceClassifier(inputs, output);

            double[][] projection = mmdc.Scores(inputs);

            // Or perform classification using:
            int[] results = mmdc.Decide(inputs);


            Assert.IsTrue(results.IsEqual(output));

            //string str = projection.ToCSharp();

            double[][] expected = new double[][] {
                new double[] { -7.76, -62.92 },
                new double[] { -1.16, -53.92 },
                new double[] { -1.36, -62.12 },
                new double[] { -5.76, -31.72 },
                new double[] { -1.16, -32.32 },
                new double[] { -76.96, -6.12 },
                new double[] { -28.36, -5.92 },
                new double[] { -37.96, -7.12 },
                new double[] { -36.56, -0.52 },
                new double[] { -68.36, -2.72 }
            };

            Assert.IsTrue(expected.IsEqual(projection, 1e-6));
        }
Exemplo n.º 3
0
        private void btnClassify_Click(object sender, EventArgs e)
        {
            if (classifier == null)
            {
                MessageBox.Show("Please create a classifier first!");
                return;
            }

            foreach (DataGridViewRow row in dataGridView3.Rows)
            {
                double[] feature = (double[])row.Tag;

                int output = classifier.Decide(feature);

                row.Cells["colClassification"].Value = ((char)(output + 'A')).ToString();

                int expected = (int)row.Cells["colLabel2"].Value;
                row.DefaultCellStyle.BackColor = (expected == output) ? Color.LightGreen : Color.White;
            }
        }
Exemplo n.º 4
0
        public void learn_test()
        {
            #region doc_learn
            // Create some sample input data instances. This is the same
            // data used in the Gutierrez-Osuna's example available on:
            // http://research.cs.tamu.edu/prism/lectures/pr/pr_l10.pdf

            double[][] inputs =
            {
                // Class 0
                new double[] {  4,  1 },
                new double[] {  2,  4 },
                new double[] {  2,  3 },
                new double[] {  3,  6 },
                new double[] {  4,  4 },

                // Class 1
                new double[] {  9, 10 },
                new double[] {  6,  8 },
                new double[] {  9,  5 },
                new double[] {  8,  7 },
                new double[] { 10,  8 }
            };

            int[] output =
            {
                0, 0, 0, 0, 0, // The first five are from class 0
                1, 1, 1, 1, 1  // The last five are from class 1
            };


            // We'll create a KDA using a Linear kernel
            var kda = new KernelDiscriminantAnalysis()
            {
                Kernel = new Linear() // We can choose any kernel function
            };

            // Compute the analysis and create a classifier
            var classifier = kda.Learn(inputs, output);

            // Now we can project the data into KDA space:
            double[][] projection = kda.Transform(inputs);

            // Or perform classification using:
            int[] results = kda.Classify(inputs);

            // Note: The classifier generated by the KDA analysis is composed
            // of a two-step transformation. The first transformation projects
            // the input data into a new space using a kernel regression:
            MultivariateKernelRegression kernelRegression = classifier.First;
            // While the second is a classifier that attempts to map the outputs
            // of the kernel regression to each class according to their average:
            MinimumMeanDistanceClassifier meanClassifier = classifier.Second;

            // As such, calling kda.Classify is equivalent to calling:
            int[] results2 = classifier.Decide(inputs);

            // which in turn is equivalent to calling:
            int[] results3 = meanClassifier.Decide(kernelRegression.Transform(inputs));
            #endregion

            Assert.AreEqual(results, results2);
            Assert.AreEqual(results, results3);

            double[][] classifierProjection = kda.Classifier.First.Transform(inputs);
            Assert.IsTrue(projection.IsEqual(classifierProjection));

            double[][] expected = new double[][] {
                new double[] { 80.7607049998409, -5.30485371541545E-06, 6.61304584781419E-06, 4.52807990036774E-06, -3.44409628150189E-06, 3.69094504515388E-06, -1.33641000168438E-05, -0.000132874977040842, -0.000261921590627878, 1.22137997452386 },
                new double[] { 67.6629612351861, 6.80622743409742E-06, -8.48466262226566E-06, -5.80961187779394E-06, 4.4188405141643E-06, -4.73555212510135E-06, 1.71463925084936E-05, 0.000170481102685471, 0.000336050342774286, -1.5670535522193 },
                new double[] { 59.8679301679674, 4.10375477777336E-06, -5.11575246520124E-06, -3.50285421113483E-06, 2.66430090034575E-06, -2.85525936627451E-06, 1.03382660725515E-05, 0.00010279007663172, 0.000202618589039361, -0.944841112367518 },
                new double[] { 101.494441852779, 1.02093411395998E-05, -1.27269939227403E-05, -8.71441780958548E-06, 6.62826077091339E-06, -7.10332818965043E-06, 2.57195887591877E-05, 0.000255721654028207, 0.000504075514164981, -2.35058032832894 },
                new double[] { 104.145798201497, 2.80256425000402E-06, -3.49368461627364E-06, -2.39219308895144E-06, 1.81952256639306E-06, -1.94993321933623E-06, 7.06027928387698E-06, 7.01981011275166E-05, 0.000138373670580449, -0.645257345031474 },
                new double[] { 242.123077020588, 9.00824221261587E-06, -1.12297005614437E-05, -7.689192102589E-06, 5.84846541151762E-06, -6.26764250277745E-06, 2.26937548148953E-05, 0.000225636753569347, 0.000444772512580016, -2.07404146617259 },
                new double[] { 171.808759436683, 9.60879168943052E-06, -1.19783472456447E-05, -8.2018049702981E-06, 6.23836308744075E-06, -6.68548535731617E-06, 2.42066717959233E-05, 0.000240679203812988, 0.000474424013376051, -2.21231089725078 },
                new double[] { 203.147921684494, -4.5041210583463E-06, 5.61485022387842E-06, 3.8445962076139E-06, -2.92423269243614E-06, 3.13382127359318E-06, -1.13468773577097E-05, -0.000112818376692303, -0.000222386256126583, 1.03702073308629 },
                new double[] { 200.496565335776, 2.90265583302585E-06, -3.61845908969372E-06, -2.47762852723099E-06, 1.88450551963371E-06, -2.01957368695105E-06, 7.31243213181187E-06, 7.27051762225983E-05, 0.000143315587422421, -0.668302250211177 },
                new double[] { 244.774433369306, 1.60146531058558E-06, -1.99639123366069E-06, -1.36696743169296E-06, 1.0397271781315E-06, -1.11424755644407E-06, 4.03444536090092E-06, 4.01132006970784E-05, 7.90706689741683E-05, -0.368718482875124 }
            };

            Assert.IsTrue(expected.Get(null, 0, 2).IsEqual(projection, 1e-6));

            // Test the classify method
            for (int i = 0; i < 5; i++)
            {
                int actual = results[i];
                Assert.AreEqual(0, actual);
            }

            for (int i = 5; i < 10; i++)
            {
                int actual = results[i];
                Assert.AreEqual(1, actual);
            }
        }