Example #1
0
        public void GammaSigmaTest()
        {
            var dtw = new DynamicTimeWarping(1);
            var gaussian = new Gaussian<DynamicTimeWarping>(dtw, 1);

            double expected, actual, gamma, sigma;

            expected = 0.01;
            gaussian.Sigma = expected;
            gamma = gaussian.Gamma;

            gaussian.Gamma = gamma;
            actual = gaussian.Sigma;

            Assert.AreEqual(expected, actual);


            expected = 0.01;
            gaussian.Gamma = expected;
            sigma = gaussian.Sigma;

            gaussian.Sigma = sigma;
            actual = gaussian.Gamma;

            Assert.AreEqual(expected, actual, 1e-12);
        }
        public void DistanceTest()
        {
            Gaussian dense = new Gaussian(3.6);
            SparseGaussian target = new SparseGaussian(3.6);

            double[] sx = { 1, -0.555556, 2, +0.250000, 3, -0.864407, 4, -0.916667 };
            double[] sy = { 1, -0.666667, 2, -0.166667, 3, -0.864407, 4, -0.916667 };
            double[] sz = { 1, -0.944444, 3, -0.898305, 4, -0.916667 };

            double[] dx = { -0.555556, +0.250000, -0.864407, -0.916667 };
            double[] dy = { -0.666667, -0.166667, -0.864407, -0.916667 };
            double[] dz = { -0.944444, +0.000000, -0.898305, -0.916667 };

            double expected, actual;

            expected = dense.Distance(dx, dy);
            actual = target.Distance(sx, sy);
            Assert.AreEqual(expected, actual);

            expected = dense.Distance(dx, dz);
            actual = target.Distance(sx, sz);
            Assert.AreEqual(expected, actual);

            expected = dense.Distance(dy, dz);
            actual = target.Distance(sy, sz);
            Assert.AreEqual(expected, actual);
        }
Example #3
0
        public void SVMGaussianKernel()
        {
            Console.WriteLine("SottoProgramma chiamato: GaussianKernel; Kernel = Gaussian.");
            this.clock = DateTime.Now;

            // if sigma is to be set, uncomment the next two lines (and line >> Kernel = Kker in the teacher properties)
            // otherwise, comment the following two lines and uncomment
            // >> UseKernelEstimation = false
            // in such a way, SMO algorithm shall optimize the constrained
            // problem
            //           min _{w}  || w ||^2 / 2
            //           given y_i (x_i * w + b) >= 1
            // to find the parameters ( w , b ) \in R ^ (d x 1)

            IKernel ker = new Gaussian(0.1);

            Accord.Statistics.Kernels.Gaussian Kker = (Gaussian)ker;

            var teacher = new MulticlassSupportVectorLearning <Gaussian>()
            {
                Learner = (p) => new SequentialMinimalOptimization <Gaussian>()
                {
                    // UseComplexityHeuristic = false,
                    Complexity = 3
                },
                Kernel = Kker
            };

            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

            // NOTE: here <<teacher>> is the MODEL (so with its parameters and optimization)
            // so there KERNEL PARAMETERS are comptued (accordingly to the proper command,
            // based on an initial guess that has been ESTIMATED)
            // while <<machine>> is the learning algorithm itself, where the MODEL PARAMETERS
            // are indeed computed, using the SMO before set in the teacher properties


            // here learning algorithm is taught the training examples to learn
            // the model parameters ( w , b ) \in R ^ (d + 1)
            var machine = teacher.Learn(DataSets[1].ItemsFeatures, DataSets[1].CatIDs);

            // learned being ( w , b ) , we can predict the categories the test examples belong to
            int[] predicted = machine.Decide(DataSets[0].ItemsFeatures);

            // error (zero to one loss) is computed
            double error = new ZeroOneLoss(DataSets[0].CatIDs).Loss(predicted);

            // results print
            PrintReport(predicted, error, "Gaussian");

            Console.WriteLine("SottoProgramma GaussianKernel terminato.\nErrore: {0}", error);
            Console.WriteLine("Tempo richiesto per l'operazione: " + (DateTime.Now - clock).TotalSeconds + " secondi.");
        }
Example #4
0
        public void GaussianFunctionTest()
        {
            var x = new double[] { 0, 4, 2, 1 };
            var y = new double[] { 3, 2, };

            var dtw = new DynamicTimeWarping(1);
            IKernel gaussian = new Gaussian<DynamicTimeWarping>(dtw, 1);

            double actual = gaussian.Function(x, y);

            Assert.AreEqual(0.3407192298459587, actual);


            gaussian = new Gaussian<DynamicTimeWarping>(dtw, 11.5);

            x = new double[] { 0.2, 5 };
            y = new double[] { 3, 0.7 };

            actual = gaussian.Function(x, y);

            Assert.AreEqual(0.99065918303292089, actual);
        }
        public void GaussianDistanceTest()
        {
            var gaussian = new Gaussian<Linear>(new Linear(0), 1);

            double[] x = { 1, 1 };
            double[] y = { 1, 1 };

            double actual = gaussian.Distance(x, y);
            double expected = 0;

            Assert.AreEqual(expected, actual);


            gaussian = new Gaussian<Linear>(new Linear(0), 11.5);

            x = new double[] { 0.2, 0.5 };
            y = new double[] { 0.3, -0.7 };

            actual = gaussian.Distance(x, y);
            expected = Accord.Statistics.Tools.Distance(gaussian, x, y);

            Assert.AreEqual(expected, actual, 1e-10);
        }
Example #6
0
        public void GaussianDistanceTest()
        {
            IDistance gaussian = new Gaussian(1);

            double[] x = { 1, 1 };
            double[] y = { 1, 1 };

            double actual = gaussian.Distance(x, y);
            double expected = 0;

            Assert.AreEqual(expected, actual);


            gaussian = new Gaussian(11.5);

            x = new double[] { 0.2, 0.5 };
            y = new double[] { 0.3, -0.7 };

            actual = gaussian.Distance(x, y);
            expected = 341.46531595796711;

            Assert.AreEqual(expected, actual, 1e-10);
        }
Example #7
0
        public void GaussianFunctionTest()
        {
            IKernel gaussian = new Gaussian(1);

            double[] x = { 1, 1 };
            double[] y = { 1, 1 };

            double actual = gaussian.Function(x, y);

            double expected = 1;

            Assert.AreEqual(expected, actual);


            gaussian = new Gaussian(11.5);

            x = new double[] { 0.2, 5 };
            y = new double[] { 3, 0.7 };

            actual = gaussian.Function(x, y);
            expected = 0.9052480234;

            Assert.AreEqual(expected, actual, 1e-10);
        }
