예제 #1
0
파일: SVM.cs 프로젝트: abaffa/INF1771
        public void Run()
        {
            // Example AND problem
            double[][] inputs =
            {
            new double[] { 0, 0 }, // 0 and 0: 0 (label -1)
            new double[] { 0, 1 }, // 0 and 1: 0 (label -1)
            new double[] { 1, 0 }, // 1 and 0: 0 (label -1)
            new double[] { 1, 1 }  // 1 and 1: 1 (label +1)
            };

            // Dichotomy SVM outputs should be given as [-1;+1]
            int[] labels =
            {
            // 0,  0,  0, 1
              -1, -1, -1, 1
            };

            // Create a Support Vector Machine for the given inputs
            SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length);

            // Instantiate a new learning algorithm for SVMs
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(machine, inputs, labels);

            // Set up the learning algorithm
            smo.Complexity = 1.0;

            // Run the learning algorithm
            double error = smo.Run();

            // Compute the decision output for one of the input vectors
            int decision = System.Math.Sign(machine.Compute(inputs[0]));
        }
예제 #2
0
        static KernelSupportVectorMachine LearnSVM(HSL[] positives, HSL[] negatives,
            double throwExceptionWhenErrorGreaterThan)
        {
            int[] classes = new int[positives.Length + negatives.Length];
            double[][] vectors = new double[classes.Length][];

            int index = 0;
            for (int c = 0; c < positives.Length; c++, index++)
            {
                classes[index] = 1;
                vectors[index] = HSLToDouble(positives[c]);
            }
            for (int c = 0; c < negatives.Length; c++, index++)
            {
                classes[index] = -1;
                vectors[index] = HSLToDouble(negatives[c]);
            }

            KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Gaussian(.1), vectors[0].Length);
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, vectors.ToArray(), classes);
            //smo.Complexity = 1.0;
            double error = smo.Run();
            if (error > throwExceptionWhenErrorGreaterThan)
            {
                throw new Exception("Failed to get reasonable error value.");
            }

            return svm;
        }
예제 #3
0
 public override Func<double[], double> Learn(LearningData learningData) {
     var svm = new KernelSupportVectorMachine(_kernel, learningData.Variables.Count);
     var smo = new SequentialMinimalOptimization(
             svm, learningData.Inputs, learningData.Outputs);
     smo.Run();
     return svm.Compute;
 }
        public void LearnTest()
        {

            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] xor =
            {
                -1,
                 1,
                 1,
                -1
            };

            // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree
            KernelSupportVectorMachine machine = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length);

            // Create the sequential minimal optimization teacher
            SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, xor);

            // Run the learning algorithm
            learn.Run();


            int[] output = inputs.Apply(p => Math.Sign(machine.Compute(p)));

            for (int i = 0; i < output.Length; i++)
                Assert.AreEqual(System.Math.Sign(xor[i]), System.Math.Sign(output[i]));
        }
