Example #1
0
        static void Main(string[] args)
        {
            var    path    = Environment.CurrentDirectory;
            string DvCPath = System.IO.Path.Combine(path, DvC_TEST_FILE);
            string DvHPath = System.IO.Path.Combine(path, DvH_TEST_FILE);
            string HvCPath = System.IO.Path.Combine(path, HvC_TEST_FILE);

            DvC_prob = ProblemHelper.ReadAndScaleProblem(DvCPath);
            DvH_prob = ProblemHelper.ReadAndScaleProblem(DvHPath);
            HvC_prob = ProblemHelper.ReadAndScaleProblem(HvCPath);

            var DvCsvm = new C_SVC(DvC_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);
            var DvHsvm = new C_SVC(DvH_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);
            var HvCsvm = new C_SVC(HvC_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);

            var DvCcva = DvCsvm.GetCrossValidationAccuracy(5);
            var DvHcva = DvHsvm.GetCrossValidationAccuracy(2);
            var HvCcva = HvCsvm.GetCrossValidationAccuracy(5);

            DvCsvm.Export(System.IO.Path.Combine(path, DvC_MODEL_FILE));
            DvHsvm.Export(System.IO.Path.Combine(path, DvH_MODEL_FILE));
            HvCsvm.Export(System.IO.Path.Combine(path, HvC_MODEL_FILE));

            Console.WriteLine(String.Format("--------------------------"));
            Console.WriteLine(String.Format("DvC Result: {0}%", (Math.Round(DvCcva * 100, 2)).ToString()));
            Console.WriteLine(String.Format("DvH Result: {0}%", (Math.Round(DvHcva * 100, 2)).ToString()));
            Console.WriteLine(String.Format("HvC Result: {0}%", (Math.Round(HvCcva * 100, 2)).ToString()));
            Console.WriteLine(String.Format("--------------------------"));

            Console.ReadKey();
        }
        /// <summary>
        ///Show how to get the Mean Squared Error
        ///</summary>
        //[TestMethod()]
        public void GetMeanSquaredErrorTest()
        {
            var    svm = new Epsilon_SVR(training_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C, epsilon);
            double cms = svm.GetMeanSquaredError();

            Assert.IsTrue(cms > 0);
        }
Example #3
0
        /// <summary>
        /// Show how to get the accuracy using cross validation method
        /// Assert accurancy is greater than zero
        ///</summary>
        //[TestMethod()]
        public void DoCrossValidationTest()
        {
            var svm = new C_SVC(_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);
            var cva = svm.GetCrossValidationAccuracy(5);

            Assert.IsTrue(cva > 0);
        }
Example #4
0
        private void buttonSVM_Click(object sender, EventArgs e)
        {
            List <double> values = new List <double>();

            foreach (var column in checkedListBoxVariableRellenar.SelectedItems)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    values.Add(double.Parse(dt.Rows[i][column.ToString()].ToString()));
                }

                var dataTraining = ProblemHelper.ReadAndScaleProblem(new List <List <double> >()
                {
                    values
                });
                var    svm = new Epsilon_SVR(DataProblem, KernelHelper.RadialBasisFunctionKernel(Gamma), C, Elipson);
                double mse = svm.GetMeanSquaredError();

                var prediction = svm.Predict(dataTraining.x[0]);
            }


            // 1. primero se debe armar una subtabla con los atributos que se van a utilizar.
            // que serian los que estan en el checkbox.

            // 2. elegir la columna sobre la que se quiere rellenar valores

            // 3. se quitan los registros que contengan datos faltantes de las variables predictoras, para este caso son los que tengan valor de -200

            // 4. Aplicar el algoritmo de VSM

            // 5. Generar Vista con valores resultado

            // 6. Generar resumen de resultados: en tal fila, cambie tal por tal.
        }
        public override IModelLikelihood <double, int> GenerateModelLikelihood(IDataSet <double, int> training_set)
        {
            svm_problem prob = new svm_problem();

            prob.l = training_set.InstanceCount;
            prob.x = CreateNodeArray(ToolsCollection.ConvertToArray2D(training_set.FeatureData));
            prob.y = ToolsCollection.ConvertToDoubleArray(ToolsCollection.ConvertToArray2D(training_set.LabelData).Select1DIndex1(0));

            //Train model---------------------------------------------------------------------
            return(new ModelLibSVMCSVC(training_set.DataContext, new C_SVC(prob, KernelHelper.RadialBasisFunctionKernel(this.Gamma), this.C, this.CacheSize, true)));
        }
