Esempio n. 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);
        }
    static void Main2(string path)
    {
        // STEP 4: Read the data
        const string  dataFilePath = @"D:\texto.csv";
        var           dataTable    = DataTable.New.ReadCsv(dataFilePath);
        List <string> x            = dataTable.Rows.Select(row => row["Text"]).ToList();

        double[] y = dataTable.Rows.Select(row => double.Parse(row["IsSunny"]))
                     .ToArray();


        string texto = File.ReadAllText(path + @"/datoscsv.csv", Encoding.Default);

        List <string> x2 = new List <string>();

        double[] y2 = null;
        arreglar_dato(texto, ref x2, ref y2);

        var vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();

        var problemBuilder = new TextClassificationProblemBuilder();
        var problem        = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

        // If you want you can save this problem with :
        // ProblemHelper.WriteProblem(@"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.problem", problem);
        // And then load it again using:
        // var problem = ProblemHelper.ReadProblem(@"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.problem");

        const int C     = 1;
        var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);



        var accuracy = model.GetCrossValidationAccuracy(10);
        //  Console.Clear();
        // Console.WriteLine(new string('=', 50));
        // Console.WriteLine("Accuracy of the model is {0:P}", accuracy);
        //  model.Export(string.Format(@"D:\MACHINE_LEARNING\SVM\Tutorial\model_{0}_accuracy.model", accuracy));

        //  Console.WriteLine(new string('=', 50));
        //   Console.WriteLine("The model is trained. \r\nEnter a sentence to make a prediction. (ex: sunny rainy sunny)");
        //   Console.WriteLine(new string('=', 50));

        string userInput;

        _predictionDictionary = new Dictionary <int, string> {
            { -1, "Rainy" }, { 1, "Sunny" }
        };

        userInput = "caries";
        var newX = TextClassificationProblemBuilder.CreateNode(userInput, vocabulary);

        var predictedY = model.Predict(newX);

        //  Console.WriteLine("The prediction is {0}", _predictionDictionary[(int)predictedY]);
        //  Console.WriteLine(new string('=', 50));


        Console.WriteLine("");
    }
Esempio n. 3
0
        private SvmMethod()
        {
            var           path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files/SentimentAnalysisDataset.csv");
            List <string> x    = new List <string>();

            List <double> y = new List <double>();

            if (File.Exists(path))
            {
                var lines = File.ReadAllLines(path);
                for (int i = 0; i < 500; i++)//5146
                {
                    var lineArr = lines[i].Split(new string[] { ",Sentiment140,", ",Kaggle," }, StringSplitOptions.None);
                    y.Add(double.Parse(lineArr[0].Split(',')[1]));
                    x.Add(lineArr[1].Trim());
                }
            }

            //var dataTable = DataTable.New.ReadCsv(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files/spamdata.csv"));
            //List<string> x = dataTable.Rows.Select(row => row["Text"]).ToList();
            //double[] y = dataTable.Rows.Select(row => double.Parse(row["IsSpam"])).ToArray();

            vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y.ToArray(), vocabulary.ToList());

            const int C = 1;

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            _predictionDictionary = new Dictionary <int, string> {
                { 0, "negative" }, { 1, "positive" }
            };
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public bool Classyfi(string request)
        {
            SVMDataManager data  = new SVMDataManager();
            C_SVC          model = new C_SVC(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"model.model"));

            request = request.Replace("&", " ");
            request = request.Replace("+", " ");
            request = request.Replace("%20", " ");
            request = request.Replace("/", " ");
            request = request.Replace("?", " ");
            request = request.Replace("Filter", " ");
            request = request.Replace("Test.aspx", " ");

            var newX = SVMProblemBuilder.CreateNode(request, data.Vocabulary);
            //var predictedYProb = model.PredictProbabilities(newX);

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            var predictedY = model.Predict(newX);

            File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"log.txt"), string.Format("The prediction is {0}. Time is {1}.", _predictionDictionary[(int)predictedY], stopWatch.ElapsedMilliseconds));

            if (predictedY == -1)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Esempio n. 6
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();
        }
