Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public void DoCrossValidationTest()
        {
            var svm = new C_SVC(_prob, kernel, C);
            var cva = svm.GetCrossValidationAccuracy(5);

            Assert.IsTrue(cva > 0);
        }
Ejemplo n.º 3
0
        static void Main()
        {
            // STEP 4: Read the data
            const string dataFilePath = @"D:\MACHINE_LEARNING\SVM\Tutorial\sunnyData.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();

            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" } };
            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(""); 
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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]);
            } 
        }
Ejemplo n.º 6
0
 public void DoCrossValidationTest2()
 {
     var prob2 = ProblemHelper.ReadAndScaleProblem(SVMGUIDE1_TEST_FILE);
     var svm = new C_SVC(prob2, KernelHelper.RadialBasisFunctionKernel(3.0), 2.0);
     var cva = svm.GetCrossValidationAccuracy(5);
 }
Ejemplo n.º 7
0
        static void Main()
        {
            const string dataFilePath = @"C:\Users\Rory\Desktop\Han.csv";
            List<String> negwords = new List<String>();
            List<String> poswords = new List<String>();
            sentC = new List<String>();
            //String negFile = "C:/Users/Rory/Desktop/negative-words.txt";
            //String posFile = "C:/Users/Rory/Desktop/positive-words.txt";
            //GetNeg(negwords, negFile);
            //GetNeg(poswords, posFile);

            String[] stopwords = new String[]{"hon.","gentleman","member","friend","lady","a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount",  "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as",  "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "i","ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"};
            List<String> stop = stopwords.ToList<String>();
            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["IsPos"])).ToArray();

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

            Bigram b = new Bigram();
            List<String> v = new List<string>();

            String sent = "";

            for(int i = 0; i < x.Count; i++)
            {
                String c = x[i].ToString();

                c = c.Replace(",", "");
                c = c.ToLower();
                String[] sp = c.Split(' ');
                for (int z = 0; z < sp.Length; z++)
                {
                    String word = sp[z];
                    if(stop.Contains(word))
                    {
                        Debug.WriteLine("Stop Word");
                    }
                    else
                    {
                        sent += word;
                        sent += " ";
                    }
                }

                sent = sent.Trim();

                v.AddRange(b.getNG(sent));

                sent = "";
            }
              // int bound = v.Count();
              //v.AddRange(negwords);
              //  int nBound = v.Count();
              //v.AddRange(poswords);
              //  v = v.Distinct().ToList();
            var problemBuilder = new TextClassificationProblemBuilder();
            var problem = problemBuilder.CreateProblem(x, y, v);

            ProblemHelper.WriteProblem(@"C:\Users\Rory\Desktop\hanData.problem", problem);

            problem = ProblemHelper.ReadProblem(@"C:\Users\Rory\Desktop\hanData.problem");

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

            var accuracy = model.GetCrossValidationAccuracy(10);
            Console.WriteLine("Accuracy of the model is {0:P}", accuracy);

            string userInput;
            _predictionDictionary = new Dictionary<int, string> { { -1, "Negative" }, { 1, "Positive" } };
            getSent();
            for (int i = 0; i < nText.Count; i++)
            {
                userInput = nText[i].ToString();
                var newX = TextClassificationProblemBuilder.CreateNode(userInput, v);
                var predictedY = model.Predict(newX);

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

            addToO();
            getNumbers();
            forPeople();

            using(StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\Peop.csv"))
            {
                file.WriteLine("PersID,Name,Positive,Negative,Party");
                for(int i = 0; i < pl.Count; i++)
                {
                    String line = pl[i].getID().ToString() + "," + pl[i].getName().ToString() + "," + pl[i].getPos().ToString() + "," + pl[i].getNeg().ToString() + "," + pl[i].getParty().ToString();
                    //var json = JsonConvert.SerializeObject(pl[i]);
                    file.WriteLine(line);
                }
            }

            using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\PositiveSent.csv"))
            {
                file.WriteLine("SpeakerID,TargetID,Text,Sentiment");
                for (int i = 0; i < ps.Count; i++)
                {
                    String line = ps[i].getSID().ToString() + "," + ps[i].getAID().ToString() + "," + ps[i].getText().ToString() + "," + ps[i].getSent().ToString();
                    file.WriteLine(line);
                }
            }

            using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\NegativeSent.csv"))
            {
                file.WriteLine("SpeakerID,TargetID,Text,Sentiment");
                for (int i = 0; i < ns.Count; i++)
                {
                    String line = ns[i].getSID().ToString() + "," + ns[i].getAID().ToString() + "," + ns[i].getText().ToString() + "," + ns[i].getSent().ToString();
                    file.WriteLine(line);
                }
            }

            ns.AddRange(ps);
            doSwap();

            var json = "{\"nodes\":";
            json += JsonConvert.SerializeObject(pl);
            json += ",";

            var edge = "\"edges\":";
            edge += JsonConvert.SerializeObject(nss);
            edge += "}";

            var fullJSON = json + edge;
            Debug.Write(fullJSON);

            using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\Nodes2.JSON"))
            {
                file.Write(fullJSON);

            }

            getRels();
            List<Relationship> sl = rl.OrderBy(o=>o.source).ToList();
            var edges = "\"links\":";
            edges += JsonConvert.SerializeObject(sl);
            edges += "}";

            var fullJ = json + edges;

            using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\Rels2.JSON"))
            {
                file.Write(fullJ);

            }
        }