Example #6
0
        public static void LibSVM(List <string> inputData, List <string> testData)
        {
            var inputFilePath = @"D:\新西兰学习生活\大学上课\乐谱数据\input.txt";
            var testFilePath  = @"D:\新西兰学习生活\大学上课\乐谱数据\test.txt";

            PrepareDataLibSvm(inputData, inputFilePath);
            PrepareDataLibSvm(testData, testFilePath);

            var _prob = ProblemHelper.ReadAndScaleProblem(inputFilePath);
            var svm   = new C_SVC(_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);
        }
Example #7
0
        public static void SVMPredict()
        {
            var    svm      = new C_SVC(prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);
            double accuracy = svm.GetCrossValidationAccuracy(nr_fold);

            for (int i = 0; i < test.l; i++)
            {
                svm_node[] x       = test.x[i];
                double     y       = test.y[i];
                double     predict = svm.Predict(x); // returns the predicted value 'y'
                Dictionary <int, double> probabilities = svm.PredictProbabilities(x);
                // returns the probabilities for each 'y' value
                Console.WriteLine(predict + " :" + probabilities[1]);
            }
            Console.ReadKey();
        }
Example #8
0
        /// <summary>
        ///Show how to predict probabilities for classification problems
        ///Verify that the prediction is always the most probable class
        ///</summary>
        //[TestMethod()]
        public void PredictTest()
        {
            var svm      = new C_SVC(_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C);
            var nb_class = _prob.y.Distinct().Count();

            for (int i = 0; i < _prob.l; i++)
            {
                var x             = _prob.x[i];
                var y             = _prob.y[i];
                var probabilities = svm.PredictProbabilities(x);
                var predict       = svm.Predict(x);
                Assert.IsTrue(predict == probabilities.OrderByDescending(p => p.Value).First().Key);
                Assert.IsNotNull(probabilities);
                Assert.IsTrue(probabilities.Count == nb_class);
                var sum = probabilities.Sum(e => e.Value);
            }
        }
        /// <summary>
        ///Show how predict values for regression problems
        ///</summary>
        //[TestMethod()]
        public void PredictTest()
        {
            //Train the svm with the training datatset
            var svm = new Epsilon_SVR(training_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C, epsilon);

            for (int i = 0; i < test_prob.l; i++)
            {
                var x              = test_prob.x[i];
                var expectedValue  = test_prob.y[i];
                var predictedValue = svm.Predict(x);
                Console.WriteLine(
                    String.Format(
                        "Predicted value = {0} || Expected value = {1} || Error = {2}",
                        predictedValue,
                        expectedValue,
                        Math.Abs(predictedValue - expectedValue)));
            }
        }
Example #10
0
        private static (double C, double sigma) Dataset3Params(Matrix <double> x, Vector <double> y, Matrix <double> xval, Vector <double> yval)
        {
            double[] c_val      = new [] { 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 3 }; // possibili valori di C
            double[] sigma_test = new [] { 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 3 }; // possibili valori di sigma

            // Results:
            //  [:,0] - error
            //  [:,1] - C
            //  [:,2] -  sigma
            Matrix <double> results = Matrix <double> .Build.Dense(c_val.Length *sigma_test.Length, 3);

            // convert x, y in libsvm format
            List <List <double> > libSvmData = ConvertToLibSvmFormat(x, y);

            // try all possible pairs of C and sigma
            int i = 0;

            foreach (double c_temp in c_val)
            {
                foreach (double s_temp in sigma_test)
                {
                    double gamma     = 1 / (2 * s_temp * s_temp);
                    var    rbfKernel = KernelHelper.RadialBasisFunctionKernel(gamma);

                    svm_problem prob = ProblemHelper.ReadProblem(libSvmData);
                    C_SVC       svc  = new C_SVC(prob, rbfKernel, c_temp);

                    double error = ComputeValidationError(svc, xval, yval);

                    results[i, 0] = error;
                    results[i, 1] = c_temp;
                    results[i, 2] = s_temp;
                    i++;
                }
            }

            int idx = results.Column(0).MinimumIndex();

            return(results.Row(idx)[1], results.Row(idx)[2]);
        }
 /// <summary>
 ///Show how to get the sqsuared correlation coefficient using cross validation method
 ///Note : cros validation use the full dataset to increase the accuracy
 ///</summary>
 //[TestMethod()]
 public void GetCrossValidationSqsuaredCorrelationCoefficientTest()
 {
     var    svm = new Epsilon_SVR(training_prob, KernelHelper.RadialBasisFunctionKernel(gamma), C, epsilon);
     double CVS = svm.GetCrossValidationSqsuaredCorrelationCoefficient();
 }