Esempio n. 7
0
        // Funkcja klasyfikująca
        public bool Classyfi(string request)
        {
            SVMDataManager data  = new SVMDataManager();
            C_SVC          model = new C_SVC(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"WAF\model.model"));

            // Buduje wektor na podstawie wprowadzonego tekstu
            var newX = SVMProblemBuilder.CreateVector(SVMDataManager.SeparateNonAlphanumeric(request), data.Vocabulary);
            //var predictedYProb = model.PredictProbabilities(newX);

            // Start analizy czasu na potrzeby testów
            //Stopwatch stopWatch = new Stopwatch();
            //stopWatch.Start();

            // Klasyfikacja nowo utworzonego wektora na podstawie modelu
            var predictedY = model.Predict(newX);

            // Logowanie czasu na potrzeby testów
            //File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"WAF\log.txt"), string.Format("The prediction is {0}. Time is {1}.", _predictionDictionary[(int)predictedY], stopWatch.ElapsedMilliseconds));

            // Zwracanie wyniku
            if (predictedY == -1)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Esempio n. 8
0
        public static void Main()
        {
            // STEP 4: Read the data
            string        dataFilePath = HttpContext.Current.Server.MapPath("~/DAL/svm/");
            var           dataTable    = DataTable.New.ReadCsv(dataFilePath + "Data.csv");
            List <string> x            = dataTable.Rows.Select(row => row["Text"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["Category"]))
                         .ToArray();

            vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            // If you want you can save this problem with :
            // ProblemHelper.WriteProblem(@"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.problem", problem);
            // And then load it again using:
            // var problem = ProblemHelper.ReadProblem(@"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.problem");

            const int C = 1;

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            var accuracy = model.GetCrossValidationAccuracy(10);

            Console.Clear();
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Accuracy of the model is {0:P}", accuracy);
            model.Export(string.Format(dataFilePath + "model_{0}_accuracy.model", accuracy));

            Console.WriteLine(new string('=', 50));
            Console.WriteLine("The model is trained. \r\nEnter a sentence to make a prediction. (ex: sunny rainy sunny)");
            Console.WriteLine(new string('=', 50));
        }
        public C_SVC getmodel()
        {
            List <string> x  = new List <string>();
            List <double> yb = new List <double>();

            foreach (var obj in _context.PlainTickets)
            {
                double val = -1;

                x.Add(_context.Countries.Where(ct => ct.Key == _context.Targets.
                                               Where(t => t.Key == obj.Target).FirstOrDefault().CountryName).
                      FirstOrDefault().CountryName);

                if (obj.IsSold)
                {
                    val = 1;
                }

                yb.Add(val);
            }

            double[] y = yb.ToArray();
            this.vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();

            var problem = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C = 1;

            var model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            return(model);
        }
Esempio n. 10
0
        private static double ComputeValidationError(C_SVC svc, Matrix <double> xval, Vector <double> yval)
        {
            int    m          = xval.RowCount;
            double errorCount = 0;

            for (int i = 0; i < m; i++)
            {
                svm_node n1 = new svm_node();
                n1.index = 1;
                n1.value = xval.Row(i)[0];

                svm_node n2 = new svm_node();
                n2.index = 2;
                n2.value = xval.Row(i)[1];

                double pred = svc.Predict(new [] { n1, n2 });

                if (pred != yval[i])
                {
                    errorCount++;
                }
            }


            return(errorCount / m);
        }
Esempio n. 11
0
        private static void TrainingRecordData()
        {
            var mongoClient = new MongoClient("mongodb://*****:*****@"[^a-zA-Z]", " ").Trim().ToLowerInvariant().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Where(d => d.Count() < 20).ToArray();
            var trainingDataCollection     = database.GetCollection <ProcessInfoLabeledItem>("training_data");
            var records    = trainingDataCollection.Find(Builders <ProcessInfoLabeledItem> .Filter.Empty).ToList();
            var vocabulary = records.Select(c => c.Title + " " + c.Process).SelectMany(filter).Distinct().OrderBy(str => str).ToList();

            List <string> x = records.Select(item => item.Title + " " + item.Process).ToList();

            double[] y = records.Select(item => (double)item.Category).ToArray();

            var problemBuilder = new TextClassificationProblemBuilder();

            problemBuilder.RefineText = filter;
            var problem = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C     = 1;
            var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
            var       _predictionDictionary = new Dictionary <Karma, string> {
                { Karma.Bad, "Bad" }, { Karma.Good, "Good" }, { Karma.Neutral, "Neutral" }
            };

            var newXs = database.GetCollection <AppUsageRecord>("daily_records").Find(Builders <AppUsageRecord> .Filter.Eq(f => f.Id, AppUsageRecord.GetGeneratedId(DateTime.Now))).FirstOrDefault().ActiveApps.Select(c => c.Value).Select(c => c.MainWindowTitle + " " + c.ProcessName);

            foreach (var _x in newXs)
            {
                var newX       = TextClassificationProblemBuilder.CreateNode(_x, vocabulary, problemBuilder.RefineText);
                var predictedY = model.Predict(newX);
                Console.WriteLine($"For title {_x}");
                Console.WriteLine($"The prediction is {_predictionDictionary[(Karma)predictedY]}");
            }
        }
Esempio n. 12
0
        private void TrainingData()
        {
            string        dateFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"sunnyData.csv");
            var           dataTable    = DataTable.New.ReadCsv(dateFilePath);
            List <string> x            = dataTable.Rows.Select(row => row["Text"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["IsSunny"])).ToArray();

            var vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(w => w).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C     = 1;
            var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            string userInput;
            var    _predictionDictionary = new Dictionary <int, string> {
                { -1, "Rainy" }, { 1, "Sunny" }
            };

            do
            {
                userInput = Console.ReadLine();
                var newX = TextClassificationProblemBuilder.CreateNode(userInput, vocabulary);

                var predictedY = model.Predict(newX);
                Console.WriteLine("The prediction is {0}", _predictionDictionary[(int)predictedY]);
                Console.WriteLine(new string('=', 50));
            } while (userInput != "quit");

            Console.WriteLine("");
        }