Example #8
0
        public void FunctionTest2()
        {
            // Tested against R's kernlab

            double[][] data = 
            {
                new double[] { 5.1, 3.5, 1.4, 0.2 },
                new double[] { 5.0, 3.6, 1.4, 0.2 },
                new double[] { 4.9, 3.0, 1.4, 0.2 },
                new double[] { 5.8, 4.0, 1.2, 0.2 },
                new double[] { 4.7, 3.2, 1.3, 0.2 },
            };

            // rbf <- rbfdot(sigma = 1)

            // R's sigma is framework's Gaussian's gamma:
            Gaussian kernel = new Gaussian() { Gamma = 1 };

            // Compute the kernel matrix
            double[,] actual = new double[5, 5];
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 5; j++)
                    actual[i, j] = kernel.Function(data[i], data[j]);

            double[,] expected =
            {
                { 1.0000000, 0.9801987, 0.7482636, 0.4584060, 0.7710516 },
                { 0.9801987, 1.0000000, 0.6907343, 0.4317105, 0.7710516 },
                { 0.7482636, 0.6907343, 1.0000000, 0.1572372, 0.9139312 },
                { 0.4584060, 0.4317105, 0.1572372, 1.0000000, 0.1556726 },
                { 0.7710516, 0.7710516, 0.9139312, 0.1556726, 1.0000000 },
            };

            // Assert both are equal
            for (int i = 0; i < 5; i++)
                for (int j = 0; j < 5; j++)
                    Assert.AreEqual(expected[i, j], actual[i, j], 1e-6);
        }
        public void KernelFunctionCacheConstructorTest6()
        {
            IKernel kernel = new Gaussian(0.6);

            int cacheSize = inputs.Length;

            KernelFunctionCache target = new KernelFunctionCache(kernel, inputs, cacheSize);

            Assert.AreEqual(10, target.Size);
            Assert.AreEqual(0, target.Hits);
            Assert.AreEqual(0, target.Misses);

            for (int i = 0; i < inputs.Length; i++)
            {
                double expected = kernel.Function(inputs[i], inputs[i]);
                double actual = target.GetOrCompute(i);

                Assert.AreEqual(expected, actual);
            }

            Assert.AreEqual(0, target.Hits);


            for (int i = 0; i < inputs.Length; i++)
            {
                for (int j = 0; j < inputs.Length; j++)
                {
                    double expected = kernel.Function(inputs[i], inputs[j]);
                    double actual = target.GetOrCompute(i, j);

                    Assert.AreEqual(expected, actual);
                }
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                for (int j = 0; j < inputs.Length; j++)
                {
                    double expected = kernel.Function(inputs[i], inputs[j]);
                    double actual = target.GetOrCompute(i, j);

                    Assert.AreEqual(expected, actual);
                }
            }

            Assert.AreEqual(45, target.Misses);
            Assert.AreEqual(135, target.Hits);
            Assert.AreEqual(1.0, target.Usage);
        }