예제 #5
0
        //public SupportVectorMachine SVM
        //{
        //    get { return svm; }
        //    private set { svm = value; }
        //}

        public override void TrainningModel(TrainningData trainningData)
        {
            ContinuousDataTableAdapter continuousDataTableAdapter = new ContinuousDataTableAdapter();

            DataTable continuousDataTable = continuousDataTableAdapter.GetData();
            DataTable dataTable = continuousDataTable.DefaultView.ToTable(false, TableMetaData.TestingAttributes);
            string[] columnNames;
            double[][] inputs = dataTable.ToArray(out columnNames);
            int[] outputs = (int[])trainningData.ClassificationAttribute.Clone();

            // Create output for SVM (-1 or 1)
            for (int index = 0; index < outputs.Length; index++)
            {
                if (outputs[index] == 0)
                {
                    outputs[index] = -1;
                }
            }

            // Create a Support Vector Machine for the given inputs
            //this.svm = new SupportVectorMachine(inputs[0].Length);

            //// Create a Kernel Support Vector Machine for the given inputs
            this.svm = new KernelSupportVectorMachine(new Gaussian(0.1), inputs[0].Length);

            // Instantiate a new learning algorithm for SVMs
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, outputs);

            // Set up the learning algorithm
            smo.Complexity = 1.0;

            // Run the learning algorithm 
            double error = smo.Run();
        }
        public double v3_0_1()
        {
            var ksvm = new KernelSupportVectorMachine(new Polynomial(2), 2);
            var smo = new SequentialMinimalOptimization(ksvm, inputs, outputs);

            return smo.Run(computeError: false);
        }
        public void ComputeTest()
        {
            // Example AND problem
            double[][] inputs =
            {
                new double[] { 0, 0 }, // 0 and 0: 0 (label -1)
                new double[] { 0, 1 }, // 0 and 1: 0 (label -1)
                new double[] { 1, 0 }, // 1 and 0: 0 (label -1)
                new double[] { 1, 1 }  // 1 and 1: 1 (label +1)
            };

            // Dichotomy SVM outputs should be given as [-1;+1]
            int[] labels =
            {
                // 0,  0,  0, 1
                  -1, -1, -1, 1
            };

            // Create a Support Vector Machine for the given inputs
            KernelSupportVectorMachine machine = new KernelSupportVectorMachine(new Gaussian(0.1), inputs[0].Length);

            // Instantiate a new learning algorithm for SVMs
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(machine, inputs, labels);

            // Set up the learning algorithm
            smo.Complexity = 1.0;

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

            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[0])));
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[1])));
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[2])));
            Assert.AreEqual(+1, Math.Sign(machine.Compute(inputs[3])));

            Assert.AreEqual(error, 0);

            Assert.AreEqual(-0.6640625, machine.Threshold);
            Assert.AreEqual(1, machine.Weights[0]);
            Assert.AreEqual(-0.34375, machine.Weights[1]);
            Assert.AreEqual(-0.328125, machine.Weights[2]);
            Assert.AreEqual(-0.328125, machine.Weights[3]);
        }
        public void TransformTest()
        {
            var inputs = yinyang.Submatrix(null, 0, 1).ToArray();
            var labels = yinyang.GetColumn(2).ToInt32();
            
            ConfusionMatrix actual, expected;
            SequentialMinimalOptimization a, b;

            var kernel = new Polynomial(2, 0);

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

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

                expected = new ConfusionMatrix(values, labels);
            }

            {
                var projection = inputs.Apply(kernel.Transform);
                var machine = new SupportVectorMachine(projection[0].Length);
                b = new SequentialMinimalOptimization(machine, projection, labels);
                b.UseComplexityHeuristic = true;
                b.Run();

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

                actual = new ConfusionMatrix(values, labels);
            }

            Assert.AreEqual(a.Complexity, b.Complexity, 1e-15);
            Assert.AreEqual(expected.TrueNegatives, actual.TrueNegatives);
            Assert.AreEqual(expected.TruePositives, actual.TruePositives);
            Assert.AreEqual(expected.FalseNegatives, actual.FalseNegatives);
            Assert.AreEqual(expected.FalsePositives, actual.FalsePositives);
        }
        public void TrainTest2()
        {

            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] or =
            {
                -1,
                -1,
                -1,
                +1
            };

            // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree
            SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length);

            // Create the sequential minimal optimization teacher
            SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, or);
            learn.Complexity = 1;

            // Run the learning algorithm
            learn.Run();


            double[] output = machine.Compute(inputs);

            for (int i = 0; i < output.Length; i++)
            {
                bool sor = or[i] >= 0;
                bool sou = output[i] >= 0;
                Assert.AreEqual(sor, sou);
            }


        }
        public void WeightRatioTest()
        {
            var dataset = KernelSupportVectorMachineTest.training;
            var inputs = dataset.Submatrix(null, 0, 3);
            var labels = Tools.Scale(0, 1, -1, 1, dataset.GetColumn(4)).ToInt32();

            Gaussian kernel = Gaussian.Estimate(inputs);

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

                smo.Complexity = 1.0;
                smo.WeightRatio = 10;

                double error = smo.Run();

                Assert.AreEqual(1.0, smo.PositiveWeight);
                Assert.AreEqual(0.1, smo.NegativeWeight);
                Assert.AreEqual(0.7142857142857143, error);
                Assert.AreEqual(265.78327637381551, (machine.Kernel as Gaussian).Sigma);
                Assert.AreEqual(39, machine.SupportVectors.Length);


                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(12, matrix.TruePositives); // has more importance
                Assert.AreEqual(0, matrix.FalseNegatives); // has more importance
                Assert.AreEqual(30, matrix.FalsePositives);
                Assert.AreEqual(0, matrix.TrueNegatives);

                Assert.AreEqual(1.0, matrix.Sensitivity);
                Assert.AreEqual(0.0, matrix.Specificity);

                Assert.AreEqual(0.44444444444444448, matrix.FScore);
                Assert.AreEqual(0.0, matrix.MatthewsCorrelationCoefficient);
            }

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

                smo.Complexity = 1.0;
                smo.WeightRatio = 0.1;

                double error = smo.Run();

                Assert.AreEqual(0.1, smo.PositiveWeight);
                Assert.AreEqual(1.0, smo.NegativeWeight);
                Assert.AreEqual(0.21428571428571427, error);
                Assert.AreEqual(265.78327637381551, (machine.Kernel as Gaussian).Sigma);
                Assert.AreEqual(18, machine.SupportVectors.Length);


                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(8, matrix.FalseNegatives);
                Assert.AreEqual(1, matrix.FalsePositives); // has more importance
                Assert.AreEqual(4, matrix.TruePositives);
                Assert.AreEqual(29, matrix.TrueNegatives); // has more importance

                Assert.AreEqual(0.33333333333333331, matrix.Sensitivity);
                Assert.AreEqual(0.96666666666666667, matrix.Specificity);

                Assert.AreEqual(0.47058823529411764, matrix.FScore);
                Assert.AreEqual(0.41849149947774944, matrix.MatthewsCorrelationCoefficient);
            }
        }
        public void FixedWeightsTest()
        {
            var dataset = KernelSupportVectorMachineTest.training;
            var inputs = dataset.Submatrix(null, 0, 3);
            var labels = Tools.Scale(0, 1, -1, 1, dataset.GetColumn(4)).ToInt32();

            KernelSupportVectorMachine machine = new KernelSupportVectorMachine(
                Gaussian.Estimate(inputs), inputs[0].Length);

            var smo = new SequentialMinimalOptimization(machine, inputs, labels);

            smo.Complexity = 10;

            double error = smo.Run();

            Assert.AreEqual(0.19047619047619047, error);
            Assert.AreEqual(265.78327637381551, (machine.Kernel as Gaussian).Sigma);
            Assert.AreEqual(29, machine.SupportVectors.Length);

            double[] expectedWeights =
            {
                1.65717694716503, 1.20005456611466, -5.70824245415995, 10,
                10, -2.38755497916487, 10, -8.15723436363058, 10, -10, 10,
                10, -0.188634936781317, -5.4354281009458, -8.48341139483265,
                -5.91105702760141, -5.71489190049223, 10, -2.37289205235858,
                -3.33031262413522, -1.97545116517677, 10, -10, -9.563186799279,
                -3.917941544845, -0.532584110773336, 4.81951847548326, 0.343668292727091,
                -4.34159482731336
            };

            Assert.IsTrue(expectedWeights.IsEqual(machine.Weights, 1e-6));

            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(8, matrix.FalseNegatives);
            Assert.AreEqual(0, matrix.FalsePositives);
            Assert.AreEqual(4, matrix.TruePositives);
            Assert.AreEqual(30, matrix.TrueNegatives);

            Assert.AreEqual(1 / 3.0, matrix.Sensitivity);
            Assert.AreEqual(1, matrix.Specificity);

            Assert.AreEqual(0.5, matrix.FScore);
            Assert.AreEqual(0.5129891760425771, matrix.MatthewsCorrelationCoefficient);
        }
        public void ReceiverOperatingCharacteristicConstructorTest3()
        {
            // This example shows how to measure the accuracy of a 
            // binary classifier using a ROC curve. For this example,
            // we will be creating a Support Vector Machine trained
            // on the following instances:

            double[][] inputs =
            {
                // Those are from class -1
                new double[] { 2, 4, 0 },
                new double[] { 5, 5, 1 },
                new double[] { 4, 5, 0 },
                new double[] { 2, 5, 5 },
                new double[] { 4, 5, 1 },
                new double[] { 4, 5, 0 },
                new double[] { 6, 2, 0 },
                new double[] { 4, 1, 0 },

                // Those are from class +1
                new double[] { 1, 4, 5 },
                new double[] { 7, 5, 1 },
                new double[] { 2, 6, 0 },
                new double[] { 7, 4, 7 },
                new double[] { 4, 5, 0 },
                new double[] { 6, 2, 9 },
                new double[] { 4, 1, 6 },
                new double[] { 7, 2, 9 },
            };

            int[] outputs =
            {
                -1, -1, -1, -1, -1, -1, -1, -1, // fist eight from class -1
                +1, +1, +1, +1, +1, +1, +1, +1  // last eight from class +1
            };

            // Create a linear Support Vector Machine with 4 inputs
            SupportVectorMachine machine = new SupportVectorMachine(inputs: 3);

            // Create the sequential minimal optimization teacher
            SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, outputs);

            // Run the learning algorithm
            double error = learn.Run();

            // Extract the input labels predicted by the machine
            double[] predicted = new double[inputs.Length];
            for (int i = 0; i < predicted.Length; i++)
                predicted[i] = machine.Compute(inputs[i]);


            // Create a new ROC curve to assess the performance of the model
            var roc = new ReceiverOperatingCharacteristic(outputs, predicted);

            roc.Compute(100); // Compute a ROC curve with 100 points
            /*
                        // Generate a connected scatter plot for the ROC curve and show it on-screen
                        ScatterplotBox.Show(roc.GetScatterplot(includeRandom: true), nonBlocking: true)

                            .SetSymbolSize(0)      // do not display data points
                            .SetLinesVisible(true) // show lines connecting points
                            .SetScaleTight(true)   // tighten the scale to points
                            .WaitForClose();
            */

            Assert.AreEqual(0.7890625, roc.Area);
            // Assert.AreEqual(0.1174774, roc.StandardError, 1e-6); HanleyMcNeil
            Assert.AreEqual(0.11958120746409709, roc.StandardError, 1e-6);
        }
