コード例 #1
0
        static public double PredictTestSet(string inputfile, C_SVC svm)
        {
            /* Given a test set "Inputfile" and previoulsy trained SVM calculates the accuracty of the
             * the trained SVM. Fucntion returns the percent correct.
             */

            int    i;
            double total         = 1;
            var    predfile      = ProblemHelper.ReadProblem(inputfile); // Reads in the SVM format file and results in a svm_problem format
            double expectedValue = 0;

            for (i = 0; i < predfile.l; i++)
            {
                var x = predfile.x[i];                  // x is the ith vector sample
                expectedValue = predfile.y[i];
                var predictedValue = svm.Predict(x);    // Make label prediciton
                if (predictedValue == expectedValue)    // Compare the prediction with actual
                {
                    total++;
                }
            }
            double result = ((double)total / (double)i);    // Calculate the accuracy and return

            return(result);
        }
コード例 #2
0
        /// <summary>
        ///Test pour ReadProblem
        ///</summary>
        //[TestMethod()]
        public void ReadProblemTest()
        {
            string full_path = System.IO.Path.Combine(base_path, TEST_FILE);
            var    prob      = ProblemHelper.ReadProblem(full_path);

            Assert.IsNotNull(prob);
        }
コード例 #3
0
        public double buildSVMTestCorpus(string filename)
        {
            double total = 0, tp = 0;
            string trainDataPath = filename + "SimpleTrainSVM.txt";

            if (File.Exists(trainDataPath))
            {
                _test = ProblemHelper.ReadProblem(trainDataPath);
                _test = ProblemHelper.ScaleProblem(_test);
                svm_node[][] sn = _test.x;
                total = sn.Length;
                double[] lbls = _test.y;
                for (int i = 0; i < sn.Length; i++)
                {
                    if (_test.y[i] == svm.Predict(sn[i]))
                    {
                        tp++;
                    }
                }
                fileExistance = true;
                //ProblemHelper.WriteProblem(filename+"TestSVM.txt", _test);
            }
            else
            {
                SVMScale readyData = new SVMScale();
                readyData.buildSVMCorpus(filename);
                readyData.scaleSVMData(filename);
                buildSVMTestCorpus(filename);
            }
            return((tp / total) * 100);
        }
コード例 #4
0
        /// <summary>
        ///Test pour ScaleProblem
        ///</summary>
        //[TestMethod()]
        public void ScaleProblemTest()
        {
            string full_path = System.IO.Path.Combine(base_path, TEST_FILE);
            var    prob      = ProblemHelper.ScaleProblem(ProblemHelper.ReadProblem(full_path));

            Assert.IsNotNull(prob);
            Assert.IsTrue(prob.x.Max(v => v.Max(n => n.value)) == 1.0);
            Assert.IsTrue(prob.x.Min(v => v.Min(n => n.value)) == -1.0);
        }
コード例 #5
0
        public C_SVC_Tests()
        {
            var current_path = Environment.CurrentDirectory;
            var pos          = current_path.IndexOf("libsvm.net");

            base_path = current_path.Substring(0, pos + 10);
            string full_path = System.IO.Path.Combine(base_path, XOR_DATASET);

            xor_problem = ProblemHelper.ReadProblem(full_path);
        }
コード例 #6
0
        public bool buildSVMCorpus(string filename)
        {
            string trainDataPath = filename + "TrainSVM.txt";

            if (File.Exists(trainDataPath))
            {
                _prob = ProblemHelper.ReadProblem(trainDataPath);
                _test = ProblemHelper.ScaleProblem(_prob);
                svm   = new C_SVC(_test, KernelHelper.LinearKernel(), C);
                ProblemHelper.WriteProblem(filename + "output.txt", _test);
                fileExistance = true;
            }
            return(fileExistance);
        }
コード例 #7
0
        /// <summary>
        ///Test pour WriteProblem
        ///</summary>
        //[TestMethod()]
        public void WriteProblemTest()
        {
            string full_test_path = System.IO.Path.Combine(base_path, TEST_FILE);
            var    prob           = ProblemHelper.ReadProblem(full_test_path);

            string full_write_path = System.IO.Path.Combine(base_path, WRITE_FILE);

            if (File.Exists(full_write_path))
            {
                File.Delete(full_write_path);
            }

            ProblemHelper.WriteProblem(full_write_path, prob);
            Assert.IsTrue(File.Exists(full_write_path));
            File.Delete(full_write_path); // cleaunp after test succeeded
        }