Example #10
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            if (dgvAnalysisSource.Rows.Count == 0)
            {
                MessageBox.Show("Please load the training data before clicking this button");
                return;
            }

            lbStatus.Text = "Gathering data. This may take a while...";
            Application.DoEvents();


            // Extract inputs and outputs
            int rows = dgvAnalysisSource.Rows.Count;
            double[][] input = Jagged.Zeros(rows, 32 * 32);
            int[] output = new int[rows];
            for (int i = 0; i < rows; i++)
            {
                input.SetRow(i, (double[])dgvAnalysisSource.Rows[i].Cells["colTrainingFeatures"].Value);
                output[i] = (int)dgvAnalysisSource.Rows[i].Cells["colTrainingLabel"].Value;
            }

            // Create the chosen Kernel with given parameters
            IKernel kernel;
            if (rbGaussian.Checked)
                kernel = new Gaussian((double)numSigma.Value);
            else
                kernel = new Polynomial((int)numDegree.Value, (double)numConstant.Value);

            // Create the Kernel Discriminant Analysis using the selected Kernel
            kda = new KernelDiscriminantAnalysis(kernel)
            {
                Threshold = (double)numThreshold.Value,
                Regularization = (double)numRegularization.Value
            };


            lbStatus.Text = "Computing the analysis. This may take a significant amount of time...";
            Application.DoEvents();

            // Compute the analysis. 
            kda.Learn(input, output);


            // Show information about the analysis in the form
            dgvPrincipalComponents.DataSource = kda.Discriminants;
            dgvFeatureVectors.DataSource = new ArrayDataView(kda.DiscriminantVectors);
            dgvClasses.DataSource = kda.Classes;

            // Create the component graphs
            distributionView.DataSource = kda.Discriminants;
            cumulativeView.DataSource = kda.Discriminants;

            lbStatus.Text = "Analysis complete. Click Classify to test the analysis.";

            btnClassify.Enabled = true;
        }
        public void ThresholdTest()
        {
            double[,] inputs = 
            {
                { 1 },
                { 2 },
                { 3 },
            };

            int[] output = { 0, 1, 1 };

            IKernel kernel = new Gaussian(0.1);

            KernelDiscriminantAnalysis target = new KernelDiscriminantAnalysis(inputs, output, kernel);

            bool thrown = false;
            try { target.Threshold = -1; }
            catch (ArgumentOutOfRangeException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { target.Threshold = 1.1; }
            catch (ArgumentOutOfRangeException) { thrown = true; }
            Assert.IsTrue(thrown);

            target.Threshold = 1.0;

            target.Compute();

            Assert.AreEqual(2, target.Classes.Count);
            Assert.AreEqual(0, target.Classes[0].Number);
            Assert.AreEqual(1, target.Classes[0].Indices.Length);
            Assert.AreEqual(0, target.Classes[0].Indices[0]);
            Assert.AreEqual(1, target.Classes[1].Number);
            Assert.AreEqual(2, target.Classes[1].Indices.Length);
            Assert.AreEqual(1, target.Classes[1].Indices[0]);
            Assert.AreEqual(2, target.Classes[1].Indices[1]);
            Assert.AreEqual(0, target.CumulativeProportions.Length);
            Assert.AreEqual(3, target.DiscriminantMatrix.GetLength(0)); // dimension
            Assert.AreEqual(0, target.DiscriminantMatrix.GetLength(1)); // components kept
            Assert.AreEqual(0, target.DiscriminantProportions.Length);
            Assert.AreEqual(0, target.Discriminants.Count);
        }
        public void ComputeTest()
        {
            double[,] inputs = 
            {
                { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
            };

            int[] output = { 0, 1 };

            IKernel kernel = new Gaussian(0.1);
            KernelDiscriminantAnalysis target = new KernelDiscriminantAnalysis(inputs, output, kernel);
            target.Threshold = 0;

            target.Compute();

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

            double[,] expected = 
            {
                {  1.0, -1.0},
                { -1.0, -1.0},
            };

            Assert.IsTrue(Matrix.IsEqual(actual, expected));
        }
Example #13
0
    private void btnKernel_Click(object sender, EventArgs e)
    {
      double[,] sourceMatrix;
      double[,] inputs;
      int[] labels;

      GetData(out sourceMatrix, out inputs, out labels);

      //_svm.SimpleSMO(inputs, labels);
      // Perform classification
      SequentialMinimalOptimization smo;
      var kernel = new Gaussian(1.2236000);
      // Creates the Support Vector Machine using the selected kernel
      var svm = new KernelSupportVectorMachine(kernel, 2);
      //SupportVectorMachine svm = new SupportVectorMachine(2);

      // Creates a new instance of the SMO Learning Algorithm
      smo = new SequentialMinimalOptimization(svm, inputs.ToArray(), labels);

      // Set learning parameters
      smo.Complexity = 1.0;
      smo.Tolerance = 0.001;


      bool converged = true;

      try
      {
        // Run
        double error = smo.Run();
      }
      catch
      {
        converged = false;
      }

      //var a = sourceMatrix.Submatrix(0, sourceMatrix.GetLength(0) - 1, 0, 2);
      //CreateScatterplot(graphInput, a, svm);

      var ranges = Matrix.Range(sourceMatrix);
      double[][] map = Matrix.CartesianProduct(
          Matrix.Interval(ranges[0], 0.05),
          Matrix.Interval(ranges[1], 0.05));

      var result = map.Apply(svm.Compute).Apply(Math.Sign);

      var graph2 = map.ToMatrix().InsertColumn(result.ToDouble());

      CreateDecisionBoundaryplot(graphInput, graph2, sourceMatrix);
    }
        public void learn_precomputed()
        {
            #region doc_precomputed
            // As an example, we will try to learn a decision machine 
            // that can replicate the "exclusive-or" logical function:

            double[][] inputs =
            {
                new double[] { 0, 0 }, // the XOR function takes two booleans
                new double[] { 0, 1 }, // and computes their exclusive or: the
                new double[] { 1, 0 }, // output is true only if the two booleans
                new double[] { 1, 1 }  // are different
            };

            int[] xor = // this is the output of the xor function
            {
                0, // 0 xor 0 = 0 (inputs are equal)
                1, // 0 xor 1 = 1 (inputs are different)
                1, // 1 xor 0 = 1 (inputs are different)
                0, // 1 xor 1 = 0 (inputs are equal)
            };

            // Let's use a Gaussian kernel
            var kernel = new Gaussian(0.1);

            // Create a pre-computed Gaussian kernel matrix
            var precomputed = new Precomputed(kernel.ToJagged(inputs));

            // Now, we can create the sequential minimal optimization teacher
            var learn = new SequentialMinimalOptimization<Precomputed, int>()
            {
                Kernel = precomputed // set the precomputed kernel we created
            };

            // And then we can obtain the SVM by using Learn
            var svm = learn.Learn(precomputed.Indices, xor);

            // Finally, we can obtain the decisions predicted by the machine:
            bool[] prediction = svm.Decide(precomputed.Indices);

            // We can also compute the machine prediction to new samples
            double[][] sample =
            {
                new double[] { 0, 1 } 
            };

            // Update the precomputed kernel with the new samples
            precomputed = new Precomputed(kernel.ToJagged2(inputs, sample));

            // Update the SVM kernel
            svm.Kernel = precomputed;

            // Compute the predictions to the new samples
            bool[] newPrediction = svm.Decide(precomputed.Indices);
            #endregion

            Assert.AreEqual(prediction, Classes.Decide(xor));
            Assert.AreEqual(newPrediction.Length, 1);
            Assert.AreEqual(newPrediction[0], true);
        }
        public void RevertTest2()
        {

            string path = @"..\..\..\..\Unit Tests\Accord.Tests.Statistics\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.ToMatrix();

            IKernel kernel = new Gaussian(5);

            // Create analysis
            KernelPrincipalComponentAnalysis target = new KernelPrincipalComponentAnalysis(matrix,
                kernel, AnalysisMethod.Center, centerInFeatureSpace: false);

            target.Compute();

            double[,] forward = target.Result;

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

            Assert.IsTrue(!reversion.HasNaN());
        }
        public void TransformTest5()
        {
            int element = 10;
            int dimension = 20;

            double[,] data = new double[element, dimension];

            int x = 0;

            for (int i = 0; i < element; i++)
            {
                for (int j = 0; j < dimension; j++)
                    data[i, j] = x;

                x += 10;
            }

            IKernel kernel = new Gaussian(10.0);
            var kpca = new KernelPrincipalComponentAnalysis(data, kernel, AnalysisMethod.Center);

            kpca.Compute();

            double[,] result = kpca.Transform(data, 2);

            double[,] expected = new double[,] 
            {
                { -0.23053882357602, -0.284413654763538 },
                { -0.387883199575312, -0.331485820285834 },
                { -0.422077400361521, -0.11134948984113 },
                { -0.322265008788599, 0.23632015508648 },
                { -0.12013575394419, 0.490928809797139 },
                { 0.120135753938394, 0.490928809796094 },
                { 0.322265008787236, 0.236320155085067 },
                { 0.422077400363969, -0.111349489837512 },
                { 0.38788319957867, -0.331485820278937 },
                { 0.230538823577373, -0.28441365475783 } 
            };

            Assert.IsTrue(result.IsEqual(expected, 1e-10));
        }
        public void TransformTest4()
        {
            // 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 =
            {
                #region Fisher's iris dataset
                { 5.1, 3.5,  1.4, 0.2, 0 },
                { 4.9, 3.0,  1.4, 0.2, 0 },
                { 4.7, 3.2,  1.3, 0.2, 0 },
                { 4.6, 3.1,  1.5, 0.2, 0 },
                { 5.0, 3.6,  1.4, 0.2, 0 },
                { 5.4, 3.9,  1.7, 0.4, 0 },
                { 4.6, 3.4,  1.4, 0.3, 0 },
                { 5.0, 3.4,  1.5, 0.2, 0 },
                { 4.4, 2.9,  1.4, 0.2, 0 },
                { 4.9, 3.1,  1.5, 0.1, 0 },
                { 5.4, 3.7,  1.5, 0.2, 0 },
                { 4.8, 3.4,  1.6, 0.2, 0 },
                { 4.8, 3.0,  1.4, 0.1, 0 },
                { 4.3, 3.0,  1.1, 0.1, 0 },
                { 5.8, 4.0,  1.2, 0.2, 0 },
                { 5.7, 4.4,  1.5, 0.4, 0 },
                { 5.4, 3.9,  1.3, 0.4, 0 },
                { 5.1, 3.5,  1.4, 0.3, 0 },
                { 5.7, 3.8,  1.7, 0.3, 0 },
                { 5.1, 3.8,  1.5, 0.3, 0 },
                { 5.4, 3.4,  1.7, 0.2, 0 },
                { 5.1, 3.7,  1.5, 0.4, 0 },
                { 4.6, 3.6,  1.0, 0.2, 0 },
                { 5.1, 3.3,  1.7, 0.5, 0 },
                { 4.8, 3.4,  1.9, 0.2, 0 },
                { 5.0, 3.0,  1.6, 0.2, 0 },
                { 5.0, 3.4,  1.6, 0.4, 0 },
                { 5.2, 3.5,  1.5, 0.2, 0 },
                { 5.2, 3.4,  1.4, 0.2, 0 },
                { 4.7, 3.2,  1.6, 0.2, 0 },
                { 4.8, 3.1,  1.6, 0.2, 0 },
                { 5.4, 3.4,  1.5, 0.4, 0 },
                { 5.2, 4.1,  1.5, 0.1, 0 },
                { 5.5, 4.2,  1.4, 0.2, 0 },
                { 4.9, 3.1,  1.5, 0.2, 0 },
                { 5.0, 3.2,  1.2, 0.2, 0 },
                { 5.5, 3.5,  1.3, 0.2, 0 },
                { 4.9, 3.6,  1.4, 0.1, 0 },
                { 4.4, 3.0,  1.3, 0.2, 0 },
                { 5.1, 3.4,  1.5, 0.2, 0 },
                { 5.0, 3.5,  1.3, 0.3, 0 },
                { 4.5, 2.3,  1.3, 0.3, 0 },
                { 4.4, 3.2,  1.3, 0.2, 0 },
                { 5.0, 3.5,  1.6, 0.6, 0 },
                { 5.1, 3.8,  1.9, 0.4, 0 },
                { 4.8, 3.0,  1.4, 0.3, 0 },
                { 5.1, 3.8,  1.6, 0.2, 0 },
                { 4.6, 3.2,  1.4, 0.2, 0 },
                { 5.3, 3.7,  1.5, 0.2, 0 },
                { 5.0, 3.3,  1.4, 0.2, 0 },
                { 7.0, 3.2,  4.7, 1.4, 1 },
                { 6.4, 3.2,  4.5, 1.5, 1 },
                { 6.9, 3.1,  4.9, 1.5, 1 },
                { 5.5, 2.3,  4.0, 1.3, 1 },
                { 6.5, 2.8,  4.6, 1.5, 1 },
                { 5.7, 2.8,  4.5, 1.3, 1 },
                { 6.3, 3.3,  4.7, 1.6, 1 },
                { 4.9, 2.4,  3.3, 1.0, 1 },
                { 6.6, 2.9,  4.6, 1.3, 1 },
                { 5.2, 2.7,  3.9, 1.4, 1 },
                { 5.0, 2.0,  3.5, 1.0, 1 },
                { 5.9, 3.0,  4.2, 1.5, 1 },
                { 6.0, 2.2,  4.0, 1.0, 1 },
                { 6.1, 2.9,  4.7, 1.4, 1 },
                { 5.6, 2.9,  3.6, 1.3, 1 },
                { 6.7, 3.1,  4.4, 1.4, 1 },
                { 5.6, 3.0,  4.5, 1.5, 1 },
                { 5.8, 2.7,  4.1, 1.0, 1 },
                { 6.2, 2.2,  4.5, 1.5, 1 },
                { 5.6, 2.5,  3.9, 1.1, 1 },
                { 5.9, 3.2,  4.8, 1.8, 1 },
                { 6.1, 2.8,  4.0, 1.3, 1 },
                { 6.3, 2.5,  4.9, 1.5, 1 },
                { 6.1, 2.8,  4.7, 1.2, 1 },
                { 6.4, 2.9,  4.3, 1.3, 1 },
                { 6.6, 3.0,  4.4, 1.4, 1 },
                { 6.8, 2.8,  4.8, 1.4, 1 },
                { 6.7, 3.0,  5.0, 1.7, 1 },
                { 6.0, 2.9,  4.5, 1.5, 1 },
                { 5.7, 2.6,  3.5, 1.0, 1 },
                { 5.5, 2.4,  3.8, 1.1, 1 },
                { 5.5, 2.4,  3.7, 1.0, 1 },
                { 5.8, 2.7,  3.9, 1.2, 1 },
                { 6.0, 2.7,  5.1, 1.6, 1 },
                { 5.4, 3.0,  4.5, 1.5, 1 },
                { 6.0, 3.4,  4.5, 1.6, 1 },
                { 6.7, 3.1,  4.7, 1.5, 1 },
                { 6.3, 2.3,  4.4, 1.3, 1 },
                { 5.6, 3.0,  4.1, 1.3, 1 },
                { 5.5, 2.5,  4.0, 1.3, 1 },
                { 5.5, 2.6,  4.4, 1.2, 1 },
                { 6.1, 3.0,  4.6, 1.4, 1 },
                { 5.8, 2.6,  4.0, 1.2, 1 },
                { 5.0, 2.3,  3.3, 1.0, 1 },
                { 5.6, 2.7,  4.2, 1.3, 1 },
                { 5.7, 3.0,  4.2, 1.2, 1 },
                { 5.7, 2.9,  4.2, 1.3, 1 },
                { 6.2, 2.9,  4.3, 1.3, 1 },
                { 5.1, 2.5,  3.0, 1.1, 1 },
                { 5.7, 2.8,  4.1, 1.3, 1 },
                { 6.3, 3.3,  6.0, 2.5, 2 },
                { 5.8, 2.7,  5.1, 1.9, 2 },
                { 7.1, 3.0,  5.9, 2.1, 2 },
                { 6.3, 2.9,  5.6, 1.8, 2 },
                { 6.5, 3.0,  5.8, 2.2, 2 },
                { 7.6, 3.0,  6.6, 2.1, 2 },
                { 4.9, 2.5,  4.5, 1.7, 2 },
                { 7.3, 2.9,  6.3, 1.8, 2 },
                { 6.7, 2.5,  5.8, 1.8, 2 },
                { 7.2, 3.6,  6.1, 2.5, 2 },
                { 6.5, 3.2,  5.1, 2.0, 2 },
                { 6.4, 2.7,  5.3, 1.9, 2 },
                { 6.8, 3.0,  5.5, 2.1, 2 },
                { 5.7, 2.5,  5.0, 2.0, 2 },
                { 5.8, 2.8,  5.1, 2.4, 2 },
                { 6.4, 3.2,  5.3, 2.3, 2 },
                { 6.5, 3.0,  5.5, 1.8, 2 },
                { 7.7, 3.8,  6.7, 2.2, 2 },
                { 7.7, 2.6,  6.9, 2.3, 2 },
                { 6.0, 2.2,  5.0, 1.5, 2 },
                { 6.9, 3.2,  5.7, 2.3, 2 },
                { 5.6, 2.8,  4.9, 2.0, 2 },
                { 7.7, 2.8,  6.7, 2.0, 2 },
                { 6.3, 2.7,  4.9, 1.8, 2 },
                { 6.7, 3.3,  5.7, 2.1, 2 },
                { 7.2, 3.2,  6.0, 1.8, 2 },
                { 6.2, 2.8,  4.8, 1.8, 2 },
                { 6.1, 3.0,  4.9, 1.8, 2 },
                { 6.4, 2.8,  5.6, 2.1, 2 },
                { 7.2, 3.0,  5.8, 1.6, 2 },
                { 7.4, 2.8,  6.1, 1.9, 2 },
                { 7.9, 3.8,  6.4, 2.0, 2 },
                { 6.4, 2.8,  5.6, 2.2, 2 },
                { 6.3, 2.8,  5.1, 1.5, 2 },
                { 6.1, 2.6,  5.6, 1.4, 2 },
                { 7.7, 3.0,  6.1, 2.3, 2 },
                { 6.3, 3.4,  5.6, 2.4, 2 },
                { 6.4, 3.1,  5.5, 1.8, 2 },
                { 6.0, 3.0,  4.8, 1.8, 2 },
                { 6.9, 3.1,  5.4, 2.1, 2 },
                { 6.7, 3.1,  5.6, 2.4, 2 },
                { 6.9, 3.1,  5.1, 2.3, 2 },
                { 5.8, 2.7,  5.1, 1.9, 2 },
                { 6.8, 3.2,  5.9, 2.3, 2 },
                { 6.7, 3.3,  5.7, 2.5, 2 },
                { 6.7, 3.0,  5.2, 2.3, 2 },
                { 6.3, 2.5,  5.0, 1.9, 2 },
                { 6.5, 3.0,  5.2, 2.0, 2 },
                { 6.2, 3.4,  5.4, 2.3, 2 },
                { 5.9, 3.0,  5.1, 1.8, 2 },
                #endregion
            };


            // 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(data, kernel);

            kpc.Compute(2);

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

            double[] expected_eig = { 28.542404060412132, 15.235596653653861 };
            double[,] expected_pcv = 
            {
                #region expected PCV without R's / m normalization
                { -0.0266876243479222, -0.00236424647855596 },
                { -0.0230827502249994, -0.00182207284533632 },
                { -0.0235846044938792, -0.00184417084258023 },
                { -0.0219741114149703, -0.00162806197434679 },
                { -0.0262369254935451, -0.00228351232176506 },
                { -0.0194129527129315, -0.00128157547584046 },
                { -0.0233710173690426, -0.00183018780267092 },
                { -0.0270621345426091, -0.00244551460941156 },
                { -0.01660360115437, -0.000759995006404066 },
                { -0.0241543595644871, -0.00200028851593623 },
                { -0.0229396684027426, -0.00178791975184668 },
                { -0.0141003945759371, -0.000349601250510858 },
                { -0.0122801944616023, -0.00011745003303124 },
                { -0.0195599909514198, -0.00123924882430174 },
                { -0.0267285199984888, -0.00237835151986576 },
                { -0.0164051441608544, -0.000826433392421186 },
                { -0.0241385103747907, -0.00196921837902471 },
                { -0.0228276819006861, -0.00190280719845395 },
                { -0.0250090125071634, -0.00212322482498387 },
                { -0.018268574949505, -0.00103729989327242 },
                { -0.0239555047501124, -0.00216590896337712 },
                { -0.0218837974825259, -0.00180921340210779 },
                { -0.0228699114274226, -0.00189079843025579 },
                { -0.0262414571955617, -0.00238666692022459 },
                { -0.02628286882499, -0.00232756740052467 },
                { -0.0261369413490628, -0.00229661111040973 },
                { -0.0237893959503383, -0.00195315162891338 },
                { -0.0237354902927562, -0.00197089686864334 },
                { -0.0234996712936547, -0.0019545398678434 },
                { -0.0179796342021205, -0.00100004281923827 },
                { -0.0143171193045046, -0.000421228427584423 },
                { -0.0241702773143143, -0.00196078350204665 },
                { -0.0215781675204649, -0.00158425565875557 },
                { -0.0174405137049866, -0.000872162100068597 },
                { -0.0268982927662575, -0.00242925852652081 },
                { -0.0261930727206913, -0.00227548913089953 },
                { -0.00807266421494459, 0.000505384268295841 },
                { -0.0189832707329651, -0.00111618515851182 },
                { -0.023789443724428, -0.00203239968623136 },
                { -0.0201457716377357, -0.00150731437393246 },
                { -0.0226387870826046, -0.00174799717649726 },
                { -0.0237772220904885, -0.00192536309172948 },
                { -0.0227864577886965, -0.00172757669197999 },
                { -0.0241368046325238, -0.00197147776349598 },
                { 0.0162307596401467, -0.00932217153629181 },
                { 0.00924104683890504, -0.0371256298132236 },
                { 0.0172460604733757, -0.00601678602419225 },
                { 0.0164784470762724, -0.00012129053123478 },
                { 0.00225808467595593, -0.0155701555363185 },
                { 0.0152659569368524, -0.00695503994803249 },
                { 0.00795619200816849, -0.034188555904496 },
                { 0.00255986394744671, -0.0156335839305463 },
                { 0.0157735235376026, -0.0339711483141172 },
                { 0.00860192955815661, -0.0310332456913026 },
                { 0.0188286198627367, -0.0143146603067418 },
                { 0.0081385823965042, -0.0358483794263587 },
                { 0.0131727085950618, -0.00748671161967017 },
                { 0.0150373592446138, -0.0269773780381651 },
                { 0.0126779242124717, -0.0162727482334416 },
                { 0.00983265072294127, -0.0416039968698012 },
                { 0.0162669562079483, -0.000151449934923387 },
                { 0.0137854766363786, -0.0375070307423622 },
                { 0.0170058660389757, -0.0184237621007135 },
                { 0.0154946725649067, -0.0227889410670457 },
                { 0.014708096275464, -0.011169199019916 },
                { 0.0135541309647514, 0.00627293040317239 },
                { 0.0153982178833786, 0.0228745884070871 },
                { 0.0186116855914761, -0.0238281923214434 },
                { 0.00661605660296714, -0.0332168101819555 },
                { 0.00812230548276198, -0.0380947707628449 },
                { 0.00704157480127114, -0.0353293378234606 },
                { 0.0118813500247593, -0.0433955442329169 },
                { 0.0168403649935284, 0.00717417511929008 },
                { 0.0144885311444922, -0.0128879186387195 },
                { 0.0148385454088314, 0.00481616750741218 },
                { 0.0127847825706042, -0.0211295878510692 },
                { 0.0126141523297424, -0.0394948238730571 },
                { 0.0105804278587419, -0.0411832808826231 },
                { 0.0185081272399827, -0.0181339486962481 },
                { 0.0124993892884636, -0.0434407731971394 },
                { 0.0135227934497893, -0.0415894662412569 },
                { 0.0136028421755366, -0.0388446289823116 },
                { 0.0144604273990706, -0.0404041262573942 },
                { 0.0165646866155949, -0.0294021220435322 },
                { 0.00146858312783178, -0.0134333124454357 },
                { 0.0137785343752508, -0.0429733697468562 },
                { 0.00510997410924024, 0.0292833047881736 },
                { 0.014720812085274, 0.00944264118212137 },
                { 0.00598583015620509, 0.038742545754176 },
                { 0.0125544895333347, 0.0349170237345097 },
                { 0.00140911493699792, 0.0164126558963803 },
                { 0.00546764790022381, -0.0140904440836446 },
                { 0.0029496609416271, 0.0249945804373137 },
                { 0.00769932014045035, 0.0313261912102264 },
                { 0.00266139821119332, 0.0231665860038695 },
                { 0.0147368620502789, 0.0315131740192214 },
                { 0.0149582869669828, 0.0314622232024109 },
                { 0.0106818628524054, 0.0443273862959601 },
                { 0.0123400540017047, 0.00422397833506881 },
                { 0.0101542521522688, 0.0157643916651046 },
                { 0.000660568495239385, 0.0087957765410289 },
                { 0.000634971613911479, 0.00896839373841372 },
                { 0.0119909422310846, -0.0019235494568038 },
                { 0.00742254354227651, 0.0421145349479265 },
                { 0.0130658707704511, 0.000658712215109605 },
                { 0.00103199141821948, 0.0130131637684367 },
                { 0.0180388007633923, 0.0112135357385706 },
                { 0.00879897568153878, 0.0428371609763469 },
                { 0.00466754803065601, 0.0321456973019424 },
                { 0.0188135431637204, 0.00458127473828957 },
                { 0.0184728744733845, 0.00843677964296344 },
                { 0.0055676853191067, 0.0305087649038716 },
                { 0.0033635667326866, 0.026834775073324 },
                { 0.0108405706484462, 0.0394739066547236 },
                { 0.0172770225385115, 0.0124967454210229 },
                { 0.0100507351970463, 0.0166565450918105 },
                { 0.00209404741665691, 0.0205532162586405 },
                { 0.0078782378323636, 0.0341148825697675 },
                { 0.0132731813046484, 0.0368540207320806 },
                { 0.0182550250587539, 0.000797957664175355 },
                { 0.0102561686092287, 0.0420705939254378 },
                { 0.00857331992305152, 0.0423810139397453 },
                { 0.00964648674506066, 0.0337591223497657 },
                { 0.014720812085274, 0.00944264118212137 },
                { 0.00659947194015947, 0.0404655648392282 },
                { 0.011337029514041, 0.0378339231578959 },
                { 0.0154602034267052, 0.0153085911335171 },
                { 0.0152371977428677, 0.0355309408870963 },
                { 0.0096520854263212, 0.0316677099444034 },
                { 0.016280981143395, 0.011860068380509 } 
#endregion
            };

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

            double[,] irisSubset =
            {
                #region Iris subset for testing
                { 5.7,  4.4,  1.5,  0.4 },
                { 6.5,  3.0,  5.5,  1.8 },
                { 5.0,  2.3,  3.3,  1.0 },
                { 7.9,  3.8,  6.4,  2.0 },
                { 4.8,  3.0,  1.4,  0.1 },
                { 6.3,  2.5,  4.9,  1.5 },
                { 5.8,  2.7,  4.1,  1.0 },
                { 6.4,  2.8,  5.6,  2.1 },
                { 5.5,  2.6,  4.4,  1.2 },
                { 5.0,  3.3,  1.4,  0.2 },
                { 5.7,  2.8,  4.5,  1.3 },
                { 4.8,  3.4,  1.6,  0.2 },
                { 6.7,  3.3,  5.7,  2.5 },
                { 6.5,  3.0,  5.8,  2.2 },
                { 4.9,  3.1,  1.5,  0.2 },
                { 6.9,  3.1,  4.9,  1.5 },
                { 4.9,  3.6,  1.4,  0.1 },
                { 7.0,  3.2,  4.7,  1.4 },
                { 5.4,  3.0,  4.5,  1.5 },
                { 6.4,  3.2,  5.3,  2.3 },
                #endregion
            };

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

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


            double[,] proj = kpc.Transform(testing);

            double[,] expectedProjection =
            {
                #region expected projection without R's /m normalization
                { -0.262045246354652, 0.00522592803297944 },
                { 0.379100878015288, 0.588655883281293 },
                { 0.071459976634984, -0.256289120689712 },
                { 0.0208479105625222, 0.139976227370984 },
                { -0.634360697174036, -0.0253624940325211 },
                { 0.467841054198416, 0.0142078237631541 },
                { 0.3464723948387, -0.62695357333265 },
                { 0.327328144102457, 0.603762286061834 },
                { 0.351964514241981, -0.539035845068089 },
                { -0.759054821003877, -0.035920361137046 },
                { 0.449638018323254, -0.492049890038061 },
                { -0.732335083049923, -0.0341252836840602 },
                { 0.192183096200302, 0.580343336854431 },
                { 0.256170478557119, 0.639157216957949 },
                { -0.703212303846621, -0.0317868463626801 },
                { 0.3515430820112, 0.224868844202495 },
                { -0.722813976459246, -0.0325608519534802 },
                { 0.286990265042346, 0.102161459040097 },
                { 0.354904620698745, -0.390810675482863 },
                { 0.332125880099634, 0.566660263312128 } 
                #endregion
            };

            Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6));

        }
        public void GaussianReverseDistanceTest()
        {
            var gaussian = new Gaussian(4.2);

            var x = new double[] { 0.2, 0.5 };
            var y = new double[] { 0.3, -0.7 };

            double expected = Distance.SquareEuclidean(x, y);

            double df = gaussian.Distance(x, y);
            double actual = gaussian.ReverseDistance(df);

            Assert.AreEqual(expected, actual, 1e-10);
        }
        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));
        }
