Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00025 #ifndef MARSYAS_OneRClassifier_H 00026 #define MARSYAS_OneRClassifier_H 00027 00028 #include <marsyas/system/MarSystem.h> 00029 #include <marsyas/WekaData.h> 00030 00031 namespace Marsyas 00032 { 00033 00034 class OneRClassifier: public MarSystem 00035 { 00036 private: 00037 00038 //This class represents one rule of the OneR algorithm. 00039 class OneRRule 00040 { 00041 public: 00042 //Construct a rule with inst count, attribute this rule represents, number of breaks and correct value 00043 OneRRule(mrs_natural attr, mrs_natural nBreaks, mrs_natural correct) 00044 { 00045 attr_ = attr; 00046 nBreaks_ = nBreaks; 00047 correct_ = correct; 00048 00049 //size the classifications and breakpoints accrding to nbreaks. 00050 //breakpoints has one less because the last entry represents infinity 00051 classifications_.resize(nBreaks_); 00052 breakpoints_.resize(nBreaks_-1); 00053 } 00054 00055 //nothing to destroy 00056 ~OneRRule() {} 00057 00058 //get the correct count from this rule 00059 mrs_natural getCorrect()const {return correct_;}; 00060 00061 //get the breakpoints and classifications vectors 00062 inline std::vector<mrs_natural>& getClassifications() {return classifications_;} 00063 inline std::vector<mrs_real>& getBreakpoints() {return breakpoints_;} 00064 00065 //get the number of breakpoints(set in ctor) 00066 inline mrs_natural getnBreaks()const {return nBreaks_;} 00067 00068 //get the attribute this rule is for. 00069 inline mrs_natural getAttr()const {return attr_;} 00070 00071 private: 00072 //mrs_natural numInst_; 00073 mrs_natural attr_; 00074 mrs_natural nBreaks_; 00075 mrs_natural correct_; 00076 00077 std::vector<mrs_natural>classifications_; 00078 std::vector<mrs_real>breakpoints_; 00079 }; 00080 00081 private: 00082 void addControls(); 00083 void myUpdate(MarControlPtr sender); 00084 00085 //this is a table of all the attribute data. It is built 00086 //when mode = "train" 00087 WekaData instances_; 00088 00089 //construct a new rule 00090 OneRRule *newRule(mrs_natural attr, mrs_natural nLabels); 00091 00092 //Predict a class. This is done when mode is "predict" 00093 mrs_natural Predict(const realvec& in); 00094 00095 //build the classifier. This is performed when the done flag goes to true. 00096 void Build(mrs_natural nLabels); 00097 00098 //The current rule during the build classifier stage 00099 OneRRule *rule_; 00100 00101 //the minimum number of buckes 00102 static const mrs_natural minBucketSize_ = 6; 00103 00104 //keeps track of the last mode found when myProcess is called. 00105 //It is used to determine when the incoming data changes from 00106 //one state to the other. 00107 //When train -> predict : Build the classifier and get ready for prediction 00108 //When predict -> train : Clear the classifier and start retraining 00109 bool lastModePredict_; 00110 mrs_realvec row_; 00111 public: 00112 OneRClassifier(const std::string name); 00113 ~OneRClassifier(); 00114 00115 MarSystem *clone() const; 00116 void myProcess(realvec& in, realvec& out); 00117 }; 00118 00119 }//namespace Marsyas 00120 #endif 00121