Example #12
0
        static void Main(string[] args)
        {
            bool   kernelparam     = false;
            bool   properformat    = false;
            bool   needsFormatting = false;
            bool   done            = false;
            int    vectorlength; // number of features
            int    kernelchoice; // integer representation of selected kernel
            int    numberofArgs = args.Length;
            string inputmatrix, savefilename, labelfile;
            string path = Directory.GetCurrentDirectory();
            string save_model_name;
            string kerneltype;
            string testfile;

            /* SVM specific initializations
             */
            int degree = 3; // default for none specified
            int r      = 1;
            // C and gamma come from using grid.py on the training set resume.mat 982 x 7768
            double C     = 2.0;
            double gamma = 0.001953125; // used for Radial Basis Function Kernel (RBF)
            C_SVC  svm;                 // setup the default variable for the SVM

            if (numberofArgs < 1)
            {
                Console.WriteLine(MyStrings.usage);
                System.Environment.Exit(1);
            } // Exit if no params passed on the command line

            /* At least one command line parameter we can continue, but it can't be an int.
             * so check for that next.
             */
            if (numberofArgs == 1 && Int32.TryParse(args[0], out kernelchoice))
            {
                Console.WriteLine(MyStrings.usage); // single paramater can't be int
                System.Environment.Exit(1);
            }
            else // Assume file name and check if it needs formatting, if not we are good to train and save the model
            {
                kernelparam     = false;
                properformat    = HelperFunctions.CheckFormat(args[0]);
                inputmatrix     = args[0];
                savefilename    = inputmatrix.Replace(".mat", ".svm"); // update the suffix
                svm             = new C_SVC(savefilename, KernelHelper.LinearKernel(), C);
                save_model_name = savefilename.Replace(".svm", ".model");
                svm.Export(save_model_name);
                done = true;
            }

            if (numberofArgs >= 1)
            {
                if (Int32.TryParse(args[0], out kernelchoice))
                {
                    kernelparam = true;

                    switch (numberofArgs)
                    {
                    case 2:
                        needsFormatting = HelperFunctions.CheckFormat(args[1]);
                        inputmatrix     = args[1];
                        if (needsFormatting)
                        {
                            Console.WriteLine("Missing label file");
                            System.Environment.Exit(1);
                        }
                        break;

                    case 3:
                        needsFormatting = HelperFunctions.CheckFormat(args[1]);
                        inputmatrix     = args[1];
                        labelfile       = args[2];
                        break;

                    case 4:
                        needsFormatting = HelperFunctions.CheckFormat(args[1]);
                        inputmatrix     = args[1];
                        labelfile       = args[2];
                        testfile        = args[3];
                        break;

                    default:

                        Console.WriteLine("too many parameters");
                        Console.WriteLine(MyStrings.usage);
                        System.Environment.Exit(1);
                        break;
                    }
                }
            }
            savefilename = inputmatrix.Replace(".mat", ".svm"); // update the suffix
            if (!done && needsFormatting && args.Length >= 2)
            {
                inputmatrix  = args[1];
                labelfile    = args[2];
                vectorlength = HelperFunctions.VectorLength(inputmatrix);            // Get the number of features
                string[] labels = new string[HelperFunctions.SampleSize(labelfile)]; // Calculate the number of labels and use to create storage

                /* if the input matrix is not already in the correct format Call reformat function
                 * result is that a file is written that is the LIBSVM format, expects the
                 * labels to be in a separate file
                 *
                 * Reformatdata(string[] data, string labels, string fname)
                 *
                 */

                HelperFunctions.Reformatdata(inputmatrix, labels, savefilename, vectorlength);
            }


            // Train the SVM

            /* "." means every 1,000 iterations (or every #data iterations is your #data is less than 1,000).
             *  "*" means that after iterations of using a smaller shrunk problem, we reset to use the whole set. */
            /*  optimization finished, #iter = 219
             *  nu = 0.431030
             *  obj = -100.877286, rho = 0.424632
             *  nSV = 132, nBSV = 107
             *  Total nSV = 132
             *  obj is the optimal objective value of the dual SVM problem. rho is the bias term in the decision
             *  function sgn(w^Tx - rho). nSV and nBSV are number of support vectors and bounded support vectors
             *  (i.e., alpha_i = C). nu-svm is a somewhat equivalent form of C-SVM where C is replaced by nu.
             *  nu simply shows the corresponding parameter.
             */

            /* if a kernel is specified on the command line, then select the corresponding kernel for training the SVM as follows
             * 0 = linear
             * 1 = polynomial
             * 2 = RBF
             * 3 = sigmoind
             * 4 = precomputed
             */

            // 7/23/19 fix up save file name, kernelchoice does not seem to be in the rigth place, also logic flow thru above switch and if statements needs some review

            Int32.TryParse(args[0], out kernelchoice);


            if (kernelparam)
            {
                int caseSwitch = kernelchoice;
                switch (caseSwitch)
                {
                case 0:
                    svm        = new C_SVC(savefilename, KernelHelper.LinearKernel(), C);
                    kerneltype = "Linear";
                    break;

                case 1:
                    svm        = new C_SVC(savefilename, KernelHelper.PolynomialKernel(degree, gamma, r), C);
                    kerneltype = "Polynomial";
                    break;

                case 2:
                    svm        = new C_SVC(savefilename, KernelHelper.RadialBasisFunctionKernel(gamma), C);
                    kerneltype = "RBF";
                    break;

                default:
                    svm        = new C_SVC(savefilename, KernelHelper.LinearKernel(), C);
                    kerneltype = "Linear";
                    break;
                }
            }
            else
            {
                svm        = new C_SVC(savefilename, KernelHelper.LinearKernel(), C);
                kerneltype = "Linear";
            }

            // For RBF kernel, linear kernel would be KernelHelper.LinearKernel
            //
            // var accuracy = svm.GetCrossValidationAccuracy(5);
            save_model_name = savefilename.Replace(".svm", ".model");
            svm.Export(save_model_name);

            /*
             * ********** Stoppted here for checking file input formats
             */


            //double accuracy = svm.Predict(testfile);
            //Console.WriteLine(MyStrings.Accuracy, accuracy * 100);
            Console.WriteLine("SVM kernel type {0}", kerneltype);
        }