Example #21
0
        public void FunctionTest_EqualInputs()
        {
            var x = new double[] { 1, 2, 5, 1 };
            var y = new double[] { 1, 2, 5, 1 };

            var target = new Gaussian<Linear>(new Linear(1), 4.2);

            double expected = target.Function(x, y);
            double actual = target.Function(x, x);

            Assert.AreEqual(expected, actual);
        }
Example #22
0
        /// <summary>
        ///   Estimates a suitable value for SMO's Complexity parameter C.
        /// </summary>
        /// 
        private void btnEstimateC_Click(object sender, EventArgs e)
        {
            // Extract inputs
            int rows = dgvTrainingSource.Rows.Count;
            double[][] input = new double[rows][];
            for (int i = 0; i < rows; i++)
                input[i] = (double[])dgvTrainingSource.Rows[i].Cells["colTrainingFeatures"].Value;

            IKernel kernel;
            if (rbGaussian.Checked)
                kernel = new Gaussian((double)numSigma.Value);
            else
                kernel = new Polynomial((int)numDegree.Value, (double)numConstant.Value);

            numComplexity.Value = (decimal)SequentialMinimalOptimization.EstimateComplexity(kernel, input);
        }
Example #23
0
        public void FunctionTest_EqualInputs()
        {
            var x = new double[] { 1, 2, 5, 1 };
            var y = new double[] { 1, 2, 5, 1 };

            var target = new Gaussian<DynamicTimeWarping>(new DynamicTimeWarping(1), 4.2);

            double expected = target.Function(x, y);
            double actual = target.Function(x, x);

            Assert.AreEqual(expected, actual, 0.000001);
        }
        public void ConstructorTest1()
        {
            double[,] inputs = 
            {
                { 1, 1, },
                { 2, 2, },
                { 3, 3, },
            };

            int[] output = { 1, 2, 3 };

            IKernel kernel = new Gaussian(0.1);

            KernelDiscriminantAnalysis target = new KernelDiscriminantAnalysis(inputs, output, kernel);

            Assert.AreEqual(3, target.Classes.Count);
            Assert.AreEqual(0, target.Classes[0].Index);
            Assert.AreEqual(1, target.Classes[1].Index);
            Assert.AreEqual(2, target.Classes[2].Index);
            Assert.AreEqual(1, target.Classes[0].Number);
            Assert.AreEqual(2, target.Classes[1].Number);
            Assert.AreEqual(3, target.Classes[2].Number);

            Assert.AreEqual(output, target.Classifications);
            Assert.AreEqual(kernel, target.Kernel);
            Assert.AreEqual(1e-4, target.Regularization);
            Assert.AreEqual(inputs, target.Source);
            Assert.AreEqual(0.001, target.Threshold);

            Assert.IsNull(target.CumulativeProportions);
            Assert.IsNull(target.DiscriminantMatrix);
            Assert.IsNull(target.DiscriminantProportions);
            Assert.IsNull(target.Discriminants);
            Assert.IsNull(target.Eigenvalues);
            Assert.IsNull(target.Result);
            Assert.IsNull(target.ScatterBetweenClass);
            Assert.IsNull(target.ScatterMatrix);
            Assert.IsNull(target.ScatterWithinClass);
            Assert.IsNull(target.StandardDeviations);
            Assert.IsNull(target.Means);
        }