예제 #13
0
        public void GridsearchConstructorTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Example binary data
            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] xor = // xor labels
            {
                -1, 1, 1, -1
            };

            // Declare the parameters and ranges to be searched
            GridSearchRange[] ranges = 
            {
                new GridSearchRange("complexity", new double[] { 0.00000001, 5.20, 0.30, 0.50 } ),
                new GridSearchRange("degree",     new double[] { 1, 10, 2, 3, 4, 5 } ),
                new GridSearchRange("constant",   new double[] { 0, 1, 2 } )
            };


            // Instantiate a new Grid Search algorithm for Kernel Support Vector Machines
            var gridsearch = new GridSearch<KernelSupportVectorMachine>(ranges);

            // Set the fitting function for the algorithm
            gridsearch.Fitting = delegate(GridSearchParameterCollection parameters, out double error)
            {
                // The parameters to be tried will be passed as a function parameter.
                int degree = (int)parameters["degree"].Value;
                double constant = parameters["constant"].Value;
                double complexity = parameters["complexity"].Value;

                // Use the parameters to build the SVM model
                Polynomial kernel = new Polynomial(degree, constant);
                KernelSupportVectorMachine ksvm = new KernelSupportVectorMachine(kernel, 2);

                // Create a new learning algorithm for SVMs
                SequentialMinimalOptimization smo = new SequentialMinimalOptimization(ksvm, inputs, xor);
                smo.Complexity = complexity;

                // Measure the model performance to return as an out parameter
                error = smo.Run();

                return ksvm; // Return the current model
            };


            // Declare some out variables to pass to the grid search algorithm
            GridSearchParameterCollection bestParameters; double minError;

            // Compute the grid search to find the best Support Vector Machine
            KernelSupportVectorMachine bestModel = gridsearch.Compute(out bestParameters, out minError);


            // A linear kernel can't solve the xor problem.
            Assert.AreNotEqual((int)bestParameters["degree"].Value, 1);

            // The minimum error should be zero because the problem is well-known.
            Assert.AreEqual(minError, 0.0);


            Assert.IsNotNull(bestModel);
            Assert.IsNotNull(bestParameters);
            Assert.AreEqual(bestParameters.Count, 3);
        }
        public void CompactTest()
        {

            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] xor =
            {
                -1,
                 1,
                 1,
                -1
            };

            {
                var machine = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length);
                var learn = new SequentialMinimalOptimization(machine, inputs, xor);

                learn.Compact = false;
                double error = learn.Run();
                Assert.AreEqual(0, error);
            }

            {
                var machine = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length);
                var learn = new SequentialMinimalOptimization(machine, inputs, xor);

                bool thrown = false;
                try { learn.Compact = true; }
                catch { thrown = true; }
                Assert.IsTrue(thrown);
            }

            {
                var machine = new KernelSupportVectorMachine(new Linear(), inputs[0].Length);
                var learn = new SequentialMinimalOptimization(machine, inputs, xor);

                learn.Compact = false;
                double error = learn.Run();
                Assert.AreEqual(0.5, error);
            }

            {
                var machine = new KernelSupportVectorMachine(new Linear(), inputs[0].Length);
                var learn = new SequentialMinimalOptimization(machine, inputs, xor);

                learn.Compact = false;
                double error = learn.Run();
                Assert.AreEqual(0.5, error);
            }
        }
        public void LearnTest5()
        {

            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] positives =
            {
                1,
                1,
                1,
                1
            };

            // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree
            SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length);

            // Create the sequential minimal optimization teacher
            SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, positives);
            learn.Complexity = 1;

            // Run the learning algorithm
            double error = learn.Run();

            Assert.AreEqual(0, error);


            int[] output = inputs.Apply(p => (int)machine.Compute(p));

            for (int i = 0; i < output.Length; i++)
            {
                bool sor = positives[i] >= 0;
                bool sou = output[i] >= 0;
                Assert.AreEqual(sor, sou);
            }
        }
        public void WeightsTest1()
        {
            var dataset = yinyang;
            double[][] inputs = dataset.Submatrix(null, 0, 1).ToArray();
            int[] labels = dataset.GetColumn(2).ToInt32();

            Accord.Math.Tools.SetupGenerator(0);

            var kernel = new Linear(1);

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

                smo.Complexity = 1.0;
                smo.PositiveWeight = 1;
                smo.NegativeWeight = 1;
                smo.Tolerance = 0.001;

                double error = smo.Run();

                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(43, matrix.TruePositives); // both classes are
                Assert.AreEqual(43, matrix.TrueNegatives); // well equilibrated
                Assert.AreEqual(7, matrix.FalseNegatives);
                Assert.AreEqual(7, matrix.FalsePositives);

                Assert.AreEqual(1.0, smo.Complexity);
                Assert.AreEqual(1.0, smo.WeightRatio);
                Assert.AreEqual(1.0, smo.NegativeWeight);
                Assert.AreEqual(1.0, smo.PositiveWeight);
                Assert.AreEqual(0.14, error);
                Assert.AreEqual(0.001, smo.Tolerance);
                Assert.AreEqual(31, machine.SupportVectors.Length);
            }

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

                smo.Complexity = 1;
                smo.PositiveWeight = 100;
                smo.NegativeWeight = 1;
                smo.Tolerance = 0.001;

                double error = smo.Run();

                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(50, matrix.TruePositives); // has more importance
                Assert.AreEqual(23, matrix.TrueNegatives);
                Assert.AreEqual(0, matrix.FalseNegatives); // has more importance
                Assert.AreEqual(27, matrix.FalsePositives);

                Assert.AreEqual(1.0, smo.Complexity);
                Assert.AreEqual(100, smo.WeightRatio);
                Assert.AreEqual(1.0, smo.NegativeWeight);
                Assert.AreEqual(100, smo.PositiveWeight);
                Assert.AreEqual(0.001, smo.Tolerance);
                Assert.AreEqual(0.27, error);
                Assert.AreEqual(41, machine.SupportVectors.Length);
            }

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

                smo.Complexity = 1;
                smo.PositiveWeight = 1;
                smo.NegativeWeight = 100;
                smo.Tolerance = 0.001;

                double error = smo.Run();

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

                var matrix = new ConfusionMatrix(actual, labels);

                Assert.AreEqual(25, matrix.TruePositives);
                Assert.AreEqual(50, matrix.TrueNegatives); // has more importance
                Assert.AreEqual(25, matrix.FalseNegatives);
                Assert.AreEqual(0, matrix.FalsePositives);  // has more importance

                Assert.AreEqual(1.0, smo.Complexity);
                Assert.AreEqual(0.01, smo.WeightRatio);
                Assert.AreEqual(100, smo.NegativeWeight);
                Assert.AreEqual(1.0, smo.PositiveWeight);
                Assert.AreEqual(0.25, error);
                Assert.AreEqual(0.001, smo.Tolerance);
                Assert.AreEqual(40, machine.SupportVectors.Length);
            }
        }
        private static void testWeights(double[][] inputs, int[] labels, IKernel kernel)
        {
            {
                var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length);
                var smo = new SequentialMinimalOptimization(machine, inputs, labels);

                smo.PositiveWeight = 100;
                smo.NegativeWeight = 1;

                double error = smo.Run();

                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(50, matrix.TruePositives); // has more importance
                Assert.AreEqual(0, matrix.FalseNegatives); // has more importance
            }

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

                smo.PositiveWeight = 1;
                smo.NegativeWeight = 100;

                double error = smo.Run();

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

                var matrix = new ConfusionMatrix(actual, labels);
                Assert.AreEqual(50, matrix.TrueNegatives); // has more importance
                Assert.AreEqual(0, matrix.FalsePositives);  // has more importance
            }
        }
