コード例 #1
0
ファイル: Prediction.cs プロジェクト: zbxzc35/Adaboost
 public static double Predict(Model model, Node[] vx)
 {
     if (model.NumberOfClasses > 2)
     {
         int labelIndex = 0;
         int count = 0;
         for (int i = 0; i < model.NumberOfClasses; i++)
         {
             if ( !double.IsNaN(model.BinaryClassifiers[i].Classify(vx)) )
             {
                 labelIndex = i;
                 count++;
             }
         }
         if (count == 1)
         {
             return model.BinaryClassifiers[labelIndex].PosLabel;
         }
         else
             return double.NaN;
     }
     else if (model.NumberOfClasses == 2)
     {
         return model.BinaryClassifiers[0].Classify(vx);
     }
     else
         return double.NaN;;
 }
コード例 #2
0
ファイル: Training.cs プロジェクト: zbxzc35/Adaboost
 public static Model Train(Problem prob, TrainingArg arg)
 {
     List<double> labels = GetLabels(prob);
     if (labels.Count > 2)
     {
         List<BinaryClassifier> classifiers = new List<BinaryClassifier>(labels.Count);
         foreach (double label in labels)
         {
             Console.WriteLine("{0} vs all:",label);
             BinaryClassifier classifier = BinaryClassifier.Train(prob, arg, label);
             classifiers.Add(classifier);
             Console.WriteLine("finish");
         }
         Model model = new Model();
         model.NumberOfClasses = classifiers.Count;
         model.BinaryClassifiers = classifiers.ToArray();
         return model;
     }
     else if (labels.Count == 2)
     {
         BinaryClassifier[] classifiers = new BinaryClassifier[1];
         classifiers[0] = BinaryClassifier.Train(prob, arg, labels[0], labels[1]);
         Model model = new Model();
         model.NumberOfClasses = 2;
         model.BinaryClassifiers = classifiers;
         return model;
     }
     else
         throw new Exception(Messege.CouldNotClassify);
 }
コード例 #3
0
ファイル: Model.cs プロジェクト: zbxzc35/Adaboost
        /// <summary>
        /// Reads a Model from the provided stream.
        /// </summary>
        /// <param name="stream">The stream from which to read the Model.</param>
        /// <returns>the Model</returns>
        public static Model Read(Stream stream)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(stream);
            Model model = new Model();
            XmlNode modelNode = xmldoc.FirstChild;
            XmlNodeList classifyNodes = modelNode.ChildNodes;
            model.NumberOfClasses = int.Parse(modelNode.Attributes["NumberOfClasses"].Value);
            if (    (model.NumberOfClasses == 2 && classifyNodes.Count != 1) &&
                    (classifyNodes.Count != model.NumberOfClasses) )
                throw new Exception(Messege.ReadModelFail);

            model.BinaryClassifiers = new BinaryClassifier[classifyNodes.Count];
            for (int i = 0; i < classifyNodes.Count; i++ )
            {
                XmlElement binaryClassifierNode = (XmlElement)classifyNodes.Item(i);
                BinaryClassifier binaryClassifer = BinaryClassifier.DeserializeFromXML(binaryClassifierNode);
                model.BinaryClassifiers[i] = binaryClassifer;
            }
            return model;
        }
コード例 #4
0
ファイル: Model.cs プロジェクト: zbxzc35/Adaboost
 /// <summary>
 /// Reads a Model from the provided file.
 /// </summary>
 /// <param name="filename">The name of the file containing the Model</param>
 /// <returns>the Model</returns>
 public static void Write(string filename, Model model)
 {
     FileStream stream = File.Open(filename, FileMode.Create);
     try
     {
         Write(stream, model);
     }
     finally
     {
         stream.Close();
     }
 }
コード例 #5
0
ファイル: Model.cs プロジェクト: zbxzc35/Adaboost
        /// <summary>
        /// Number of support vectors per class.
        /// </summary>
        public static void Write(Stream stream, Model model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //XmlNode declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //xmlDoc.AppendChild(declaration);
            XmlElement modelRoot = xmlDoc.CreateElement("model");
            modelRoot.SetAttribute("NumberOfClasses", model.NumberOfClasses.ToString());
            xmlDoc.AppendChild(modelRoot);

            for(int i = 0; i<model.BinaryClassifiers.Length; i++)
            {
                XmlElement binaryClassifierNode = xmlDoc.CreateElement("BinaryClassifier");
                model.BinaryClassifiers[i].SerializeToXml(ref binaryClassifierNode);
                modelRoot.AppendChild(binaryClassifierNode);
            }
            xmlDoc.Save(stream);
        }