Example #25
0
 void IEstimable <TInput> .Estimate(TInput[] inputs)
 {
     this.Gamma = Gaussian.Estimate <TInput, TDistance>(inputs, distance).Gamma;
 }
        public void ComputeTest3()
        {
            // Schölkopf KPCA toy example
            double[][] inputs = scholkopf().ToArray();

            int[] output = Matrix.Expand(new int[,] { { 1 }, { 2 }, { 3 } }, new int[] { 30, 30, 30 }).GetColumn(0);

            IKernel kernel = new Gaussian(0.2);
            KernelDiscriminantAnalysis target = new KernelDiscriminantAnalysis(inputs, output, kernel);

            target.Compute();


            double[][] actual = target.Transform(inputs, 2);

            double[][] expected1 =
            {
                new double[] { 1.2785801485080475, 0.20539157505913622},
                new double[] { 1.2906613255489541, 0.20704272225753775},
                new double[] { 1.2978134597266808, 0.20802649628632208},
            };

            double[][] actual1 = actual.Submatrix(0, 2, 0, 1);

            Assert.IsTrue(Matrix.IsEqual(actual1, expected1, 0.0000001));

            // Assert the result equals the transformation of the input
            double[][] result = target.Result.ToArray();
            double[][] projection = target.Transform(inputs);
            Assert.IsTrue(Matrix.IsEqual(result, projection));
        }