Esempio n. 13
0
        public void C_SVC_Should_predict_perfectly_XOR_dataset_with_polynomial_kernel()
        {
            // note : K(u; v) = (u  v + 1)^2 kernel is able to feet exactly the xor function
            // see http://www.doc.ic.ac.uk/~dfg/ProbabilisticInference/IDAPILecture18.pdf for more infos
            var svm = new C_SVC(xor_problem, KernelHelper.PolynomialKernel(2, 1, 1), 1);

            checkXOR(svm);
        }
Esempio n. 14
0
        public int Classify()
        {
            try
            {
                if (lengths != null)
                {
                    var path = Environment.CurrentDirectory.Remove(Environment.CurrentDirectory.Length - 20, 20);
                    path = path + "RethinopathyAnalysisModule\\bin\\Debug\\";
                    var DvCsvm = new C_SVC(System.IO.Path.Combine(path, DvC_MODEL_FILE));
                    var DvHsvm = new C_SVC(System.IO.Path.Combine(path, DvH_MODEL_FILE));
                    var HvCsvm = new C_SVC(System.IO.Path.Combine(path, HvC_MODEL_FILE));

                    svm_node[] x = new svm_node[lengths.Count];
                    for (int j = 0; j < lengths.Count; j++)
                    {
                        x[j] = new svm_node()
                        {
                            index = j, value = lengths[j]
                        };
                    }
                    double DvCresult = DvCsvm.Predict(x);
                    double DvHresult = DvCsvm.Predict(x);
                    double HvCresult = DvCsvm.Predict(x);

                    if (DvCresult == 1)
                    {
                        if (DvHresult == 1)
                        {
                            return(1);
                        }
                    }
                    else
                    {
                        if (HvCresult == -1)
                        {
                            return(0);
                        }
                        else
                        if (DvHresult == -1)
                        {
                            return(2);
                        }
                    }
                    return(-1);
                }
                else
                {
                    return(-1);
                }
            }
            catch
            {
                return(-1);
            }
        }
Esempio n. 15
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);
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            // STEP 4: Read the data
            const string  dataFilePath = @"spamdata.csv";
            var           dataTable    = DataTable.New.ReadCsv(dataFilePath);
            List <string> x            = dataTable.Rows.Select(row => row["Text"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["IsSpam"])).ToArray();

            var vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            // If you want you can save this problem with :
            // ProblemHelper.WriteProblem(@"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.problem", problem);
            // And then load it again using:
            // var problem = ProblemHelper.ReadProblem(@"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.problem");

            const int C     = 1;
            var       model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            var accuracy = model.GetCrossValidationAccuracy(10);

            Console.Clear();
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("Accuracy of the model is {0:P}", accuracy);
            model.Export(string.Format(@"model_{0}_accuracy.model", accuracy));

            Console.WriteLine(new string('=', 50));
            Console.WriteLine("The model is trained. \r\nEnter a sentence to make a prediction. (ex: love hate dong)");
            Console.WriteLine(new string('=', 50));

            string userInput;

            //This just takes the predicted value (-1 to 3) and translates to your categorization response

            _predictionDictionary = new Dictionary <int, string> {
                { -2, "Angry" }, { -1, "Sad" }, { 0, "Normal" }, { 1, "Happy" }, { 2, "Love" }
            };


            do
            {
                userInput = Console.ReadLine();
                var newX = TextClassificationProblemBuilder.CreateNode(userInput, vocabulary);

                var predictedY = model.Predict(newX);
                Console.WriteLine("The prediction is {0}  value is {1} ", _predictionDictionary[(int)predictedY], predictedY);
                Console.WriteLine(new string('=', 50));
            } while (userInput != "quit");

            Console.WriteLine("");
        }