コード例 #8
0
        public void Should_be_able_to_read_files()
        {
            var prob = ProblemHelper.ReadProblem(path_to_xor_dataset);

            Assert.IsNotNull(prob);

            var lineCount = File.ReadLines(path_to_xor_dataset).Count();

            Assert.IsTrue(prob.l == lineCount);
            Assert.IsTrue(prob.x.Count() == lineCount);
            Assert.IsTrue(prob.y.Count() == lineCount);

            Assert.IsTrue(prob.x.Max(v => v.Max(n => n.value)) == 1.0);
            Assert.IsTrue(prob.x.Min(v => v.Min(n => n.value)) == -1.0);

            Assert.IsTrue(prob.y.Max() == 1.0);
            Assert.IsTrue(prob.y.Min() == 0);
        }
コード例 #9
0
        public void Should_be_able_to_read_files()
        {
            svm_problem problem = ProblemHelper.ReadProblem(path_to_xor_dataset);

            var expectedProblem = GetExpectedXorProblem();

            Assert.AreEqual(expectedProblem.l, problem.l);
            CollectionAssert.AreEqual(expectedProblem.y, problem.y);

            // We cannot use CollectionAssert so we use this ugly method
            for (var i = 0; i < problem.x.Length; i++)
            {
                for (var j = 0; j < problem.x[i].Length; j++)
                {
                    Assert.AreEqual(expectedProblem.x[i][j].index, problem.x[i][j].index, string.Format("Index value are different on line {0} element {1}", i + 1, j + 1));
                    Assert.AreEqual(expectedProblem.x[i][j].value, problem.x[i][j].value, string.Format("Value are different on line {0} element {1} ", i + 1, j + 1));
                }
            }
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: mlnethub/ML.Net
        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]);
        }
コード例 #11
0
ファイル: XorTest.cs プロジェクト: xwyangjshb/libsvm.net
        public void XORTest()
        {
            var range      = Enumerable.Range(-10, 16);
            var log2gammas = range.Select(i => Math.Pow(2, i));
            var log2Cs     = range.Select(i => Math.Pow(2, i + 1));
            var log2Rs     = range.Select(i => Math.Pow(2, i + 1));
            var prob       = ProblemHelper.ReadProblem(XOR_TRAINING_FILE);
            //Assert.IsTrue(prob.l == 4);
            Tuple <double, double, double, int> best = Tuple.Create(0.0, 0.0, 0.0, prob.l);

            foreach (var g in log2gammas)
            {
                foreach (var c in log2Cs)
                {
                    foreach (var r in log2Rs)
                    {
                        var svm       = new C_SVC(prob, KernelHelper.SigmoidKernel(g, r), c);
                        var errorCout = 0;
                        for (int i = 0; i < prob.l; i++)
                        {
                            //var x = (prob.x[i].FirstOrDefault(xi => xi.index == 1) == null) ? 0.0 : prob.x[i].FirstOrDefault(xi => xi.index == 1).value;
                            //var y = (prob.x[i].FirstOrDefault(xi => xi.index == 2) == null) ? 0.0 : prob.x[i].FirstOrDefault(xi => xi.index == 2).value;
                            var z             = svm.Predict(prob.x[i]);
                            var probabilities = svm.PredictProbabilities(prob.x[i]);
                            if (z != prob.y[i])
                            {
                                errorCout++;
                            }
                            //Debug.WriteLine(String.Format("x={0} & y={1} => z={2} -- {3}", x, y, z, z == prob.y[i]));
                        }
                        if (errorCout < best.Item4)
                        {
                            best = Tuple.Create(g, c, r, errorCout);
                        }
                        //Debug.WriteLine(String.Format("g={0} && C={1} && C={2} => Error rate = {3}%", g, c, r, (double)errorCout / prob.l * 100));
                    }
                }
            }
            Debug.WriteLine(String.Format("BEST :: g={0} && C={1} && R={2} => Error rate = {3}%", best.Item1, best.Item2, best.Item3, (double)best.Item4 / (double)prob.l * 100));
        }