Example #27
0
 void IEstimable <double[]> .Estimate(double[][] inputs)
 {
     this.Gamma = Gaussian.Estimate(distance, inputs).Gamma;
 }
Example #28
0
 /// <summary>
 ///   Estimates kernel parameters from the data.
 /// </summary>
 ///
 /// <param name="inputs">The input data.</param>
 ///
 void IEstimable <double[]> .Estimate(double[][] inputs)
 {
     this.Gamma = Gaussian.Estimate(innerKernel, inputs).Gamma;
 }
Example #29
0
        public void GammaSigmaSquaredTest()
        {
            Gaussian gaussian = new Gaussian(3.6);
            Assert.AreEqual(3.6 * 3.6, gaussian.SigmaSquared);
            Assert.AreEqual(3.6, gaussian.Sigma);
            Assert.AreEqual(1.0 / (2 * 3.6 * 3.6), gaussian.Gamma);

            gaussian.SigmaSquared = 81;
            Assert.AreEqual(81, gaussian.SigmaSquared);
            Assert.AreEqual(9, gaussian.Sigma);
            Assert.AreEqual(1.0 / (2 * 81), gaussian.Gamma);

            gaussian.Sigma = 6;
            Assert.AreEqual(36, gaussian.SigmaSquared);
            Assert.AreEqual(6, gaussian.Sigma);
            Assert.AreEqual(1.0 / (2 * 36), gaussian.Gamma);

            gaussian.Gamma = 1.0 / (2 * 49);
            Assert.AreEqual(49, gaussian.SigmaSquared, 1e-10);
            Assert.AreEqual(7, gaussian.Sigma, 1e-10);
            Assert.AreEqual(1.0 / (2 * 49), gaussian.Gamma);
        }