예제 #18
0
        public void ComputeTest5()
        {
            var dataset = SequentialMinimalOptimizationTest.yinyang;
            var inputs = dataset.Submatrix(null, 0, 1).ToArray();
            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);
            }

        }
예제 #19
0
        private static void kernelSvm(double[][] inputs, int[] outputs)
        {
            // Estimate the kernel from the data
            var gaussian = Gaussian.Estimate(inputs);

            // Create a Gaussian binary support machine with 2 inputs
            var svm = new KernelSupportVectorMachine(gaussian, inputs: 2);

            // Create a new Sequential Minimal Optimization (SMO) learning 
            // algorithm and estimate the complexity parameter C from data
            var teacher = new SequentialMinimalOptimization(svm, inputs, outputs)
            {
                UseComplexityHeuristic = true
            };

            // Teach the vector machine
            double error = teacher.Run();

            // Classify the samples using the model
            int[] answers = inputs.Apply(svm.Compute).Apply(System.Math.Sign);

            // Plot the results
            ScatterplotBox.Show("Expected results", inputs, outputs);
            ScatterplotBox.Show("GaussianSVM results", inputs, answers);

            // Grab the index of multipliers higher than 0
            int[] idx = teacher.Lagrange.Find(x => x > 0);

            // Select the input vectors for those
            double[][] sv = inputs.Submatrix(idx);

            // Plot the support vectors selected by the machine
            ScatterplotBox.Show("Support vectors", sv).Hold();
        }
