public void ComputeTest5()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            var kernel = new Polynomial(2, 1);

            Accord.Math.Tools.SetupGenerator(0);
            var projection = inputs.Apply(kernel.Transform);
            var machine    = new SupportVectorMachine(projection[0].Length);
            var smo        = new LinearCoordinateDescent(machine, projection, labels)
            {
                Complexity = 1000000,
                Tolerance  = 1e-15
            };

            double error = smo.Run();

            Assert.AreEqual(1000000.0, smo.Complexity, 1e-15);

            int[] actual = new int[labels.Length];
            for (int i = 0; i < actual.Length; i++)
            {
                actual[i] = Math.Sign(machine.Compute(projection[i]));
            }

            ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);

            Assert.AreEqual(6, matrix.FalseNegatives);
            Assert.AreEqual(7, matrix.FalsePositives);
            Assert.AreEqual(44, matrix.TruePositives);
            Assert.AreEqual(43, matrix.TrueNegatives);
        }
Beispiel #2
0
        public void RunTest2()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            var svm     = new SupportVectorMachine(inputs: 2);
            var teacher = new ProbabilisticCoordinateDescent(svm, inputs, labels);

            teacher.Tolerance  = 1e-10;
            teacher.Complexity = 1e+10;

            double error = teacher.Run();

            double[] weights = svm.ToWeights();

            Assert.AreEqual(0.11, error);
            Assert.AreEqual(3, weights.Length);
            Assert.AreEqual(-1.3231203367770932, weights[0], 1e-8);
            Assert.AreEqual(-3.0227742288788493, weights[1], 1e-8);
            Assert.AreEqual(-0.73074823290553259, weights[2], 1e-8);

            Assert.AreEqual(svm.Threshold, weights[0]);
        }
        public void RunTest()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            Accord.Math.Random.Generator.Seed = 0;

            var svm     = new SupportVectorMachine(inputs: 2);
            var teacher = new ProbabilisticDualCoordinateDescent(svm, inputs, labels);

            teacher.Tolerance = 1e-10;
            teacher.UseComplexityHeuristic = true;

            Assert.IsFalse(svm.IsProbabilistic);
            double error = teacher.Run();

            Assert.IsTrue(svm.IsProbabilistic);

            double[] weights = svm.ToWeights();

            Assert.AreEqual(0.13, error);
            Assert.AreEqual(3, weights.Length);
            Assert.AreEqual(-0.52913278486359605, weights[0], 1e-4);
            Assert.AreEqual(-1.6426069611746976, weights[1], 1e-4);
            Assert.AreEqual(-0.77766953652287762, weights[2], 1e-4);

            Assert.AreEqual(svm.Threshold, weights[0]);
        }