Example #30
0
        public void GammaSigmaSquaredTest()
        {
            var dtw = new DynamicTimeWarping(1);
            var gaussian = new Gaussian<DynamicTimeWarping>(dtw, 3.6);

            Assert.AreEqual(3.6 * 3.6, gaussian.SigmaSquared);
            Assert.AreEqual(3.6, gaussian.Sigma);
            Assert.AreEqual(1.0 / (2 * 3.6 * 3.6), gaussian.Gamma);

            gaussian.SigmaSquared = 81;
            Assert.AreEqual(81, gaussian.SigmaSquared);
            Assert.AreEqual(9, gaussian.Sigma);
            Assert.AreEqual(1.0 / (2 * 81), gaussian.Gamma);

            gaussian.Sigma = 6;
            Assert.AreEqual(36, gaussian.SigmaSquared);
            Assert.AreEqual(6, gaussian.Sigma);
            Assert.AreEqual(1.0 / (2 * 36), gaussian.Gamma);

            gaussian.Gamma = 1.0 / (2 * 49);
            Assert.AreEqual(49, gaussian.SigmaSquared, 1e-10);
            Assert.AreEqual(7, gaussian.Sigma, 1e-10);
            Assert.AreEqual(1.0 / (2 * 49), gaussian.Gamma);
        }