Example #13
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

            CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            var M = Matrix <double> .Build;
            var V = Vector <double> .Build;


            //// =============== Part 1: Loading and Visualizing Data ================
            //  We start the exercise by first loading and visualizing the dataset.
            //  The following code will load the dataset into your environment and plot
            //  the data.
            //

            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex6data1:
            // You will have X, y in your environment
            Dictionary <string, Matrix <double> > ms = MatlabReader.ReadAll <double>("data\\ex6data1.mat");

            Matrix <double> X = ms["X"];                 // 51 X 2
            Vector <double> y = ms["y"].Column(0);       // 51 X 1

            // Plot training data
            GnuPlot.HoldOn();
            PlotData(X, y);

            Pause();

            //// ==================== Part 2: Training Linear SVM ====================
            //  The following code will train a linear SVM on the dataset and plot the
            //  decision boundary learned.
            //

            System.Console.WriteLine("\nTraining Linear SVM ...\n");

            // You should try to change the C value below and see how the decision
            // boundary varies (e.g., try C = 1000)
            double C            = 1.0;
            var    linearKernel = KernelHelper.LinearKernel();

            List <List <double> > libSvmData = ConvertToLibSvmFormat(X, y);
            svm_problem           prob       = ProblemHelper.ReadProblem(libSvmData);
            var svc = new C_SVC(prob, linearKernel, C);

            PlotBoundary(X, svc);
            GnuPlot.HoldOff();

            System.Console.WriteLine();

            Pause();

            //// =============== Part 3: Implementing Gaussian Kernel ===============
            //  You will now implement the Gaussian kernel to use
            //  with the SVM. You should complete the code in gaussianKernel.m
            //

            System.Console.WriteLine("\nEvaluating the Gaussian Kernel ...\n");

            double sigma = 2.0;
            double sim   = GaussianKernel(
                V.DenseOfArray(new [] { 1.0, 2, 1 }),
                V.DenseOfArray(new [] { 0.0, 4, -1 }),
                sigma
                );

            System.Console.WriteLine("Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = {0:f6} :\n\t{1:f6}\n(for sigma = 2, this value should be about 0.324652)\n", sigma, sim);

            Pause();

            //// =============== Part 4: Visualizing Dataset 2 ================
            //  The following code will load the next dataset into your environment and
            //  plot the data.
            //

            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex6data2:
            // You will have X, y in your environment
            ms = MatlabReader.ReadAll <double>("data\\ex6data2.mat");

            X = ms["X"];                 // 863 X 2
            y = ms["y"].Column(0);       // 863 X 1

            // Plot training data
            GnuPlot.HoldOn();
            PlotData(X, y);

            Pause();

            //// ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========
            //  After you have implemented the kernel, we can now use it to train the
            //  SVM classifier.
            //

            System.Console.WriteLine("\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\n");

            // SVM Parameters
            C     = 1;
            sigma = 0.1;
            double gamma = 1 / (2 * sigma * sigma);

            var rbfKernel = KernelHelper.RadialBasisFunctionKernel(gamma);

            libSvmData = ConvertToLibSvmFormat(X, y);
            prob       = ProblemHelper.ReadProblem(libSvmData);
            svc        = new C_SVC(prob, rbfKernel, C);


            PlotBoundary(X, svc);
            GnuPlot.HoldOff();

            Pause();

            double acc = svc.GetCrossValidationAccuracy(10);

            System.Console.WriteLine("\nCross Validation Accuracy: {0:f6}\n", acc);

            Pause();

            //// =============== Part 6: Visualizing Dataset 3 ================
            //  The following code will load the next dataset into your environment and
            //  plot the data.
            //

            System.Console.WriteLine("Loading and Visualizing Data ...\n");

            // Load from ex6data2:
            // You will have X, y in your environment
            ms = MatlabReader.ReadAll <double>("data\\ex6data3.mat");

            Matrix <double> Xval;
            Vector <double> yval;

            X    = ms["X"];              // 211 X 2
            y    = ms["y"].Column(0);    // 211 X 1
            Xval = ms["Xval"];           // 200 X 2
            yval = ms["yval"].Column(0); // 200 X 1

            // Plot training data
            GnuPlot.HoldOn();
            PlotData(X, y);

            //// ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========

            //  This is a different dataset that you can use to experiment with. Try
            //  different values of C and sigma here.
            //


            (C, sigma) = Dataset3Params(X, y, Xval, yval);

            gamma     = 1 / (2 * sigma * sigma);
            rbfKernel = KernelHelper.RadialBasisFunctionKernel(gamma);

            libSvmData = ConvertToLibSvmFormat(X, y);
            prob       = ProblemHelper.ReadProblem(libSvmData);
            svc        = new C_SVC(prob, rbfKernel, C);

            PlotBoundary(X, svc);

            GnuPlot.HoldOff();
            Pause();
        }
