示例#1
0
        public static void testSVMWrapperPackage() //rafal parsal
        {
            libSVM_Problem Problem = libSVM_Problem.Load("../../../LibSVMFull/testSvmWrapper/data/train.dat");

            GuiPreferences.Instance.setLog("trainnig data loaded");
            Problem.Save("../../../LibSVMFull/testSvmWrapper/data/train_saved");
            GuiPreferences.Instance.setLog("training data saved");
            libSVM_Extension svm       = new libSVM_Extension();
            libSVM_Parameter Parameter = new libSVM_Parameter();

            Parameter.svm_type    = SVM_TYPE.C_SVC;
            Parameter.kernel_type = KERNEL_TYPE.LINEAR;

            svm = new libSVM_Extension();
            svm.Train(Problem, Parameter);
            libSVM_Problem Test = libSVM_Problem.Load("../../../LibSVMFull/testSvmWrapper/data/test.dat");

            svm.GetAccuracyFromTest(Test);
            double accuracy = Preferences.Instance.svmWrapper.output.accuracy;

            GuiPreferences.Instance.setLog("Predicted accuracy from testing set: " + accuracy.ToString());
            svm.Dispose();


            svm = new libSVM_Extension();
            //svm.TrainAuto(10, Problem, Parameter, libSVM_Grid.C(), libSVM_Grid.gamma(), libSVM_Grid.p(), libSVM_Grid.nu(), libSVM_Grid.coef0(), libSVM_Grid.degree());
            libSVM_Grid grid = new libSVM_Grid();

            accuracy = svm.TrainAuto(10, Problem, Parameter, grid, null, null, null, null, null);
            GuiPreferences.Instance.setLog("Predicted accuracy from 10 cross fold validation: " + accuracy.ToString());
            svm.Save("../../../LibSVMFull/testSvmWrapper/data/model_file");
            GuiPreferences.Instance.setLog("10 cfv best model saved");
            svm.Dispose();
        }
示例#2
0
        /// <summary>
        /// defauld grid for c
        /// </summary>
        /// <returns>defauld grid for c</returns>
        public static libSVM_Grid C()
        {
            libSVM_Grid grid = new libSVM_Grid();

            grid.min  = 0.1;
            grid.max  = 500;
            grid.step = 5;

            return(grid);
        }
示例#3
0
        /// <summary>
        /// default grid for degree
        /// </summary>
        /// <returns>defauld grid for degree</returns>
        public static libSVM_Grid degree()
        {
            libSVM_Grid grid = new libSVM_Grid();

            grid.min  = 0.01;
            grid.max  = 4;
            grid.step = 7;

            return(grid);
        }
示例#4
0
        /// <summary>
        /// default grid for coef
        /// </summary>
        /// <returns>defauld grid for coef</returns>
        public static libSVM_Grid coef()
        {
            libSVM_Grid grid = new libSVM_Grid();

            grid.min  = 0.1;
            grid.max  = 300;
            grid.step = 14;

            return(grid);
        }
示例#5
0
        /// <summary>
        /// defauld grid for nu
        /// </summary>
        /// <returns>defauld grid for nu</returns>
        public static libSVM_Grid nu()
        {
            libSVM_Grid grid = new libSVM_Grid();

            grid.min  = 0.01;
            grid.max  = 0.2;
            grid.step = 3;

            return(grid);
        }
示例#6
0
        /// <summary>
        /// defauld grid for p
        /// </summary>
        /// <returns>defauld grid for p</returns>
        public static libSVM_Grid p()
        {
            libSVM_Grid grid = new libSVM_Grid();

            grid.min  = 0.001;
            grid.max  = 100;
            grid.step = 7;

            return(grid);
        }
示例#7
0
        /// <summary>
        /// defauld grid for gamma
        /// </summary>
        /// <returns>defauld grid for gamma</returns>
        public static libSVM_Grid gamma()
        {
            libSVM_Grid grid = new libSVM_Grid();

            grid.min  = 1e-5;
            grid.max  = 0.6;
            grid.step = 15;

            return(grid);
        }
