static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Provide training examples and a test set");
                return;
            }

            List <string> features;
            List <KeyValuePair <string, Dictionary <string, object> > > examples = GetExamples(args[0], out features);

            NaiveBayesUpdateable bayes = new NaiveBayesUpdateable(features);

            foreach (KeyValuePair <string, Dictionary <string, object> > example in examples)
            {
                bayes.AddExample(example.Key, example.Value);
            }

            bayes.UpdateClassifier();
            NaiveBayes classifier = bayes.Classifier;

            List <KeyValuePair <string, Dictionary <string, object> > > testCases = GetExamples(args[1], out features);

            foreach (KeyValuePair <string, Dictionary <string, object> > pair in testCases)
            {
                string className = pair.Key;
                Dictionary <string, double> results = classifier.Classify(pair.Value);
                Console.WriteLine();
                Console.WriteLine("Actual Class: " + className);
                foreach (KeyValuePair <string, double> result in results)
                {
                    Console.WriteLine("    " + result.Key + ": " + result.Value.ToString());
                }
            }
        }
Exemple #2
0
        protected void btnOut_Click(object sender, EventArgs e)
        {
            double Symptom = double.Parse(ddlSymptom.SelectedValue);
            double Job     = double.Parse(ddlJob.SelectedValue);

            /*
             * 打喷嚏--1
             * 头痛--2
             *
             * 护士--1
             * 农夫--2
             * 建筑工人--3
             * 教师--4
             */
            //training data
            DataTable table = new DataTable();

            table.Columns.Add("disease");
            table.Columns.Add("symptom", typeof(double));
            table.Columns.Add("job", typeof(double));

            table.Rows.Add("感冒", 1, 1);
            table.Rows.Add("过敏", 1, 2);
            table.Rows.Add("脑震荡", 2, 3);
            table.Rows.Add("感冒", 2, 3);
            table.Rows.Add("感冒", 1, 4);
            table.Rows.Add("脑震荡", 2, 4);
            NaiveBayes classifier = new NaiveBayes();

            classifier.TrainClassifier(table);
            Response.Write(classifier.Classify(new double[] { Symptom, Job }));//新来了打喷嚏的建筑工人,问可能得什么病
        }
        public void TestParser(string path)
        {
            var parser = new Parser();

            var cases = parser.GetCases(UsersXml, path);

            Func <Case, Document> caseTransform =
                i =>
                i.ToDoc(@case => @case.Area,
                        @case => @case.Title);

            #region Event Text comment
            //@case.Events.Aggregate("", (acc, bug) => acc + " " + Regex.Replace(bug.Text, Regex.Escape("[") + "code" + Regex.Escape("]") + ".*" + Regex.Escape("[") + "/code" + Regex.Escape("]"), "")));
            #endregion

            var total = cases.Count();

            var trainingSet = cases.Take((int)(total * (3.0 / 4))).Select(caseTransform).ToList();

            var validationSet = cases.Skip((int)(total * (3.0 / 4))).Select(caseTransform).ToList();

            var trainedData = NaiveBayes.TrainBayes(trainingSet);

            var successRate = 0.0;

            foreach (var @case in validationSet)
            {
                if (NaiveBayes.Classify(@case, trainedData).Name == @case.Class.Name)
                {
                    successRate++;
                }
            }

            successRate = successRate / validationSet.Count();

            foreach (var type in trainedData.Probabilities)
            {
                Console.WriteLine(type.Class.Name);
                Console.WriteLine("--------------------");

                type.Top(10).ForEach(i => Console.WriteLine("[{1:0.00}] {0}", i.Word, i.Probability));

                Console.WriteLine();
                Console.WriteLine();
            }

            Console.WriteLine("Prediction success rate is {0:0.00}%", successRate * 100);
        }
 private void classifyButton_Click(object sender, RoutedEventArgs e)
 {
     MessageBox.Show(string.Format("Classified as '{0}'", classifier.Classify(textBoxDescription.Text)));
 }
Exemple #5
0
 public void calculate()
 {
     result.text = b.Classify(classType);
 }