21 #define GRT_DLL_EXPORTS 27 UINT Classifier::numClassifierInstances = 0;
30 Classifier* Classifier::createInstanceFromString(
const std::string &
id) {
return create(
id); }
36 StringClassifierMap::iterator iter =
getMap()->find(
id );
37 if( iter ==
getMap()->end() ){
42 return iter->second();
53 if( newInstance == NULL )
return NULL;
69 StringClassifierMap::iterator iter =
getMap()->begin();
70 while( iter !=
getMap()->end() ){
71 registeredClassifiers.push_back( iter->first );
74 return registeredClassifiers;
79 classifierMode = STANDARD_CLASSIFIER_MODE;
80 supportsNullRejection =
false;
81 useNullRejection =
false;
82 numInputDimensions = 0;
83 numOutputDimensions = 0;
85 predictedClassLabel = 0;
89 trainingSetAccuracy = 0;
90 nullRejectionCoeff = 5;
91 numClassifierInstances++;
95 if( --numClassifierInstances == 0 ){
96 delete stringClassifierMap;
97 stringClassifierMap = NULL;
103 if( classifier == NULL ){
104 errorLog <<
"copyBaseVariables(const Classifier *classifier) - Classifier is NULL!" << std::endl;
112 this->classifierMode = classifier->classifierMode;
113 this->supportsNullRejection = classifier->supportsNullRejection;
114 this->useNullRejection = classifier->useNullRejection;
115 this->numClasses = classifier->numClasses;
116 this->predictedClassLabel = classifier->predictedClassLabel;
117 this->classifierMode = classifier->classifierMode;
118 this->nullRejectionCoeff = classifier->nullRejectionCoeff;
119 this->maxLikelihood = classifier->maxLikelihood;
120 this->bestDistance = classifier->bestDistance;
121 this->phase = classifier->phase;
122 this->trainingSetAccuracy = classifier->trainingSetAccuracy;
123 this->classLabels = classifier->classLabels;
124 this->classLikelihoods = classifier->classLikelihoods;
125 this->classDistances = classifier->classDistances;
126 this->nullRejectionThresholds = classifier->nullRejectionThresholds;
127 this->ranges = classifier->ranges;
138 predictedClassLabel = GRT_DEFAULT_NULL_CLASS_LABEL;
143 classLikelihoods.clear();
144 classDistances.clear();
145 classLikelihoods.
resize(numClasses,0);
146 classDistances.
resize(numClasses,0);
157 predictedClassLabel = GRT_DEFAULT_NULL_CLASS_LABEL;
161 trainingSetAccuracy = 0;
162 classLikelihoods.clear();
163 classDistances.clear();
164 nullRejectionThresholds.clear();
180 return supportsNullRejection;
184 return useNullRejection;
188 return nullRejectionCoeff;
192 if( trained )
return maxLikelihood;
201 return trainingSetAccuracy;
205 if( trained )
return bestDistance;
206 return DEFAULT_NULL_DISTANCE_VALUE;
214 for(UINT i=0; i<classLabels.size(); i++){
215 if( classLabel == classLabels[i] )
222 if( trained )
return predictedClassLabel;
227 if( trained )
return classLikelihoods;
232 if( trained )
return classDistances;
237 if( trained )
return nullRejectionThresholds;
250 this->useNullRejection = useNullRejection;
255 if( nullRejectionCoeff > 0 ){
256 this->nullRejectionCoeff = nullRejectionCoeff;
264 nullRejectionThresholds = newRejectionThresholds;
276 if( !file.is_open() ){
277 errorLog <<
"saveBaseSettingsToFile(fstream &file) - The file is not open!" << std::endl;
283 file <<
"UseNullRejection: " << useNullRejection << std::endl;
284 file <<
"ClassifierMode: " << classifierMode << std::endl;
285 file <<
"NullRejectionCoeff: " << nullRejectionCoeff << std::endl;
289 file <<
"NumClasses: " << numClasses << std::endl;
291 file <<
"NullRejectionThresholds: ";
292 if (useNullRejection && nullRejectionThresholds.size()){
293 for(UINT i=0; i<nullRejectionThresholds.size(); i++){
294 file <<
" " << nullRejectionThresholds[i];
298 for(UINT i=0; i<numClasses; i++){
304 file <<
"ClassLabels: ";
305 for(UINT i=0; i<classLabels.size(); i++){
306 file <<
" " << classLabels[i];
311 file <<
"Ranges: " << std::endl;
312 for(UINT i=0; i<ranges.size(); i++){
313 file << ranges[i].minValue <<
"\t" << ranges[i].maxValue << std::endl;
323 if( !file.is_open() ){
324 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
337 if( word !=
"UseNullRejection:" ){
338 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read UseNullRejection header!" << std::endl;
342 file >> useNullRejection;
346 if( word !=
"ClassifierMode:" ){
347 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read ClassifierMode header!" << std::endl;
351 file >> classifierMode;
355 if( word !=
"NullRejectionCoeff:" ){
356 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read NullRejectionCoeff header!" << std::endl;
360 file >> nullRejectionCoeff;
367 if( word !=
"NumClasses:" ){
368 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read NumClasses header!" << std::endl;
376 if( word !=
"NullRejectionThresholds:" ){
377 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read NullRejectionThresholds header!" << std::endl;
381 nullRejectionThresholds.
resize(numClasses);
382 for(UINT i=0; i<nullRejectionThresholds.size(); i++){
383 file >> nullRejectionThresholds[i];
388 if( word !=
"ClassLabels:" ){
389 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read ClassLabels header!" << std::endl;
393 classLabels.
resize( numClasses );
394 for(UINT i=0; i<classLabels.size(); i++){
395 file >> classLabels[i];
401 if( word !=
"Ranges:" ){
402 errorLog <<
"loadBaseSettingsFromFile(fstream &file) - Failed to read Ranges header!" << std::endl;
406 ranges.
resize(numInputDimensions);
408 for(UINT i=0; i<ranges.size(); i++){
409 file >> ranges[i].minValue;
410 file >> ranges[i].maxValue;
bool saveBaseSettingsToFile(std::fstream &file) const
bool saveBaseSettingsToFile(std::fstream &file) const
std::string getId() const
#define DEFAULT_NULL_LIKELIHOOD_VALUE
Classifier * deepCopy() const
Vector< UINT > getClassLabels() const
Vector< MinMax > getRanges() const
VectorFloat getNullRejectionThresholds() const
Classifier(const std::string &classifierId="")
std::string getClassifierType() const
virtual bool setNullRejectionCoeff(const Float nullRejectionCoeff)
virtual UINT getNumClasses() const
virtual bool resize(const unsigned int size)
const Classifier * getClassifierPointer() const
static StringClassifierMap * getMap()
Float getNullRejectionCoeff() const
virtual bool computeAccuracy(const ClassificationData &data, Float &accuracy)
virtual bool deepCopyFrom(const Classifier *classifier)
bool getSupportsNullRejection() const
bool getNullRejectionEnabled() const
UINT getPredictedClassLabel() const
bool copyMLBaseVariables(const MLBase *mlBase)
Float getBestDistance() const
bool copyBaseVariables(const Classifier *classifier)
virtual ~Classifier(void)
bool loadBaseSettingsFromFile(std::fstream &file)
Float getMaximumLikelihood() const
UINT getClassLabelIndexValue(const UINT classLabel) const
bool loadBaseSettingsFromFile(std::fstream &file)
VectorFloat getClassDistances() const
Float getTrainingSetAccuracy() const
static Vector< std::string > getRegisteredClassifiers()
VectorFloat getClassLikelihoods() const
std::map< std::string, Classifier *(*)() > StringClassifierMap
bool enableNullRejection(const bool useNullRejection)
virtual bool setNullRejectionThresholds(const VectorFloat &newRejectionThresholds)
Classifier * create() const
This is the main base class that all GRT machine learning algorithms should inherit from...
This is the main base class that all GRT Classification algorithms should inherit from...
static bool computeAccuracy(Classifier &model, const ClassificationData &data, Float &accuracy)
const Classifier & getBaseClassifier() const