Accord.Tests.MachineLearning.GridsearchTest.GridsearchConstructorTest2 C# (CSharp) Method

GridsearchConstructorTest2() private method

private GridsearchConstructorTest2 ( ) : void
return void
        public void GridsearchConstructorTest2()
        {
            Accord.Math.Random.Generator.Seed = 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, 1000000, 0.50 } ),
            };


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

            gridsearch.ParallelOptions.MaxDegreeOfParallelism = 1;

            // 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.
                double complexity = parameters["complexity"].Value;

                // Use the parameters to build the SVM model
                SupportVectorMachine svm = new SupportVectorMachine(2);

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

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

                return svm; // 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
                SupportVectorMachine bestModel = gridsearch.Compute(out bestParameters, out minError);


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


                Assert.IsNotNull(bestModel);
                Assert.IsNotNull(bestParameters);
                Assert.AreEqual(bestParameters.Count, 1);
            }

            {
                // Compute the grid search to find the best Support Vector Machine
                var result = gridsearch.Compute();


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

                Assert.IsNotNull(result.Model);
                Assert.AreEqual(5, result.Errors.Length);
                Assert.AreEqual(5, result.Models.Length);
            }
        }
    }