43 trainingLog.setProceedingText(
"[TRAINING DecisionStump]");
44 warningLog.setProceedingText(
"[WARNING DecisionStump]");
45 errorLog.setProceedingText(
"[ERROR DecisionStump]");
68 if( weakClassifer == NULL )
return false;
84 errorLog <<
"train(ClassificationData &trainingData, VectorDouble &weights) - There should only be 2 classes in the training data, but there are : " << trainingData.
getNumClasses() << endl;
90 errorLog <<
"train(ClassificationData &trainingData, VectorDouble &weights) - There number of examples in the training data (" << trainingData.
getNumSamples() <<
") does not match the lenght of the weights vector (" << weights.size() <<
")" << endl;
96 UINT bestFeatureIndex = 0;
97 vector< MinMax > ranges = trainingData.
getRanges();
98 double minError = numeric_limits<double>::max();
102 double threshold = 0;
103 double bestThreshold = 0;
110 minRange = ranges[n].minValue;
111 maxRange = ranges[n].maxValue;
118 for(UINT i=0; i<M; i++){
119 bool positiveClass = trainingData[ i ].getClassLabel() == WEAK_CLASSIFIER_POSITIVE_CLASS_LABEL;
120 bool rhs = trainingData[ i ][ n ] >= threshold;
121 bool lhs = trainingData[ i ][ n ] <= threshold;
122 if( (rhs && !positiveClass) || (!rhs && positiveClass) ) rhsError += weights[ i ];
123 if( (lhs && !positiveClass) || (!lhs && positiveClass) ) lhsError += weights[ i ];
127 if( rhsError < minError ){
129 bestFeatureIndex = n;
130 bestThreshold = threshold;
133 if( lhsError < minError ){
135 bestFeatureIndex = n;
136 bestThreshold = threshold;
161 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << endl;
167 file <<
"Trained: "<<
trained << endl;
172 file <<
"Direction: "<<
direction << endl;
184 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << endl;
191 if( word !=
"WeakClassifierType:" ){
192 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read WeakClassifierType header!" << endl;
198 errorLog <<
"loadModelFromFile(fstream &file) - The weakClassifierType:" << word <<
" does not match: " <<
weakClassifierType << endl;
203 if( word !=
"Trained:" ){
204 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read Trained header!" << endl;
210 if( word !=
"NumInputDimensions:" ){
211 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << endl;
217 if( word !=
"DecisionFeatureIndex:" ){
218 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read DecisionFeatureIndex header!" << endl;
224 if( word !=
"Direction:" ){
225 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read Direction header!" << endl;
231 if( word !=
"NumRandomSplits:" ){
232 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumRandomSplits header!" << endl;
238 if( word !=
"DecisionValue:" ){
239 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read DecisionValue header!" << endl;
249 cout <<
"Trained: " <<
trained;
252 cout <<
"\tDirection: " <<
direction << endl;
UINT numRandomSplits
The number of random splits used to search for the best decision spilt.
UINT getNumRandomSplits() const
DecisionStump & operator=(const DecisionStump &rhs)
UINT numInputDimensions
The number of input dimensions to the weak classifier.
virtual void print() const
virtual bool saveModelToFile(fstream &file) const
UINT getNumDimensions() const
UINT getNumSamples() const
string weakClassifierType
A string that represents the weak classifier type, e.g. DecisionStump.
UINT getNumClasses() const
static RegisterWeakClassifierModule< DecisionStump > registerModule
This is used to register the DecisionStump with the WeakClassifier base class.
virtual double predict(const VectorDouble &x)
DecisionStump(const UINT numRandomSplits=100)
UINT decisionFeatureIndex
The dimension that the data will be spilt on.
string getWeakClassifierType() const
int getRandomNumberInt(int minRange, int maxRange)
double getRandomNumberUniform(double minRange=0.0, double maxRange=1.0)
virtual bool train(ClassificationData &trainingData, VectorDouble &weights)
UINT getDirection() const
bool trained
A flag to show if the weak classifier model has been trained.
UINT getDecisionFeatureIndex() const
vector< MinMax > getRanges() const
bool copyBaseVariables(const WeakClassifier *weakClassifer)
UINT direction
Indicates if the decision spilt threshold is greater than (1), or less than (0)
virtual bool loadModelFromFile(fstream &file)
virtual bool deepCopyFrom(const WeakClassifier *weakClassifer)
This class implements a DecisionStump, which is a single node of a DecisionTree.
double decisionValue
The decision spilt threshold.
double getDecisionValue() const