示例#1
0
        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());
                }
            }
        }
        /// <summary>
        /// Trains the Naive Bayes classifier using pre-computed data. This data
        /// is in the form of a List which contains key-value-pairs of intended class
        /// to a dictionary containing feature values indexed by feature names.
        /// </summary>
        /// <param name="featureNames">Names of all the features so that their values
        /// can be looked up in the dictionary</param>
        /// <param name="data">All the pre-computed feature data</param>
        public void TrainCombo(List <string> featureNames, List <KeyValuePair <ShapeType, Dictionary <string, object> > > data)
        {
            NaiveBayesUpdateable bayes = new NaiveBayesUpdateable(featureNames);

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

            bayes.UpdateClassifier();

            _comboClassifier = bayes;
        }
        /// <summary>
        /// Update the Naive Bayes classifier to learn from an example shape.
        /// </summary>
        /// <param name="shape"></param>
        public void Learn(Sketch.Shape shape)
        {
            double believedOrientation;
            Dictionary <string, object> features = GetIndRecognitionResults(shape, out believedOrientation);

            // Since the combo classifier is trained with such a large
            // number of training examples (1600+), each additional
            // learning example has an extremely small effect.
            // However, we want the error-corrected learning to have a
            // more meaningful effect. So, we add the same example
            // multiple times to the bayes classifier.

            for (int i = 0; i < 5; i++) // MAGIC NUMBER
            {
                _comboClassifier.AddExample(shape.Type, features);
            }

            _comboClassifier.UpdateClassifier();
        }