예제 #20
0
파일: MainForm.cs 프로젝트: qusma/framework
        /// <summary>
        ///   Creates a Support Vector Machine and teaches it to recognize
        ///   the previously loaded dataset using the current UI settings.
        /// </summary>
        /// 
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (dgvLearningSource.DataSource == null)
            {
                MessageBox.Show("Please load some data first.");
                return;
            }

            // Finishes and save any pending changes to the given data
            dgvLearningSource.EndEdit();



            // Creates a matrix from the entire source data table
            double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix(out columnNames);

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

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


            // Create the specified Kernel
            IKernel kernel = createKernel();


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

            // Creates a new instance of the SMO learning algorithm
            var smo = new SequentialMinimalOptimization(svm, inputs, outputs)
            {
                // Set learning parameters
                Complexity = (double)numC.Value,
                Tolerance = (double)numT.Value,
                PositiveWeight = (double)numPositiveWeight.Value,
                NegativeWeight = (double)numNegativeWeight.Value
            };


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

                lbStatus.Text = "Training complete!";
            }
            catch (ConvergenceException)
            {
                lbStatus.Text = "Convergence could not be attained. "+
                    "The learned machine might still be usable.";
            }


            // Check if we got support vectors
            if (svm.SupportVectors.Length == 0)
            {
                dgvSupportVectors.DataSource = null;
                graphSupportVectors.GraphPane.CurveList.Clear();
                return;
            }



            // Show support vectors on the Support Vectors tab page
            double[][] supportVectorsWeights = svm.SupportVectors.InsertColumn(svm.Weights);

            string[] supportVectorNames = columnNames.RemoveAt(columnNames.Length - 1).Concatenate("Weight");
            dgvSupportVectors.DataSource = new ArrayDataView(supportVectorsWeights, supportVectorNames);



            // Show the support vector labels on the scatter plot
            double[] supportVectorLabels = new double[svm.SupportVectors.Length];
            for (int i = 0; i < supportVectorLabels.Length; i++)
            {
                int j = inputs.Find(sv => sv == svm.SupportVectors[i])[0];
                supportVectorLabels[i] = outputs[j];
            }

            double[][] graph = svm.SupportVectors.InsertColumn(supportVectorLabels);

            CreateScatterplot(graphSupportVectors, graph.ToMatrix());



            // Get the ranges for each variable (X and Y)
            DoubleRange[] ranges = Matrix.Range(table, 0);

            // Generate a Cartesian coordinate system
            double[][] map = Matrix.CartesianProduct(
                Matrix.Interval(ranges[0], 0.05),
                Matrix.Interval(ranges[1], 0.05));

            // Classify each point in the Cartesian coordinate system
            double[] result = map.Apply(svm.Compute).Apply(Math.Sign).ToDouble();
            double[,] surface = map.ToMatrix().InsertColumn(result);

            CreateScatterplot(zedGraphControl2, surface);
        }
        public void ComputeTest()
        {
            // Example AND problem
            double[][] inputs =
            {
                new double[] { 0, 0 }, // 0 and 0: 0 (label -1)
                new double[] { 0, 1 }, // 0 and 1: 0 (label -1)
                new double[] { 1, 0 }, // 1 and 0: 0 (label -1)
                new double[] { 1, 1 }  // 1 and 1: 1 (label +1)
            };

            // Dichotomy SVM outputs should be given as [-1;+1]
            int[] labels =
            {
                // 0,  0,  0, 1
                  -1, -1, -1, 1
            };

            // Create a Support Vector Machine for the given inputs
            KernelSupportVectorMachine machine = new KernelSupportVectorMachine(new Linear(0), inputs[0].Length);

            // Instantiate a new learning algorithm for SVMs
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(machine, inputs, labels);

            // Set up the learning algorithm
            smo.Complexity = 100.0;

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

            Assert.AreEqual(0, error);
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[0])));
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[1])));
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[2])));
            Assert.AreEqual(+1, Math.Sign(machine.Compute(inputs[3])));

            // At this point we have the weighted support vectors
            //     w        sv        b
            //   (+4)  *  (1,1)      -3
            //   (-2)  *  (1,0)
            //   (-2)  *  (0,1)
            //
            // However, it can be seen that the last SV can be written
            // as a linear combination of the two first vectors:
            //
            //   (0,1) = (1,1) - (1,0)
            //
            // Since we have a linear space (we are using a linear kernel)
            // this vector could be removed from the support vector set.
            //
            // f(x) = sum(alpha_i * x * x_i) + b
            //      = 4*(1,1)*x - 2*(1,0)*x - 2*(0,1)*x             - 3
            //      = 4*(1,1)*x - 2*(1,0)*x - 2*((1,1) - (1,0))*x   - 3
            //      = 4*(1,1)*x - 2*(1,0)*x - 2*(1,1)*x + 2*(1,0)*x - 3
            //      = 4*(1,1)*x - 2*(1,0)*x - 2*(1,1)*x + 2*(1,0)*x - 3
            //      = 2*(1,1)*x - 3
            //      = 2*x1 + 2*x2 - 3
            //

            SupportVectorReduction svr = new SupportVectorReduction(machine);

            double error2 = svr.Run();


            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[0])));
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[1])));
            Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[2])));
            Assert.AreEqual(+1, Math.Sign(machine.Compute(inputs[3])));
        }
        public void ComputeTest5()
        {
            var dataset = yinyang;

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

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

                smo.Complexity = 1.0;

                double error = smo.Run();

                Assert.AreEqual(1.0, smo.Complexity);
                Assert.AreEqual(1.0, smo.WeightRatio);
                Assert.AreEqual(1.0, smo.NegativeWeight);
                Assert.AreEqual(1.0, smo.PositiveWeight);
                Assert.AreEqual(0.14, error);
                Assert.AreEqual(30, machine.SupportVectors.Length);

                double[] actualWeights = machine.Weights;
                double[] expectedWeights = { -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 0.337065120144639, -1, 1, -0.337065120144639, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1 };
                Assert.IsTrue(expectedWeights.IsEqual(actualWeights, 1e-10));

                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(7, matrix.FalseNegatives);
                Assert.AreEqual(7, matrix.FalsePositives);
                Assert.AreEqual(43, matrix.TruePositives);
                Assert.AreEqual(43, matrix.TrueNegatives);
            }

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

                smo.Complexity = 1.0;
                smo.PositiveWeight = 0.3;
                smo.NegativeWeight = 1.0;

                double error = smo.Run();

                Assert.AreEqual(1.0, smo.Complexity);
                Assert.AreEqual(0.3 / 1.0, smo.WeightRatio);
                Assert.AreEqual(1.0, smo.NegativeWeight);
                Assert.AreEqual(0.3, smo.PositiveWeight);
                Assert.AreEqual(0.21, error);
                Assert.AreEqual(24, machine.SupportVectors.Length);

                double[] actualWeights = machine.Weights;
                //string str = actualWeights.ToString(Accord.Math.Formats.CSharpArrayFormatProvider.InvariantCulture);
                double[] expectedWeights = { -0.771026323762095, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -0.928973676237905, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
                Assert.IsTrue(expectedWeights.IsEqual(actualWeights, 1e-10));

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

                ConfusionMatrix matrix = new ConfusionMatrix(actual, labels);

                Assert.AreEqual(50, matrix.FalseNegatives);
                Assert.AreEqual(0, matrix.FalsePositives);
                Assert.AreEqual(0, matrix.TruePositives);
                Assert.AreEqual(50, matrix.TrueNegatives);
            }

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

                smo.Complexity = 1.0;
                smo.PositiveWeight = 1.0;
                smo.NegativeWeight = 0.3;

                double error = smo.Run();

                Assert.AreEqual(1.0, smo.Complexity);
                Assert.AreEqual(1.0 / 0.3, smo.WeightRatio);
                Assert.AreEqual(0.3, smo.NegativeWeight);
                Assert.AreEqual(1.0, smo.PositiveWeight);
                Assert.AreEqual(0.15, error);
                Assert.AreEqual(19, machine.SupportVectors.Length);

                double[] actualWeights = machine.Weights;
                double[] expectedWeights = new double[] { 1, 1, -0.3, 1, -0.3, 1, 1, -0.3, 1, 1, 1, 1, 1, 1, 1, 1, 0.129080057278249, 1, 0.737797469918795 };
                Assert.IsTrue(expectedWeights.IsEqual(actualWeights, 1e-10));

                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(0, matrix.FalseNegatives);
                Assert.AreEqual(50, matrix.FalsePositives);
                Assert.AreEqual(50, matrix.TruePositives);
                Assert.AreEqual(0, matrix.TrueNegatives);
            }
        }