示例#8
0
        /// <summary>
        /// Trains model automatically using users grids. if your svm_type/kernel_type does not need it then set it to null
        /// </summary>
        /// <param name="fold">folds for samples >=2 </param>
        /// <param name="problem">samples and labels</param>
        /// <param name="parameter">model parameters</param>
        /// <param name="grid_c">grid for C</param>
        /// <param name="grid_gamma">grid for gamma</param>
        /// <param name="grid_p">grid for p</param>
        /// <param name="grid_nu">grid for nu</param>
        /// <param name="grid_coef0">grid for coef</param>
        /// <param name="grid_degree">grid for degree</param>
        /// <returns>Total Accuracy in %</returns>
        public void TrainAutoBestTotal(int fold, libSVM_Problem problem, libSVM_Parameter parameter, libSVM_Grid grid_c, libSVM_Grid grid_gamma, libSVM_Grid grid_p, libSVM_Grid grid_nu, libSVM_Grid grid_coef0, libSVM_Grid grid_degree)
        {
            Dispose();

            if (problem == null)
            {
                throw new Exception("libSVMProblem not initialized");
            }
            if (problem.samples == null)
            {
                throw new Exception("libSVMProblem.samples = null");
            }
            if (problem.labels == null)
            {
                throw new Exception("libSVMProblem.labels = null");
            }
            if (problem.samples.Length != problem.labels.Length)
            {
                throw new Exception("libSVMProblem.samples.Length != libSVMProblem.labels.length");
            }
            if (parameter == null)
            {
                throw new Exception("libSVMParameter not initialized");
            }
            if (parameter.weight == null && parameter.weight_label != null)
            {
                throw new Exception("libSVMParameter.weight = null and libSVMParameter.weight_label != null");
            }
            if (parameter.weight != null && parameter.weight_label == null)
            {
                throw new Exception("libSVMParameter.weight_label = null and libSVMParameter.weight != null");
            }
            if (parameter.weight != null && parameter.weight_label != null && parameter.weight_label.Length != parameter.weight.Length)
            {
                throw new Exception("libSVMParameter.weight_label.Length != libSVMParameter.weight.Length");
            }
            if (fold < 2 || fold > problem.samples.Length)
            {
                throw new Exception("fold < 2 || fold > nr_samples");
            }

            libSVM_Grid _grid_gamma  = grid_gamma;
            libSVM_Grid _grid_coef0  = grid_coef0;
            libSVM_Grid _grid_degree = grid_degree;

            if (parameter.kernel_type == KERNEL_TYPE.LINEAR ||
                parameter.kernel_type == KERNEL_TYPE.PRECOMPUTED)
            {
                _grid_gamma  = new libSVM_Grid();
                _grid_coef0  = new libSVM_Grid();
                _grid_degree = new libSVM_Grid();
            }
            else if (parameter.kernel_type == KERNEL_TYPE.POLY)
            {
                if (_grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (_grid_coef0 == null)
                {
                    throw new Exception("grid_coef0 not set");
                }
                if (_grid_degree == null)
                {
                    throw new Exception("grid_degree not set");
                }
            }
            else if (parameter.kernel_type == KERNEL_TYPE.RBF)
            {
                if (_grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }

                _grid_coef0  = new libSVM_Grid();
                _grid_degree = new libSVM_Grid();
            }
            else if (parameter.kernel_type == KERNEL_TYPE.SIGMOID)
            {
                if (_grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (_grid_coef0 == null)
                {
                    throw new Exception("grid_coef not set");
                }
                _grid_degree = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown kernel_type");
            }

            libSVM_Grid _grid_c  = grid_c;
            libSVM_Grid _grid_p  = grid_p;
            libSVM_Grid _grid_nu = grid_nu;

            if (parameter.svm_type == SVM_TYPE.C_SVC ||
                parameter.svm_type == SVM_TYPE.ONE_CLASS)
            {
                if (_grid_c == null)
                {
                    throw new Exception("grid_C not set");
                }

                _grid_nu = new libSVM_Grid();
                _grid_p  = new libSVM_Grid();
            }
            else if (parameter.svm_type == SVM_TYPE.NU_SVC || parameter.svm_type == SVM_TYPE.NU_SVR)
            {
                if (_grid_nu == null)
                {
                    throw new Exception("grid_nu not set");
                }

                _grid_c = new libSVM_Grid();
                _grid_p = new libSVM_Grid();
            }
            else if (parameter.svm_type == SVM_TYPE.EPSILON_SVR)
            {
                if (_grid_p == null)
                {
                    throw new Exception("grid_p not set");
                }

                _grid_c  = new libSVM_Grid();
                _grid_nu = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown svm_type");
            }

            double c  = _grid_c.min;
            double p  = _grid_p.min;
            double nu = _grid_nu.min;

            double gamma  = _grid_gamma.min;
            double coef0  = _grid_coef0.min;
            double degree = _grid_degree.min;
            int    f      = 0;

            int test_probe   = 0;
            int test_samples = 0;
            //ori
            int total_test_probe             = 0;
            int total_test_samples           = 0;
            libSVM_Parameter train_parameter = new libSVM_Parameter();

            //copy parameters set by user;
            train_parameter.weight       = parameter.weight;
            train_parameter.weight_label = parameter.weight_label;
            train_parameter.shrinking    = parameter.shrinking;
            train_parameter.svm_type     = parameter.svm_type;
            train_parameter.kernel_type  = parameter.kernel_type;
            train_parameter.probability  = parameter.probability;
            train_parameter.cache_size   = parameter.cache_size;

            libSVM_Problem train_problem = new libSVM_Problem();
            libSVM_Problem test_problem  = new libSVM_Problem();

            //ori
            libSVM_output this_output = new libSVM_output();

            //fold
            do
            {
                train_problem.labels  = new double[1];
                train_problem.samples = new SortedDictionary <int, double> [1];

                test_problem.labels  = new double[1];
                test_problem.samples = new SortedDictionary <int, double> [1];

                int j = 0;
                int k = 0;
                for (int i = 0; i < problem.samples.Length; i++)
                {
                    if ((i + 1 + f) % fold == 0)
                    {
                        Array.Resize(ref test_problem.labels, j + 1);
                        Array.Resize(ref test_problem.samples, j + 1);
                        this_output.setValue(i, 999);

                        test_problem.labels[j]  = problem.labels[i];
                        test_problem.samples[j] = problem.samples[i];
                        j++;
                    }
                    else
                    {
                        Array.Resize(ref train_problem.labels, k + 1);
                        Array.Resize(ref train_problem.samples, k + 1);

                        train_problem.labels[k]  = problem.labels[i];
                        train_problem.samples[k] = problem.samples[i];
                        k++;
                    }
                }

                //p
                p = _grid_p.min;
                do
                {
                    //nu
                    nu = _grid_nu.min;
                    do
                    {
                        //gamma
                        gamma = _grid_gamma.min;
                        do
                        {
                            //coef0
                            coef0 = _grid_coef0.min;
                            do
                            {
                                //degree

                                degree = _grid_degree.min;
                                do
                                {
                                    //c

                                    c = _grid_c.min;
                                    do
                                    {
                                        //svm_train alters problem_ptr so it's necessary to create it every time;
                                        IntPtr this_problem_ptr = libSVM_Problem_to_svm_problem_ptr(train_problem);

                                        //set generated parameters
                                        train_parameter.C      = c;
                                        train_parameter.p      = p;
                                        train_parameter.nu     = nu;
                                        train_parameter.gamma  = gamma;
                                        train_parameter.degree = (int)degree;
                                        train_parameter.coef0  = coef0;

                                        IntPtr this_parameter_ptr = libSVM_Parameter_to_svm_parameter_ptr(train_parameter);

                                        IntPtr error_ptr = svm_check_parameter(this_problem_ptr, this_parameter_ptr);

                                        if (error_ptr != IntPtr.Zero)
                                        {
                                            throw new Exception(ptr_to_string(error_ptr));
                                        }

                                        IntPtr this_model_ptr = svm_train(this_problem_ptr, this_parameter_ptr);

                                        int this_test_probe = 0;

                                        //count propperly recognized test_problem.samples
                                        for (int i = 0; i < test_problem.labels.Length; i++)
                                        {
                                            IntPtr svm_nodes_ptr = sample_to_svm_nodes_ptr(test_problem.samples[i]);

                                            //ori

                                            double testPrediction = svm_predict(this_model_ptr, svm_nodes_ptr);

                                            this_output.setValue(this_output.getKeyByIndex(i), testPrediction);
                                            total_test_samples++; // can also be problem.labels.length
                                            if (test_problem.labels[i] == testPrediction)
                                            {
                                                this_test_probe++;
                                                total_test_probe++;
                                            }

                                            Free_ptr(ref svm_nodes_ptr);
                                        }

                                        //if first run then just copy this
                                        if (_model_ptr == IntPtr.Zero)
                                        {
                                            _model_ptr     = this_model_ptr;
                                            _parameter_ptr = this_parameter_ptr;
                                            _problem_ptr   = this_problem_ptr;

                                            test_probe   = this_test_probe;
                                            test_samples = test_problem.samples.Length;

                                            //ori
                                            output = this_output;

                                            //!-uncommenting this gets mapping of predicted labels only for best model
                                            this_output = new libSVM_output();

                                            //!-uncommenting this gets only best model and not runs on all 10 folds (unless best is 10th)
                                            //exit when no better solution could be found
                                            if (test_probe == test_problem.samples.Length)
                                            {
                                                goto leave;
                                            }
                                        }
                                        else
                                        //if model was better than previous then free previous data and copy this
                                        if (this_test_probe > test_probe)
                                        {
                                            Free_svm_parameter_ptr(ref _parameter_ptr);
                                            Free_svm_problem_ptr(ref _problem_ptr);
                                            svm_free_and_destroy_model(ref _model_ptr);

                                            _parameter_ptr = this_parameter_ptr;
                                            _problem_ptr   = this_problem_ptr;
                                            _model_ptr     = this_model_ptr;

                                            test_probe   = this_test_probe;
                                            test_samples = test_problem.samples.Length;

                                            //ori
                                            output = this_output;

                                            //!-uncommenting this gets mapping of predicted labels only for best model
                                            this_output = new libSVM_output();

                                            //!-uncommenting this gets only best model and not runs on all 10 folds (unless best is 10th)
                                            //exit when no better solution could be found;
                                            if (test_probe == test_problem.samples.Length)
                                            {
                                                goto leave;
                                            }
                                        }
                                        //if not then free this model
                                        else
                                        {
                                            Free_svm_problem_ptr(ref this_problem_ptr);
                                            Free_svm_parameter_ptr(ref this_parameter_ptr);
                                            svm_free_and_destroy_model(ref this_model_ptr);

                                            //!-uncommenting this gets mapping of predicted labels only for best model
                                            this_output = new libSVM_output();
                                        }

                                        c *= _grid_c.step;
                                    } while (c < _grid_c.max);

                                    degree *= _grid_degree.step;
                                } while (degree < _grid_degree.max);

                                coef0 *= _grid_coef0.step;
                            } while (coef0 < _grid_coef0.max);

                            gamma *= _grid_gamma.step;
                        } while (gamma < _grid_gamma.max);

                        nu *= _grid_nu.step;
                    } while (nu < _grid_nu.max);

                    p *= _grid_p.step;
                } while (p < _grid_p.max);

                f++;
            } while (f < fold);

leave:
            //return new double []{100.0 * test_probe / (double) test_samples,100.0 * total_test_probe / (double)total_test_samples};
            output.accuracy = 100.0 * test_probe / (double)test_samples;
            return;
        }
示例#9
0
        public void TrainFolds(int N, libSVM_ExtendedProblem problem, trainingType type)
        {
            folds = N;
            Preferences.Instance.svmWrapper = new libSVM_Extension();
            //svm.TrainAuto(10, Problem, Parameter, libSVM_Grid.C(), libSVM_Grid.gamma(), libSVM_Grid.p(), libSVM_Grid.nu(), libSVM_Grid.coef0(), libSVM_Grid.degree());


            //used without grids. only folds
            libSVM_Grid grid = new libSVM_Grid();

            GuiPreferences.Instance.setLog("Training + Cross Validation Started");
            double accuracy = -1;

            if (type == trainingType.best)
            {
                accuracy = Preferences.Instance.svmWrapper.TrainAuto(N, problem, Parameter, grid, null, null, null, null, null);
            }
            else if (type == trainingType.bestTotal)
            {
                Preferences.Instance.svmWrapper.TrainAutoBestTotal(N, problem, Parameter, grid, null, null, null, null, null);
                accuracy = Preferences.Instance.svmWrapper.output.accuracy;
            }
            else if (type == trainingType.cfv)
            {
                Preferences.Instance.svmWrapper.CrossValidate(problem, Parameter, N);
                accuracy = Preferences.Instance.svmWrapper.output.accuracy;
            }



            //used with default grids + only folds
            //double accuracy = Preferences.Instance.svm.TrainAuto(N, problem, Parameter);//, grid, null, null, null, null, null);

            /*
             * //used to compare against libsvm with grids and folds. when grids have the default minimum and max = min +1 and step = 2
             * libSVM_Grid gridc = libSVM_Grid.C();
             * gridc.max = gridc.min + 1;
             * gridc.step = 2;
             * libSVM_Grid gridgamma= libSVM_Grid.gamma();
             * gridgamma.max = gridgamma.min + 1;
             * gridgamma.step = 2;
             * libSVM_Grid gridp = libSVM_Grid.p();
             * gridp.max = gridp.min + 1;
             * gridp.step = 2;
             * libSVM_Grid gridnu = libSVM_Grid.nu();
             * gridnu.max = gridnu.min + 1;
             * gridnu.step = 2;
             * libSVM_Grid gridcoef = libSVM_Grid.coef0();
             * gridcoef.max = gridcoef.min + 1;
             * gridcoef.step = 2;
             * libSVM_Grid griddegree = libSVM_Grid.degree();
             * griddegree.max = griddegree.min + 1;
             * griddegree.step = 2;
             * double accuracy = Preferences.Instance.svm.TrainAuto(N, problem, Parameter, gridc, gridgamma, gridp, gridnu, gridcoef, griddegree);
             */

            GuiPreferences.Instance.setLog("Training + Cross Validation Finished");
            //calculate tr statistics
            if (type == trainingType.bestTotal)
            {
                int i = 1;
                foreach (int key in Preferences.Instance.svmWrapper.output.getKeys())
                {
                    GuiPreferences.Instance.setLog(i.ToString() + ". " + key.ToString() +
                                                   ": " + problem.labels[key].ToString() +
                                                   " => " + Preferences.Instance.svmWrapper.output.getValue(key).ToString());
                    i++;
                }
                GuiPreferences.Instance.setLog("Best Model Predicted accuracy from " + folds.ToString() + " cross fold validation: " + accuracy.ToString());

                printTRStatistics(problem);
            }
            else
            {
                GuiPreferences.Instance.setLog("Predicted accuracy from " + folds.ToString() + " cross fold validation: " + accuracy.ToString());
            }
        }
示例#10
0
文件: libSVM.cs 项目: uaqeel/ZeusXL
        /// <summary>
        /// Trains model automatically using users grids. if your svm_type/kernel_type does not need it then set it to null
        /// </summary>
        /// <param name="_fold">folds for samples >=2 </param>
        /// <param name="_problem">samples and labels</param>
        /// <param name="_parameter">model parameters</param>
        /// <param name="_grid_c">grid for C</param>
        /// <param name="_grid_gamma">grid for gamma</param>
        /// <param name="_grid_p">grid for p</param>
        /// <param name="_grid_nu">grid for nu</param>
        /// <param name="_grid_coef0">grid for coef</param>
        /// <param name="_grid_degree">grid for degree</param>
        public void TrainAuto(int _fold, libSVM_Problem _problem, libSVM_Parameter _parameter, libSVM_Grid _grid_c, libSVM_Grid _grid_gamma, libSVM_Grid _grid_p, libSVM_Grid _grid_nu, libSVM_Grid _grid_coef0, libSVM_Grid _grid_degree)
        {
            Dispose();

            if (_problem == null)
            {
                throw new Exception("libSVMProblem not initialized");
            }
            if (_problem.samples == null)
            {
                throw new Exception("libSVMProblem.samples = null");
            }
            if (_problem.labels == null)
            {
                throw new Exception("libSVMProblem.labels = null");
            }
            if (_problem.samples.Length != _problem.labels.Length)
            {
                throw new Exception("libSVMProblem.samples.Length != libSVMProblem.labels.length");
            }
            if (_parameter == null)
            {
                throw new Exception("libSVMParameter not initialized");
            }
            if (_parameter.weight == null && _parameter.weight_label != null)
            {
                throw new Exception("libSVMParameter.weight = null and libSVMParameter.weight_label != null");
            }
            if (_parameter.weight != null && _parameter.weight_label == null)
            {
                throw new Exception("libSVMParameter.weight_label = null and libSVMParameter.weight != null");
            }
            if (_parameter.weight != null && _parameter.weight_label != null && _parameter.weight_label.Length != _parameter.weight.Length)
            {
                throw new Exception("libSVMParameter.weight_label.Length != libSVMParameter.weight.Length");
            }
            if (_fold < 2 || _fold > _problem.samples.Length)
            {
                throw new Exception("fold < 2 || fold > nr_samples");
            }

            libSVM_Grid grid_gamma  = _grid_gamma;
            libSVM_Grid grid_coef0  = _grid_coef0;
            libSVM_Grid grid_degree = _grid_degree;

            if (_parameter.kernel_type == KERNEL_TYPE.LINEAR ||
                _parameter.kernel_type == KERNEL_TYPE.PRECOMPUTED)
            {
                grid_gamma  = new libSVM_Grid();
                grid_coef0  = new libSVM_Grid();
                grid_degree = new libSVM_Grid();
            }
            else if (_parameter.kernel_type == KERNEL_TYPE.POLY)
            {
                if (grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (grid_coef0 == null)
                {
                    throw new Exception("grid_coef0 not set");
                }
                if (grid_degree == null)
                {
                    throw new Exception("grid_degree not set");
                }
            }
            else if (_parameter.kernel_type == KERNEL_TYPE.RBF)
            {
                if (grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }

                grid_coef0  = new libSVM_Grid();
                grid_degree = new libSVM_Grid();
            }
            else if (_parameter.kernel_type == KERNEL_TYPE.SIGMOID)
            {
                if (grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (grid_coef0 == null)
                {
                    throw new Exception("grid_coef not set");
                }
                grid_degree = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown kernel_type");
            }

            libSVM_Grid grid_c  = _grid_c;
            libSVM_Grid grid_p  = _grid_p;
            libSVM_Grid grid_nu = _grid_nu;

            if (_parameter.svm_type == SVM_TYPE.C_SVC ||
                _parameter.svm_type == SVM_TYPE.ONE_CLASS)
            {
                if (grid_c == null)
                {
                    throw new Exception("grid_C not set");
                }

                grid_nu = new libSVM_Grid();
                grid_p  = new libSVM_Grid();
            }
            else if (_parameter.svm_type == SVM_TYPE.NU_SVC || _parameter.svm_type == SVM_TYPE.NU_SVR)
            {
                if (grid_nu == null)
                {
                    throw new Exception("grid_nu not set");
                }

                grid_c = new libSVM_Grid();
                grid_p = new libSVM_Grid();
            }
            else if (_parameter.svm_type == SVM_TYPE.EPSILON_SVR)
            {
                if (grid_p == null)
                {
                    throw new Exception("grid_p not set");
                }

                grid_c  = new libSVM_Grid();
                grid_nu = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown svm_type");
            }

            double c  = grid_c.min;
            double p  = grid_p.min;
            double nu = grid_nu.min;

            double gamma  = grid_gamma.min;
            double coef0  = grid_coef0.min;
            double degree = grid_degree.min;
            int    f      = 0;

            int nr_test_samples  = _problem.labels.Length / (int)_fold;
            int nr_train_samples = _problem.labels.Length - nr_test_samples;

            double test_probe = 0.0;

            libSVM_Parameter train_parameter = new libSVM_Parameter();

            //copy parameters set by user;
            train_parameter.weight       = _parameter.weight;
            train_parameter.weight_label = _parameter.weight_label;
            train_parameter.shrinking    = _parameter.shrinking;
            train_parameter.svm_type     = _parameter.svm_type;
            train_parameter.kernel_type  = _parameter.kernel_type;
            train_parameter.probability  = _parameter.probability;
            train_parameter.cache_size   = _parameter.cache_size;

            libSVM_Problem train_problem = new libSVM_Problem();
            libSVM_Problem test_problem  = new libSVM_Problem();

            //fold
            do
            {
                train_problem.labels  = new double[nr_train_samples];
                train_problem.samples = new double[nr_train_samples][];

                test_problem.labels  = new double[nr_test_samples];
                test_problem.samples = new double[nr_test_samples][];

                int j = 0;
                int k = 0;
                for (int i = 0; i < nr_train_samples + nr_test_samples; i++)
                {
                    if (i % _fold == 0)
                    {
                        test_problem.labels[j]  = _problem.labels[i];
                        test_problem.samples[j] = _problem.samples[i];
                        j++;
                    }
                    else
                    {
                        train_problem.labels[k]  = _problem.labels[i];
                        train_problem.samples[k] = _problem.samples[i];
                    }
                }

                //p
                p = grid_p.min;
                do
                {
                    //nu
                    nu = grid_nu.min;
                    do
                    {
                        //gamma
                        gamma = grid_gamma.min;
                        do
                        {
                            //coef0
                            coef0 = grid_coef0.min;
                            do
                            {
                                //degree

                                degree = grid_degree.min;
                                do
                                {
                                    //c

                                    c = grid_c.min;
                                    do
                                    {
                                        //svm_train alters problem_ptr so it's necessary to create it every time;
                                        IntPtr this_problem_ptr = libSVM_Problem_to_svm_problem_ptr(train_problem);

                                        //set generated parameters
                                        train_parameter.C      = c;
                                        train_parameter.p      = p;
                                        train_parameter.nu     = nu;
                                        train_parameter.gamma  = gamma;
                                        train_parameter.degree = (int)degree;
                                        train_parameter.coef0  = coef0;

                                        IntPtr this_parameter_ptr = libSVM_Parameter_to_svm_parameter_ptr(train_parameter);

                                        IntPtr error_ptr = svm_check_parameter(__problem_ptr, __parameter_ptr);

                                        if (error_ptr != IntPtr.Zero)
                                        {
                                            throw new Exception(ptr_to_string(error_ptr));
                                        }

                                        IntPtr this_model_ptr = svm_train(this_problem_ptr, this_parameter_ptr);

                                        double this_test_probe = 0.0;

                                        //count propperly recognized test_problem.samples
                                        for (int i = 0; i < test_problem.labels.Length; i++)
                                        {
                                            IntPtr svm_nodes_ptr = sample_to_svm_nodes_ptr(test_problem.samples[i]);

                                            if (test_problem.labels[i] == svm_predict(__model_ptr, svm_nodes_ptr))
                                            {
                                                this_test_probe++;
                                            }

                                            Free_ptr(ref svm_nodes_ptr);
                                        }

                                        //if first run then just copy this
                                        if (__model_ptr == IntPtr.Zero)
                                        {
                                            __model_ptr     = this_model_ptr;
                                            __parameter_ptr = this_parameter_ptr;
                                            __problem_ptr   = this_problem_ptr;

                                            test_probe = this_test_probe;
                                        }
                                        else
                                        //if model was better than previous then free previous data and copy this
                                        if (this_test_probe > test_probe)
                                        {
                                            Free_svm_parameter_ptr(ref __parameter_ptr);
                                            Free_svm_problem_ptr(ref __problem_ptr);
                                            svm_free_and_destroy_model(ref __model_ptr);

                                            __parameter_ptr = this_parameter_ptr;
                                            __problem_ptr   = this_problem_ptr;
                                            __model_ptr     = this_model_ptr;

                                            test_probe = this_test_probe;
                                        }
                                        //if not then free this model
                                        else
                                        {
                                            Free_svm_problem_ptr(ref this_problem_ptr);
                                            Free_svm_parameter_ptr(ref this_parameter_ptr);
                                            svm_free_and_destroy_model(ref this_model_ptr);
                                        }

                                        c *= grid_c.step;
                                    }while(c < grid_c.max);

                                    degree *= grid_degree.step;
                                }while(degree < grid_degree.max);

                                coef0 *= grid_coef0.step;
                            }while(coef0 < grid_coef0.max);

                            gamma *= grid_degree.step;
                        }while(gamma < grid_gamma.max);

                        nu *= grid_nu.step;
                    }while(nu < grid_nu.max);

                    p *= grid_p.step;
                }while(p < grid_p.max);

                f++;
            }while(f < _fold);
        }