Esempio n. 17
0
        public void Create_Train_SVMmodel(string path_dataCSV_trainning, double C)
        {
            var           dataTable = DataAccess.DataTable.New.ReadCsv(path_dataCSV_trainning);
            List <string> x         = dataTable.Rows.Select(row => row["text"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["class"])).ToArray();

            vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();
            var problemBuilder = new TextClassificationProblemBuilder();
            var problem        = problemBuilder.CreateProblem(x, y.ToArray(), vocabulary.ToList());

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
        }
Esempio n. 18
0
        public static IModelLikelihood <double, int> Read(BinaryReader reader)
        {
            byte[] bytes          = reader.ReadByteArray1D();
            string temp_file_name = Path.GetTempFileName();

            File.WriteAllBytes(temp_file_name, bytes); //Super HAXX
            C_SVC svm = new C_SVC(temp_file_name);

            File.Delete(temp_file_name);

            IDataContext data_context = DataSet.DataContext.Read(reader);

            return(new ModelLibSVMCSVC(data_context, svm));
        }
Esempio n. 19
0
        public static void Train()
        {
            DataHandler.ImportReviewData(3);
            var x = DataHandler.Reviews.Select(r => r.reviewText);

            double[] y = DataHandler.Reviews.Select(r => r.overall).ToArray();


            var       problemBuilder = new TextClassificationProblemBuilder();
            var       problem        = problemBuilder.CreateProblem(x, y, DataHandler.Vocabulary);
            const int C = 1;

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
        }
Esempio n. 20
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);
        }
Esempio n. 21
0
        public void Train()
        {
            SVMDataManager data = new SVMDataManager();

            var problemBuilder = new SVMProblemBuilder();
            var problem        = problemBuilder.CreateProblem(data.RequestText, data.ClassValue, data.Vocabulary.ToList());

            const double C     = 0.5;
            C_SVC        model = new C_SVC(problem, KernelHelper.LinearKernel(), C); // Train is called automatically here

            accuracy = model.GetCrossValidationAccuracy(100);

            model.Export(string.Format(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format(@"bin\model_{0}_accuracy.model", accuracy))));
            System.IO.File.WriteAllLines(string.Format(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format(@"bin\model_{0}_vocabulary.txt", accuracy))), data.Vocabulary);
        }
Esempio n. 22
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();
        }
Esempio n. 23
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);
            }
        }
        public string svn(C_SVC model, string country_name)
        {
            var accuracy = model.GetCrossValidationAccuracy(10);

            var _predictionDictionary = new Dictionary <int, string> {
                { -1, "no" }, { 1, "yes" }
            };



            var newX = TextClassificationProblemBuilder.CreateNode(country_name, vocabulary);

            var predictedY = model.Predict(newX);

            return(_predictionDictionary[(int)predictedY]);
            //Debug.WriteLine("The prediction is " + _predictionDictionary[(int)predictedY]);
        }
Esempio n. 25
0
        private static double GetSVMAccuracy(svm_problem trainData, svm_problem testData, Kernel kernel, double c)
        {
            double accuracy = 0;
            var    svm      = new C_SVC(trainData, kernel, c);

            for (int i = 0; i < testData.l; i++)
            {
                var x          = testData.x[i];
                var y          = testData.y[i];
                var predictedY = svm.Predict(x);
                if (y == predictedY)
                {
                    accuracy++;
                }
            }

            return(accuracy / testData.l);
        }
Esempio n. 26
0
        public bool buildSVMCorpus(string filename)
        {
            string trainDataPath = filename + "SimpleScaledTrainSVM.txt";

            if (File.Exists(trainDataPath))
            {
                _prob         = ProblemHelper.ReadAndScaleProblem(trainDataPath);
                svm           = new C_SVC(_prob, KernelHelper.LinearKernel(), C);
                fileExistance = true;

                var      reader = new StreamReader(File.OpenRead(filename + "MinMax.txt"));
                string[] minMax = reader.ReadLine().Split(',');
                scale.min = Convert.ToDouble(minMax[0]);
                scale.max = Convert.ToDouble(minMax[1]);
            }

            return(fileExistance);
        }