예제 #23
0
        public void BootstrapConstructorTest3()
        {

            Accord.Math.Tools.SetupGenerator(0);

            // This is a sample code on how to use 0.632 Bootstrap
            // to assess the performance of Support Vector Machines.

            // Consider the example binary data. We will be trying
            // to learn a XOR problem and see how well does SVMs
            // perform on this data.

            double[][] data =
            {
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
            };

            int[] xor = // result of xor for the sample input data
            {
                -1,       1,
                 1,      -1,
                -1,       1,
                 1,      -1,
                -1,       1,
                 1,      -1,
                -1,       1,
                 1,      -1,
            };


            // Create a new Bootstrap algorithm passing the set size and the number of resamplings
            var bootstrap = new Bootstrap(size: data.Length, subsamples: 50);

            // Define a fitting function using Support Vector Machines. The objective of this
            // function is to learn a SVM in the subset of the data indicated by the bootstrap.

            bootstrap.Fitting = delegate(int[] indicesTrain, int[] indicesValidation)
            {
                // The fitting function is passing the indices of the original set which
                // should be considered training data and the indices of the original set
                // which should be considered validation data.

                // Lets now grab the training data:
                var trainingInputs = data.Submatrix(indicesTrain);
                var trainingOutputs = xor.Submatrix(indicesTrain);

                // And now the validation data:
                var validationInputs = data.Submatrix(indicesValidation);
                var validationOutputs = xor.Submatrix(indicesValidation);


                // Create a Kernel Support Vector Machine to operate on the set
                var svm = new KernelSupportVectorMachine(new Polynomial(2), 2);

                // Create a training algorithm and learn the training data
                var smo = new SequentialMinimalOptimization(svm, trainingInputs, trainingOutputs);

                double trainingError = smo.Run();

                // Now we can compute the validation error on the validation data:
                double validationError = smo.ComputeError(validationInputs, validationOutputs);

                // Return a new information structure containing the model and the errors achieved.
                return new BootstrapValues(trainingError, validationError);
            };


            // Compute the bootstrap estimate
            var result = bootstrap.Compute();

            // Finally, access the measured performance.
            double trainingErrors = result.Training.Mean;
            double validationErrors = result.Validation.Mean;

            // And compute the 0.632 estimate
            double estimate = result.Estimate;

            Assert.AreEqual(50, bootstrap.B);
            Assert.AreEqual(0, trainingErrors);
            Assert.AreEqual(0.021428571428571429, validationErrors);

            Assert.AreEqual(50, bootstrap.Subsamples.Length);
            Assert.AreEqual(0.013542857142857143, estimate);
        }