Example #14
0
        static void Main(string[] args)
        {
            bool   kernelparam  = false;
            int    numberofArgs = args.Length;
            string inputmatrix;
            string path = Directory.GetCurrentDirectory();
            string save_model_name;
            string kerneltype;
            string testfile;

            /* SVM specific initializations
             */
            int degree = 3; // default for none specified
            int r      = 1;
            // C and gamma come from using grid.py on the training set resume.mat 982 x 7768
            double C     = 2.0;
            double gamma = 0.001953125; // used for Radial Basis Function Kernel (RBF)
            C_SVC  svm;                 // setup the default variable for the SVM

            /*
             * Three parameters are required, kernel selection, training file and test file
             */

            if (args.Length != 3)
            {
                Console.WriteLine(MyStrings.usage);
                System.Environment.Exit(1);
            }

            if (kernelparam = Int32.TryParse(args[0], out int kernelchoice) && kernelchoice <= 3)
            {
                //Legal value for kernelchoice are 0-3
                //kernelchoice = 1;
            }
            else
            {
                // Not a legal kernel selection
                Console.WriteLine(MyStrings.usage);
                System.Environment.Exit(1);
            }
            inputmatrix = args[1];
            testfile    = args[2];
            if (!HelperFunctions.CheckFormat(inputmatrix))
            {
                Console.WriteLine(MyStrings.TrainingFileFormat, inputmatrix);
                System.Environment.Exit(1);
            }
            if (!File.Exists(testfile))
            {
                Console.WriteLine(MyStrings.File_error, inputmatrix);
                System.Environment.Exit(1);
            }

            // Train the SVM

            switch (kernelchoice)
            {
            case 0:
                svm        = new C_SVC(inputmatrix, KernelHelper.LinearKernel(), C);
                kerneltype = MyStrings.Linear;
                break;

            case 1:
                svm        = new C_SVC(inputmatrix, KernelHelper.PolynomialKernel(degree, gamma, r), C);
                kerneltype = MyStrings.Polynomial;
                break;

            case 2:
                svm        = new C_SVC(inputmatrix, KernelHelper.RadialBasisFunctionKernel(gamma), C);
                kerneltype = MyStrings.RBF;
                break;

            case 3:
                svm        = new C_SVC(inputmatrix, KernelHelper.SigmoidKernel(gamma, r), C);
                kerneltype = MyStrings.Sigmoid;
                break;

            default:
                svm        = new C_SVC(inputmatrix, KernelHelper.LinearKernel(), C);
                kerneltype = MyStrings.Linear;
                break;
            }

            // var accuracy = svm.GetCrossValidationAccuracy(5);
            save_model_name = String.Concat(inputmatrix, ".model");
            svm.Export(save_model_name);
            var    predfile = ProblemHelper.ReadProblem(testfile);
            double result   = HelperFunctions.PredictTestSet(testfile, svm);

            Console.WriteLine(MyStrings.Accuracy, Math.Round(result * 100, 2));
            Console.Write("SVM kernel type {0}      ", kerneltype);
            Console.WriteLine(MyStrings.Parameters, C, gamma, degree, r);
        }
        void svm()
        {
            var pro = ProblemHelper.ReadProblem("res.txt");

            model = new C_SVC(pro, KernelHelper.RadialBasisFunctionKernel(8), 32.0);
        }