Esempio n. 27
0
        private RestuarantRecomandationByNLP()
        {
            string        dataFilePath = HttpContext.Current.Server.MapPath("~/App_Data/TrainingForIsPositiveAlgo.csv");
            var           dataTable    = DataAccess.DataTable.New.ReadCsv(dataFilePath);
            List <string> x            = dataTable.Rows.Select(row => row["Text"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["IsPositive"]))
                         .ToArray();
            vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();
            var       problemBuilder = new TextClassificationProblemBuilder();
            var       problem        = problemBuilder.CreateProblem(x, y, vocabulary.ToList());
            const int C = 1;

            model = new C_SVC(problem, KernelHelper.LinearKernel(), C);
            _predictionDictionary = new Dictionary <int, string> {
                { -1, "Bad" }, { 1, "Good" }
            };
        }
Esempio n. 28
0
        public bool FindMoodMethod(string g)

        {
            string dataFilePath = Server.MapPath("~/MoodCsv/GenreList.txt");


            var dataTable = DataTable.New.ReadCsv(dataFilePath);

            List <string> x = dataTable.Rows.Select(row => row["Genre"]).ToList();

            double[] y = dataTable.Rows.Select(row => double.Parse(row["Mood"])).ToArray();

            var vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();

            var problemBuilder = new TextClassificationProblemBuilder();

            var problem = problemBuilder.CreateProblem(x, y, vocabulary.ToList());

            const int C = 1;

            var model = new C_SVC(problem, KernelHelper.LinearKernel(), C);

            string GenreId = g;

            Dictionary <int, string> _predictionDictionary = new Dictionary <int, string> {
                { -2, "Scared" }, { -1, "Sad" }, { 1, "Laugh" }, { 2, "Romance" }
            };

            //maybe add do,while here
            //GenreId = movie.with_genres;
            var newX = TextClassificationProblemBuilder.CreateNode(GenreId, vocabulary);

            var predictedY = model.Predict(newX);

            if (predictedY == -2 || predictedY == -1 || predictedY == 1 || predictedY == 2)
            {
                return(true);
            }
            else
            {
                return(false);
            }
            // ViewBag.Mood = _predictionDictionary[-2];
        }
Esempio n. 29
0
        private static void PlotBoundary(Matrix <double> x, C_SVC svc)
        {
            double min = x.Column(0).Min();
            double max = x.Column(0).Max();

            double[] x0 = MathNet.Numerics.Generate.LinearSpaced(100, min, max);

            min = x.Column(1).Min();
            max = x.Column(1).Max();

            double[] x1 = MathNet.Numerics.Generate.LinearSpaced(100, min, max);

            int size = x0.Length * x1.Length;

            double[] sx = new double[size];
            double[] sy = new double[size];
            double[] sz = new double[size];

            int idx = 0;

            for (int i = 0; i < x0.Length; i++)
            {
                for (int j = 0; j < x1.Length; j++)
                {
                    sx[idx] = x0[i];
                    sy[idx] = x1[j];

                    svm_node n1 = new svm_node();
                    n1.index = 1;
                    n1.value = x0[i];

                    svm_node n2 = new svm_node();
                    n2.index = 2;
                    n2.value = x1[j];

                    double z = svc.Predict(new [] { n1, n2 });
                    sz[idx] = z;
                    idx++;
                }
            }

            GnuPlot.Set("cntrparam levels discrete 0.5");
            GnuPlot.Contour(sx, sy, sz, "title \"Decision Boundary\"");
        }
Esempio n. 30
0
        public void C_SVC_should_always_return_the_same_cross_validation_accuracy_when_probability_is_false()
        {
            // Arrange
            var problem = CreateSimpleProblem();
            var model   = new C_SVC(problem, KernelHelper.LinearKernel(), 1);

            // Act
            var results = new double[10];

            for (int i = 0; i < 10; i++)
            {
                results[i] = model.GetCrossValidationAccuracy(10);
            }

            //Assert
            for (int i = 1; i < 10; i++)
            {
                Assert.AreEqual(0.90909090909090906, results[i]);
            }
        }