예제 #24
0
        public void DynamicalTimeWarpingConstructorTest()
        {
            double[][] sequences = 
            {
                new double[] // -1
                {
                    0, 0, 0,
                    1, 1, 1,
                    2, 2, 2,
                },

                new double[] // -1
                {
                     0, 1, 0,
                     0, 2, 0,
                     0, 3, 0
                },

                new double[] // +1
                {
                     1, 1, 0,
                     1, 2, 0,
                     2, 1, 0,
                },

                new double[] // +1
                {
                     0, 0, 1, 
                     0, 0, 2, 
                     0, 1, 3,
                },
            };

            int[] outputs = { -1, -1, +1, +1 };


            // Set the parameters of the kernel
            double alpha = 0.85;
            int innerVectorLength = 3;


            // Create the kernel. Note that the input vector will be given out automatically
            DynamicTimeWarping target = new DynamicTimeWarping(innerVectorLength, alpha);



            // When using variable-length kernels, specify 0 as the input length.
            KernelSupportVectorMachine svm = new KernelSupportVectorMachine(target, 0);

            // Create the Sequential Minimal Optimization as usual
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, sequences, outputs);
            smo.Complexity = 1.5;
            double error = smo.Run();


            // Computing the training values
            var a0 = svm.Compute(sequences[0]);
            var a1 = svm.Compute(sequences[1]);
            var a2 = svm.Compute(sequences[2]);
            var a3 = svm.Compute(sequences[3]);

            Assert.AreEqual(-1, System.Math.Sign(a0));
            Assert.AreEqual(-1, System.Math.Sign(a1));
            Assert.AreEqual(+1, System.Math.Sign(a2));
            Assert.AreEqual(+1, System.Math.Sign(a3));



            // Computing a new testing value
            double[] test =
                {
                     1, 0, 1,
                     0, 0, 2,
                     0, 1, 3,
                };

            var a4 = svm.Compute(test);

        }
        public void LargeLearningTest1()
        {
            // Create large input vectors

            int rows = 1000;
            int dimension = 10000;

            double[][] inputs = new double[rows][];
            int[] outputs = new int[rows];

            Random rnd = new Random();

            for (int i = 0; i < inputs.Length; i++)
            {
                inputs[i] = new double[dimension];

                if (i > rows / 2)
                {
                    for (int j = 0; j < dimension; j++)
                        inputs[i][j] = rnd.NextDouble();
                    outputs[i] = -1;
                }
                else
                {
                    for (int j = 0; j < dimension; j++)
                        inputs[i][j] = rnd.NextDouble() * 4.21 + 5;
                    outputs[i] = +1;
                }
            }

            KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Polynomial(2), dimension);

            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, outputs)
            {
                UseComplexityHeuristic = true
            };


            double error = smo.Run();

            Assert.AreEqual(0, error);
         }