Example #31
0
        private void button1_Click(object sender, EventArgs e)
        {
            // Finishes and save any pending changes to the given data
            dgvAnalysisSource.EndEdit();

            if (dgvAnalysisSource.DataSource == null) return;

            // Creates a matrix from the source data table
            double[,] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToMatrix(out sourceColumns);

            int rows = sourceMatrix.GetLength(0);
            int cols = sourceMatrix.GetLength(1);


            // Creates a new Simple Descriptive Analysis
            sda = new DescriptiveAnalysis(sourceMatrix, sourceColumns);

            sda.Compute();

            // Populates statistics overview tab with analysis data
            dgvDistributionMeasures.DataSource = sda.Measures;


            IKernel kernel;
            if (rbGaussian.Checked)
            {
                kernel = new Gaussian((double)numSigma.Value);
            }
            else
            {
                kernel = new Polynomial((int)numDegree.Value, (double)numConstant.Value);
            }


            // Get only the input values (exclude the class label indicator column)
            double[,] data = sourceMatrix.Submatrix(null, startColumn: 0, endColumn: 1);

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


            // Creates the Kernel Principal Component Analysis of the given source
            kpca = new KernelPrincipalComponentAnalysis(data, kernel,
                (AnalysisMethod)cbMethod.SelectedValue);

            kpca.Center = cbCenter.Checked;

            // Compute the analysis
            kpca.Compute();


            
            double[,] result;

            if (kpca.Components.Count >= 2)
            {
                // Perform the transformation of the data using two components 
                result = kpca.Transform(data, 2);
            }
            else
            {
                result = kpca.Transform(data, 1);
                result = result.InsertColumn(Matrix.Vector(result.GetLength(0), 0.0));
            }

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

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

            // Create output table
            dgvProjectionResult.DataSource = new ArrayDataView(points, sourceColumns);
            dgvReversionSource.DataSource = new ArrayDataView(kpca.Result);


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

            dgvProjectionComponents.DataSource = kpca.Components;
            dgvReversionComponents.DataSource = kpca.Components;

            numComponents.Maximum = kpca.Components.Count;
            numNeighbor.Maximum = kpca.Result.GetLength(0);
            numNeighbor.Value = System.Math.Min(10, numNeighbor.Maximum);

            CreateComponentCumulativeDistributionGraph(graphCurve);
            CreateComponentDistributionGraph(graphShare);

        }
Example #32
0
        public void FunctionTest()
        {
            double sigma = 0.1;
            Gaussian target = new Gaussian(sigma);
            double[] x = { 2.0, 3.1, 4.0 };
            double[] y = { 2.0, 3.1, 4.0 };
            double expected = 1;
            double actual;

            actual = target.Function(x, y);
            Assert.AreEqual(expected, actual);

            actual = target.Function(x, x);
            Assert.AreEqual(expected, actual);

            actual = target.Function(y, y);
            Assert.AreEqual(expected, actual);
        }
Example #33
0
        public void GammaSigmaTest()
        {
            Gaussian gaussian = new Gaussian(1);
            double expected, actual, gamma, sigma;

            expected = 0.01;
            gaussian.Sigma = expected;
            gamma = gaussian.Gamma;

            gaussian.Gamma = gamma;
            actual = gaussian.Sigma;

            Assert.AreEqual(expected, actual);


            expected = 0.01;
            gaussian.Gamma = expected;
            sigma = gaussian.Sigma;

            gaussian.Sigma = sigma;
            actual = gaussian.Gamma;

            Assert.AreEqual(expected, actual, 1e-12);
        }
Example #34
0
        private void button3_Click(object sender, EventArgs e)
        {

            string[] Second = File.ReadAllLines(textBox14.Text);

            string[] First = File.ReadAllLines(textBox13.Text);


            List<double[]> F = new List<double[]>();
            List<double[]> S = new List<double[]>();
            double Alpha1Thresh = int.MaxValue; //2000;//  int.MaxValue;//
            double Alpha2Thresh = int.MaxValue; //2000; //
            for (int i=0;i<First.Length;i++)
            {
                string[] s1 = First[i].Split(' ');
                if ((double.Parse(s1[2]) < Alpha1Thresh) && (double.Parse(s1[3]) < Alpha2Thresh))
                {
                    double[] ar = new double[VectorSize];
                    double sum = 0;
                    for (int j = 0; j < VectorSize; j++)
                    {
                        ar[j] = double.Parse(s1[j]);
                        if ((j < VectorSize - 2) && (2<=j ))
                        sum += ar[j];
                    }
                    for (int j = 2; j < VectorSize-2; j++)
                        ar[j] = ar[j] / 1000;
                    if (ar[0] > 2000)
                    {
                        ar[0] = 2000;
                    }
                    if (ar[1] > 2000)
                    {
                        ar[1] = 2000;
                    }
                    ar[0] = ar[0] / 2000;
                    ar[1] = ar[1] / 2000;
                    ar[VectorSize - 2] = ar[VectorSize - 2] / 100;
                    ar[VectorSize - 1] = ar[VectorSize - 1] / 100;
                    F.Add(ar);
                }
            }
            for (int i = 0; i < Second.Length; i++)
            {
                string[] s1 = Second[i].Split(' ');
                if ((double.Parse(s1[2]) < Alpha1Thresh) && (double.Parse(s1[3]) < Alpha2Thresh))
                {
                    double[] ar = new double[VectorSize];
                    double sum = 0;
                    for (int j = 0; j < VectorSize; j++)
                    {
                        ar[j] = double.Parse(s1[j]);
                        if ((j < VectorSize - 2) && (2 <= j))
                            sum += ar[j];
                    }
                    for (int j = 2; j < VectorSize - 2; j++)
                        ar[j] = ar[j] / 1000;
                    if (ar[0]>2000)
                    {
                        ar[0] = 2000;
                    }
                    if (ar[1] > 2000)
                    {
                        ar[1] = 2000;
                    }
                    ar[0] = ar[0] / 2000;
                    ar[1] = ar[1] / 2000;
                    ar[VectorSize - 2] = ar[VectorSize - 2] / 100;
                    ar[VectorSize - 1] = ar[VectorSize - 1] / 100;
                    S.Add(ar);
                }
            }
            int min = Math.Min(F.Count, S.Count);
            double[][] inputs = new double[2*min][];
            int[] outputs = new int[2*min];

            int VS = VectorSize; //ТУТ

            for (int j=0;j<min;j++)
            {
                inputs[j] = new double[VS];
                inputs[j + min] = new double[VS];
                for (int i = 0; i < VS; i++)
              //  for (int i = VectorSize - 2; i < VectorSize; i++)//ТУТ
                {
                    inputs[j][i] = F[j][i];//ТУТ
                    inputs[j + min][i] = S[j][i];//ТУТ
               //     inputs[j][i - VectorSize + 2] = F[j][i];//ТУТ
                 //   inputs[j + min][i - VectorSize + 2] = S[j][i];//ТУТ
                }
                outputs[j] = -1;
                outputs[j + min] = 1;
            }

            // Get only the output labels (last column)
            


            // Create the specified Kernel
            IKernel kernel = new Gaussian((double)0.560);
         //   IKernel kernel = new Polynomial(5, 500.0);

            // Creates the Support Vector Machine for 2 input variables
            svm = new KernelSupportVectorMachine(kernel, inputs: VS);

            // Creates a new instance of the SMO learning algorithm
            var smo = new SequentialMinimalOptimization(svm, inputs, outputs)
            {
                // Set learning parameters
                Complexity = (double)1.50,
                Tolerance = (double)0.001,
                PositiveWeight = (double)1.00,
                NegativeWeight = (double)1.00,
            };


            try
            {
                // Run
                double error = smo.Run();

            }
            catch (ConvergenceException)
            {
                
            }
         //   double d = svm.Compute(inputs[10]);
            points.Clear();
            Points = 0;
            points_mid.Clear();
            timer3.Enabled = true;
        }
Example #35
0
 void IEstimable <Sparse <double> > .Estimate(Sparse <double>[] inputs)
 {
     this.Gamma = Gaussian.Estimate(inputs).Gamma;
 }