コード例 #12
0
        static void Main(string[] args)
        {
            if (!System.Console.IsOutputRedirected)
            {
                System.Console.Clear();
            }

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

            System.Console.WriteLine("Sentiment Analysis");
            System.Console.WriteLine("======================\n");

            // load data
            System.Console.WriteLine("Loading data....");
            string fileContent = ReadDataFile(".\\data\\wikipedia-detox-250-line-data.tsv");

            // preprocess file
            System.Console.WriteLine("Processing data....");
            string[,] processedComments = ProcessComments(fileContent);
            System.Console.WriteLine($"Data file contains {processedComments.GetLength(0)} comments\n");
            // for(int i = 0; i < 3; i++)
            // {
            //     System.Console.WriteLine($"{processedComments[i, 0]}\t{processedComments[i, 1]}");
            // }
            // System.Console.WriteLine("...\n");

            // generate the vocabulary list
            System.Console.WriteLine("Generating Vocabulary List....");
            string[] vocab = GenerateVocabulary(processedComments);
            System.Console.WriteLine($"Vocabulary generated with {vocab.Length} words\n");

            // get labels from preprocessed comments
            System.Console.WriteLine("Retrieving labels...");
            Vector <double> Y = GetLables(processedComments);

            //System.Console.WriteLine(Y);

            // extract features from processed comments and vocabulary
            System.Console.WriteLine("Extracting features...");
            Matrix <double> X = GetFeatures(processedComments, vocab);

            //System.Console.WriteLine(X);

            // split the data into train and test in ratio 80:20
            System.Console.WriteLine("Splitting data...");
            int m           = X.RowCount;
            int n           = X.ColumnCount;
            int testsetSize = m * 20 / 100;

            Vector <double> testLabel    = Y.SubVector(0, testsetSize);
            Matrix <double> testFeatures = X.SubMatrix(0, testsetSize, 0, n);

            Vector <double> trainingLabel    = Y.SubVector(testsetSize, m - testsetSize);
            Matrix <double> trainingFeatures = X.SubMatrix(testsetSize, m - testsetSize, 0, n);

            System.Console.WriteLine();
            System.Console.WriteLine($"Test set: {testLabel.Count}");
            System.Console.WriteLine($"Training set: {trainingLabel.Count}");

            // trainiong SVM
            System.Console.WriteLine("\nTraining linear SVM ...\n");

            // SVM parameters
            double C            = .4;
            var    linearKernel = KernelHelper.LinearKernel();

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

            System.Console.WriteLine();

            // accuacy on training set
            Vector <double> prediction = SvmPredic(trainingFeatures, svc);
            double          accuracy   = CalculateAccuracy(prediction, trainingLabel);

            System.Console.WriteLine("Training set Accuracy: {0:f2}%\n", accuracy);


            // accuacy on test set
            prediction = SvmPredic(testFeatures, svc);
            accuracy   = CalculateAccuracy(prediction, testLabel);
            System.Console.WriteLine("Test set Accuracy: {0:f2}%\n", accuracy);

            // F1 score
            double f1Score = CalculateF1Score(prediction, testLabel);

            System.Console.WriteLine("F1 Score on test set: {0:f2}%\n", f1Score * 100);

            //Pause();
        }
コード例 #13
0
ファイル: C_SVC.cs プロジェクト: xwyangjshb/libsvm.net
 /// <summary>
 ///     Classification SVM
 ///     Supports multi-class classification
 /// </summary>
 /// <param name="input_file_name">Path to the training data set file. Has respect the libsvm format</param>
 /// <param name="kernel">Selected Kernel</param>
 /// <param name="C">Cost parameter </param>
 /// <param name="cache_size">Indicates the maximum memory that the program can use </param>
 /// <param name="probability">Set this parameter to true if you want to use the PredictProbabilities function</param>
 public C_SVC(string input_file_name, Kernel kernel, double C, double cache_size = 100, bool probability = false)
     : this(ProblemHelper.ReadProblem(input_file_name), kernel, C, cache_size, probability)
 {
 }
コード例 #14
0
ファイル: Program.cs プロジェクト: mlnethub/ML.Net
        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();
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: msbobh/SVM_Train
        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);
        }
コード例 #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();
        }
コード例 #17
0
        void svm()
        {
            var pro = ProblemHelper.ReadProblem("res.txt");

            model = new C_SVC(pro, KernelHelper.RadialBasisFunctionKernel(8), 32.0);
        }