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> /// Constructor which takes in pre-trained recognizers, as well /// as a pre-trained Classifier (Naive Bayes) /// </summary> /// <param name="rubine">Pre-Trained Rubine Recognizer</param> /// <param name="dollar">Pre-Trained Dollar Recognizer</param> /// <param name="zernike">Pre-Trained Zernike Recognizer</param> /// <param name="image">Pre-Trained Image Recognizer</param> /// <param name="classifier">Pre-Trained NaiveBayes Classifier</param> public ComboRecognizer(RubineRecognizer rubine, DollarRecognizer dollar, ZernikeMomentRecognizer zernike, ImageRecognizer image, NaiveBayesUpdateable classifier) { _rubine = rubine; _dollar = dollar; _zernike = zernike; _image = image; _comboClassifier = classifier; }
/// <summary> /// Deserialization Constructor /// </summary> /// <param name="info"></param> /// <param name="ctxt"></param> public ComboRecognizer(SerializationInfo info, StreamingContext ctxt) { //Get the values from info and assign them to the appropriate properties _dollar = (DollarRecognizer)info.GetValue("dollar", typeof(DollarRecognizer)); _image = (ImageRecognizer)info.GetValue("image", typeof(ImageRecognizer)); _rubine = (RubineRecognizer)info.GetValue("rubine", typeof(RubineRecognizer)); _zernike = (ZernikeMomentRecognizer)info.GetValue("zernike", typeof(ZernikeMomentRecognizer)); _comboClassifier = (NaiveBayesUpdateable)info.GetValue("comboClassifier", typeof(NaiveBayesUpdateable)); }
/// <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; }