예제 #26
0
        public void SplitSetConstructorTest1()
        {

            Accord.Math.Tools.SetupGenerator(0);

            // This is a sample code on how to use two split sets
            // to assess the performance of Support Vector Machines.

            // Consider the example binary data. We will be trying
            // to learn a XOR problem and see how well does SVMs
            // perform on this data.

            double[][] data =
            {
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
                new double[] { -1, -1 }, new double[] {  1, -1 },
                new double[] { -1,  1 }, new double[] {  1,  1 },
            };

            int[] xor = // result of xor for the sample input data
            {
                -1,       1,
                 1,      -1,
                -1,       1,
                 1,      -1,
                -1,       1,
                 1,      -1,
                -1,       1,
                 1,      -1,
            };


            // Create a new split set validation algorithm passing the set size and the split set proportion
            var splitset = new SplitSetValidation<KernelSupportVectorMachine>(size: data.Length, proportion: 0.4);

            // Define a fitting function using Support Vector Machines. The objective of this
            // function is to learn a SVM in the subset of the data indicated by the split sets.

            splitset.Fitting = delegate(int[] indicesTrain)
            {
                // The fitting function is passing the indices of the original set which
                // should be considered training data and the indices of the original set
                // which should be considered validation data.

                // Lets now grab the training data:
                var trainingInputs = data.Submatrix(indicesTrain);
                var trainingOutputs = xor.Submatrix(indicesTrain);

                // Create a Kernel Support Vector Machine to operate on the set
                var svm = new KernelSupportVectorMachine(new Polynomial(2), 2);

                // Create a training algorithm and learn the training data
                var smo = new SequentialMinimalOptimization(svm, trainingInputs, trainingOutputs);

                double trainingError = smo.Run();

                // Compute results for the training set
                int[] computedOutputs = trainingInputs.Apply(svm.Compute).Apply(Math.Sign);

                // Compute the absolute error
                int[] errors = (computedOutputs.Subtract(trainingOutputs)).Abs();

                // Retrieve error statistics
                double mean = errors.Mean();
                double variance = errors.Variance();

                // Return a new information structure containing the model and the errors.
                return SplitSetStatistics.Create(svm, trainingInputs.Length, mean, variance);

            };

            splitset.Evaluation = delegate(int[] indicesValidation, KernelSupportVectorMachine svm)
            {
                // Lets now grab the training data:
                var validationInputs = data.Submatrix(indicesValidation);
                var validationOutputs = xor.Submatrix(indicesValidation);

                // Compute results for the validation set
                int[] computedOutputs = validationInputs.Apply(svm.Compute).Apply(Math.Sign);

                // Compute the absolute error
                int[] errors = (computedOutputs.Subtract(validationOutputs)).Abs();

                // Retrieve error statistics
                double mean = errors.Mean();
                double variance = errors.Variance();

                // Return a new information structure containing the model and the errors.
                return SplitSetStatistics.Create(svm, validationInputs.Length, mean, variance);
            };


            // Compute the bootstrap estimate
            var result = splitset.Compute();

            // Finally, access the measured performance.
            double trainingErrors = result.Training.Value;
            double validationErrors = result.Validation.Value;

            Assert.AreEqual(0, trainingErrors);
            Assert.AreEqual(0, validationErrors);
        }
예제 #27
0
        public void DynamicalTimeWarpingConstructorTest2()
        {
            // Declare some testing data
            double[][] inputs =
            {
                // Class -1
                new double[] { 0,1,1,0 },
                new double[] { 0,0,1,0 },  
                new double[] { 0,1,1,1,0 }, 
                new double[] { 0,1,0 },    

                // Class +1
                new double[] { 1,0,0,1 },   
                new double[] { 1,1,0,1 }, 
                new double[] { 1,0,0,0,1 },
                new double[] { 1,0,1 },   
                new double[] { 1,0,0,0,1,1 } 
            };

            int[] outputs =
            {
                -1,-1,-1,-1,  // First four sequences are of class -1
                 1, 1, 1, 1, 1 // Last five sequences are of class +1
            };


            // Set the parameters of the kernel
            double alpha = 1.0;
            int degree = 1;
            int innerVectorLength = 1;

            // Create the kernel. Note that the input vector will be given out automatically
            DynamicTimeWarping target = new DynamicTimeWarping(innerVectorLength, alpha, degree);


            // When using variable-length kernels, specify 0 as the input length.
            KernelSupportVectorMachine svm = new KernelSupportVectorMachine(target, 0);

            // Create the Sequential Minimal Optimization as usual
            SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, outputs);
            smo.Complexity = 1.5;
            double error = smo.Run();


            // Check if the model has learnt the sequences correctly.
            for (int i = 0; i < inputs.Length; i++)
            {
                int expected = outputs[i];
                int actual = System.Math.Sign(svm.Compute(inputs[i]));
                Assert.AreEqual(expected, actual);
            }

            // Testing new sequences
            Assert.AreEqual(-1,System.Math.Sign(svm.Compute(new double[] { 0, 1, 1, 0, 0 })));
            Assert.AreEqual(+1,System.Math.Sign(svm.Compute(new double[] { 1, 1, 0, 0, 1, 1 })));
        }
        public void ComplexityHeuristicTest()
        {
            var dataset = yinyang;

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

            var linear = new SupportVectorMachine(inputs[0].Length);

            Linear kernel = new Linear(0);
            var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length);

            var smo1 = new SequentialMinimalOptimization(machine, inputs, labels);
            smo1.UseClassProportions = true;
            smo1.UseComplexityHeuristic = true;
            double e1 = smo1.Run();

            var smo2 = new SequentialMinimalOptimization(linear, inputs, labels);
            smo2.UseClassProportions = true;
            smo2.UseComplexityHeuristic = true;
            double e2 = smo2.Run();

            Assert.AreEqual(smo1.Complexity, smo2.Complexity);
            Assert.AreEqual(e1, e2);
        }
예제 #29
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;
        }
        public void UseClassProportionsTest()
        {
            var dataset = KernelSupportVectorMachineTest.training;
            var inputs = dataset.Submatrix(null, 0, 3);
            var labels = Tools.Scale(0, 1, -1, 1, dataset.GetColumn(4)).ToInt32();

            Gaussian kernel = Gaussian.Estimate(inputs);
            var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length);
            var smo = new SequentialMinimalOptimization(machine, inputs, labels);

            smo.Complexity = 1.0;
            smo.UseClassProportions = true;

            double error = smo.Run();

            Assert.AreEqual(1, smo.Complexity);
            Assert.AreEqual(0.4, smo.PositiveWeight);
            Assert.AreEqual(1.0, smo.NegativeWeight);
            Assert.AreEqual(0.4, smo.WeightRatio, 1e-10);
            Assert.AreEqual(0.2857142857142857, error);
            Assert.AreEqual(265.78327637381551, (machine.Kernel as Gaussian).Sigma);
            Assert.AreEqual(26, machine.SupportVectors.Length);


            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(12, matrix.FalseNegatives);
            Assert.AreEqual(0, matrix.FalsePositives);
            Assert.AreEqual(0, matrix.TruePositives);
            Assert.AreEqual(30, matrix.TrueNegatives);
        }