Beispiel #4
0
        public void ComputeTest5()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();
            var inputs  = dataset.Submatrix(null, 0, 1).ToJagged();
            var labels  = dataset.GetColumn(2).ToInt32();

            var kernel = new Polynomial(2, 0);

            {
                var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length);
                var smo     = new SequentialMinimalOptimization(machine, inputs, labels);
                smo.UseComplexityHeuristic = true;

                double error = smo.Run();
                Assert.AreEqual(0.2, error);

                Assert.AreEqual(0.11714451552090824, smo.Complexity);

                int[] actual = new int[labels.Length];
                for (int i = 0; i < actual.Length; i++)
                {
                    actual[i] = Math.Sign(machine.Compute(inputs[i]));
                }

                ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(20, matrix.FalseNegatives);
                Assert.AreEqual(0, matrix.FalsePositives);
                Assert.AreEqual(30, matrix.TruePositives);
                Assert.AreEqual(50, matrix.TrueNegatives);
            }

            {
                Accord.Math.Tools.SetupGenerator(0);

                var projection = inputs.Apply(kernel.Transform);
                var machine    = new SupportVectorMachine(projection[0].Length);
                var smo        = new LinearNewtonMethod(machine, projection, labels);
                smo.UseComplexityHeuristic = true;

                double error = smo.Run();
                Assert.AreEqual(0.18, error);

                Assert.AreEqual(0.11714451552090821, smo.Complexity, 1e-15);

                int[] actual = new int[labels.Length];
                for (int i = 0; i < actual.Length; i++)
                {
                    actual[i] = Math.Sign(machine.Compute(projection[i]));
                }

                ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(17, matrix.FalseNegatives);
                Assert.AreEqual(1, matrix.FalsePositives);
                Assert.AreEqual(33, matrix.TruePositives);
                Assert.AreEqual(49, matrix.TrueNegatives);
            }
        }
        public void multithread_test()
        {
            Accord.Math.Random.Generator.Seed = 0;

            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            var kernel     = new Polynomial(2, 1);
            var projection = kernel.Transform(inputs);

            var t    = new Thread[100];
            var svms = new SupportVectorMachine <Linear> [t.Length];

            for (int i = 0; i < t.Length; i++)
            {
                t[i] = new Thread((object obj) =>
                {
                    int j = (int)obj;

                    var teacher = new LinearCoordinateDescent <Linear>()
                    {
                        Complexity = 1000000,
                        Tolerance  = 1e-15
                    };

                    svms[j] = teacher.Learn(projection, labels);
                });

                t[i].Start(i);
            }

            for (int i = 0; i < t.Length; i++)
            {
                t[i].Join();
            }

            double[][] sv = svms[0].SupportVectors;
            double[]   w  = svms[0].Weights;
            for (int i = 0; i < svms.Length; i++)
            {
                Assert.IsTrue(sv.IsEqual(svms[i].SupportVectors));
                Assert.IsTrue(w.IsEqual(svms[i].Weights));

                int[] actual = svms[i].Decide(projection).ToMinusOnePlusOne();

                var matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(6, matrix.FalseNegatives);
                Assert.AreEqual(7, matrix.FalsePositives);
                Assert.AreEqual(44, matrix.TruePositives);
                Assert.AreEqual(43, matrix.TrueNegatives);
            }
        }
Beispiel #6
0
        public void KernelTest2()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();
            var inputs  = dataset.Submatrix(null, 0, 1).ToJagged();
            var labels  = dataset.GetColumn(2).ToInt32();

            var svm = new KernelSupportVectorMachine(new Linear(1), inputs: 2);

            var p = new ProbabilisticCoordinateDescent(svm, inputs, labels);

            Assert.NotNull(p);
        }
Beispiel #7
0
        public void KernelTest1()
        {
            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      labels = dataset.GetColumn(2).ToInt32();

            double e1, e2;

            double[] w1, w2;

            {
                Accord.Math.Random.Generator.Seed = 0;

                var svm     = new SupportVectorMachine(inputs: 2);
                var teacher = new ProbabilisticCoordinateDescent(svm, inputs, labels);

                teacher.Tolerance  = 1e-10;
                teacher.Complexity = 1e+10;

                e1 = teacher.Run();
                w1 = svm.ToWeights();
            }

            {
                Accord.Math.Random.Generator.Seed = 0;

                var svm     = new KernelSupportVectorMachine(new Linear(0), inputs: 2);
                var teacher = new ProbabilisticCoordinateDescent(svm, inputs, labels);

                teacher.Tolerance  = 1e-10;
                teacher.Complexity = 1e+10;

                e2 = teacher.Run();
                w2 = svm.ToWeights();
            }

            Assert.AreEqual(e1, e2);
            Assert.AreEqual(w1.Length, w2.Length);
            Assert.AreEqual(w1[0], w2[0], 1e-8);
            Assert.AreEqual(w1[1], w2[1], 1e-8);
            Assert.AreEqual(w1[2], w2[2], 1e-8);
        }
Beispiel #8
0
        public void different_lengths_same_error()
        {
            // GH-191: Different accuracy by specifying KernelSupportVectorMachine
            //         input length https://github.com/accord-net/framework/issues/191

            var dataset = SequentialMinimalOptimizationTest.GetYingYang();

            double[][] inputs  = dataset.Submatrix(null, 0, 1).ToJagged();
            int[]      outputs = dataset.GetColumn(2).ToInt32();

            var machine1 = new KernelSupportVectorMachine(new Linear(), inputs[0].Length);
            var teacher1 = new LinearDualCoordinateDescent(machine1, inputs, outputs);
            var error1   = teacher1.Run(true);

            var machine2 = new KernelSupportVectorMachine(new Linear(), 0);
            var teacher2 = new LinearDualCoordinateDescent(machine2, inputs, outputs);
            var error2   = teacher2.Run(true);

            Assert.AreEqual(error1, error2);
        }