Example #16
0
        static void Main(string[] args)
        {
            List <double[]> continuousTrainData = DataWrangler.LoadContinuousDataAsync(TrainingCsv, _indexToIgnore).Result;
            List <double[]> continuousTestData  = DataWrangler.LoadContinuousDataAsync(TestingCsv, _indexToIgnore).Result;

            // Print continuous columns for calculating elbows in external tool(https://bl.ocks.org/rpgove/0060ff3b656618e9136b)
            foreach (int i in _continuousIndexes)
            {
                using (StreamWriter sw = new StreamWriter($"{i}.txt"))
                {
                    sw.WriteLine(string.Join(",", continuousTrainData.Select(array => array[i])));
                }
            }

            // Convert continuous to discrete
            Dictionary <int, GaussianClusterCollection> indexClusterMapping = DataWrangler.GetIndexClustersMap(continuousTrainData, _indexElbowMap);
            List <int[]> discreteTrainData = DataWrangler.ConvertContinuesToDiscrete(continuousTrainData, indexClusterMapping);
            List <int[]> discreteTestData  = DataWrangler.ConvertContinuesToDiscrete(continuousTestData, indexClusterMapping);

            var problem = ProblemHelper.ReadProblem(discreteTrainData.Select(arr =>
            {
                // Move class to front as it is expected by libsvm.
                int temp = arr[0];
                arr[SVMSupportedClassIndex] = arr[OriginalClassIndex];
                arr[OriginalClassIndex]     = temp;
                return(arr.Select(i => (double)i).ToList());
            }).ToList());

            var test = ProblemHelper.ReadProblem(discreteTestData.Select(arr =>
            {
                // Move class to front as it is expected by libsvm.
                int temp = arr[0];
                arr[SVMSupportedClassIndex] = arr[OriginalClassIndex];
                arr[OriginalClassIndex]     = temp;
                return(arr.Select(i => (double)i).ToList());
            }).ToList());

            // defaults taken from documentation http://weka.sourceforge.net/doc.stable/weka/classifiers/functions/LibSVM.html
            double c      = 1;               // default C is 1
            double gamma  = 1.0 / problem.l; // default gamma is 1/k
            double r      = 0;               // default coef0 is 0
            int    degree = 3;               // default degree is 3

            Dictionary <string, Kernel> nameKernelMap = new Dictionary <string, Kernel>(StringComparer.OrdinalIgnoreCase)
            {
                { "Linear", KernelHelper.LinearKernel() },
                { "Polynomial", KernelHelper.PolynomialKernel(degree, gamma, r) },
                { "Radial", KernelHelper.RadialBasisFunctionKernel(gamma) },
                { "Sigmoid", KernelHelper.SigmoidKernel(gamma, r) },
            };

            // Get accuracies for base comparison
            // DON'T DO PARALLEL. We don't know if the underlying implementation is MT safe or not.
            //Parallel.ForEach(nameKernelMap.Keys, (kernelName) =>
            foreach (string kernelName in nameKernelMap.Keys)
            {
                Console.WriteLine($"{kernelName}: {GetSVMAccuracy(problem, test, nameKernelMap[kernelName], c)}");
            }
            ;

            // Get accuracy of with Naive Bayes
            double[]             classWeightPrior      = new[] { 1.0, 1.0 };
            double[]             classPriorProbability = new[] { 0.5, 0.5 };
            NaiveBayesClassifier naiveBayes            = NaiveBayesClassifier.Load(discreteTrainData, SVMSupportedClassIndex, classWeightPrior, classPriorProbability);

            Console.WriteLine($"Naive Bayes: {naiveBayes.GetPredictionAccuracy(discreteTestData, SVMSupportedClassIndex)}");

            // Calculate SVMs Bias and Variance
            List <List <int[]> > samples = Sampler.SampleData(discreteTrainData, BiasVarianceNumOfSamples);

            ConcurrentDictionary <string, ConcurrentDictionary <int, ConcurrentDictionary <int, int> > > kernelInstanceClassifierPredictionsMappings = new ConcurrentDictionary <string, ConcurrentDictionary <int, ConcurrentDictionary <int, int> > >(StringComparer.OrdinalIgnoreCase);

            foreach (string kernelName in nameKernelMap.Keys)
            {
                ConcurrentDictionary <int, ConcurrentDictionary <int, int> > instanceClassifierPredictionMappings = kernelInstanceClassifierPredictionsMappings.GetOrAdd(kernelName, new ConcurrentDictionary <int, ConcurrentDictionary <int, int> >());
                for (int classifierIndex = 0; classifierIndex < BiasVarianceNumOfSamples; classifierIndex++)
                {
                    problem = ProblemHelper.ReadProblem(samples[classifierIndex].Select(arr => arr.Select(i => (double)i).ToList()).ToList());

                    var svm = new C_SVC(problem, nameKernelMap[kernelName], c);

                    for (int instanceIndex = 0; instanceIndex < discreteTestData.Count; instanceIndex++)
                    {
                        ConcurrentDictionary <int, int> classifierPredictionMappings = instanceClassifierPredictionMappings.GetOrAdd(instanceIndex, new ConcurrentDictionary <int, int>());
                        test = ProblemHelper.ReadProblem(new List <List <double> > {
                            discreteTestData[instanceIndex].Select(i => (double)i).ToList()
                        });

                        for (int i = 0; i < test.l; i++)
                        {
                            var x = test.x[i];
                            var y = test.y[i];
                            classifierPredictionMappings.GetOrAdd(classifierIndex, (int)svm.Predict(x));
                        }
                    }
                }
            }

            Console.WriteLine("Kernel, Bias, Variance, Accuracy");
            foreach (string kernelName in nameKernelMap.Keys)
            {
                ConcurrentDictionary <int, ConcurrentDictionary <int, int> > instanceClassifierPredictionMappings = kernelInstanceClassifierPredictionsMappings.GetOrAdd(kernelName, new ConcurrentDictionary <int, ConcurrentDictionary <int, int> >());
                Tuple <double, double, double> biasVarianceAccuracy = BiasVarianceHelper.GetBiasVarianceAccuracy(discreteTestData, SVMSupportedClassIndex, instanceClassifierPredictionMappings);
                Console.WriteLine($"{kernelName}, {biasVarianceAccuracy.Item1}, {biasVarianceAccuracy.Item2}, {biasVarianceAccuracy.Item3}");
            }

            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();
        }