KSquare Utilities
KKMLL::Classifier2 Class Reference

#include <Classifier2.h>

Public Types

typedef Classifier2Classifier2Ptr
 

Public Member Functions

 Classifier2 (TrainingProcess2Ptr _trainer, RunLog &_log)
 
virtual ~Classifier2 ()
 
bool Abort () const
 
MLClassPtr ClassifyAExample (FeatureVector &example)
 
void ClassifyAExample (FeatureVector &example, MLClassPtr &predClass1, MLClassPtr &predClass2, kkint32 &predClass1Votes, kkint32 &predClass2Votes, double &knownClassProb, double &predClass1Prob, double &predClass2Prob, kkint32 &numOfWinners, double &breakTie)
 
MLClassPtr ClassifyAExample (FeatureVector &example, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners)
 
MLClassPtr ClassifyAExample (FeatureVector &example, double &probability, kkint32 &numOfWinners, bool &knownClassOneOfTheWinners, double &breakTie)
 
const KKStrConfigRootName () const
 
std::vector< ProbNamePairFindWorstSupportVectors (FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
 For a given two class pair return the names of the 'numToFind' worst S/V's. More...
 
std::vector< ProbNamePairFindWorstSupportVectors2 (FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
 For a given two class pair return the names of the 'numToFind' worst S/V's. More...
 
virtual kkint32 MemoryConsumedEstimated () const
 
void PredictRaw (FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
 
ClassProbList const * PriorProbability () const
 Returns the distribution of the training data used to build the classifier. More...
 
void ProbabilitiesByClass (const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
 For a given feature vector return back the probabilities and votes for each class. More...
 
ClassProbListPtr ProbabilitiesByClass (FeatureVectorPtr example)
 
void ProbabilitiesByClassDual (FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results)
 
void RetrieveCrossProbTable (MLClassList &classes, double **crossProbTable)
 
SVM_SelectionMethod SelectionMethod () const
 
std::vector< KKStrSupportVectorNames (MLClassPtr c1, MLClassPtr c2)
 

Detailed Description

Definition at line 75 of file Classifier2.h.

Member Typedef Documentation

Definition at line 78 of file Classifier2.h.

Constructor & Destructor Documentation

Classifier2::Classifier2 ( TrainingProcess2Ptr  _trainer,
RunLog _log 
)

Definition at line 42 of file Classifier2.cpp.

References KKMLL::TrainingProcess2::Abort(), KKB::KKStr::Concat(), KKMLL::TrainingProcess2::Config(), KKMLL::TrainingConfiguration2::ConfigRootName(), KKMLL::TrainingProcess2::FeaturesAlreadyNormalized(), KKMLL::MLClassList::GetNoiseClass(), KKMLL::MLClassList::GetUnKnownClass(), KKB::KKException::KKException(), KKB::KKStr::KKStr(), KKMLL::TrainingProcess2::MLClasses(), KKMLL::MLClassList::MLClassList(), KKMLL::Model::ModelType(), KKMLL::Model::OldSVM, KKB::KKStr::operator=(), KKMLL::ModelOldSVM::SvmModel(), KKMLL::TrainingProcess2::TrainedModel(), and KKMLL::Model::ValidModel().

Referenced by KKMLL::ModelDual::ReadXML(), and KKMLL::ModelDual::TrainModel().

44  :
45 
46  abort (false),
47  classifierClassIndex (),
48  classClassifierIndex (),
49  featuresAlreadyNormalized (false),
50  mlClasses (NULL),
51  log (_log),
52  subClassifiers (NULL),
53  configRootName (),
54  trainedModel (NULL),
55  trainedModelOldSVM (NULL),
56  trainedModelSVMModel (NULL),
57  noiseMLClass (NULL),
58  trainingProcess (_trainer),
59  unKnownMLClass (NULL)
60 {
61  if (!_trainer)
62  {
63  log.Level (-1) << endl
64  << "Classifier2::Classifier2 ***ERROR*** (_trainer == NULL)" << endl
65  << endl;
66  throw KKException ("Classifier2::Classifier2 ***ERROR*** (_trainer == NULL)");
67  }
68 
69  if (_trainer->Abort ())
70  {
71  log.Level (-1) << endl
72  << "Classifier2::Classifier2 ***ERROR*** '_trainer' is invalid." << endl
73  << endl;
74  throw KKException ("Classifier2::Classifier2 ***ERROR*** '_trainer' is invalid.");
75  }
76 
77  if (trainingProcess->Config () != NULL)
78  configRootName = trainingProcess->Config ()->ConfigRootName ();
79 
80  featuresAlreadyNormalized = trainingProcess->FeaturesAlreadyNormalized ();
81  mlClasses = new MLClassList (*(_trainer->MLClasses ()));
82  trainedModel = _trainer->TrainedModel ();
83  if (trainedModel == NULL)
84  {
85  KKStr errMsg = "Classifier2::Classifier2 ***ERROR*** (trainedModel == NULL).";
86  log.Level (-1) << endl << endl << errMsg << endl << endl;
87  throw KKException (errMsg);
88  }
89 
90  if (!trainedModel->ValidModel ())
91  {
92  KKStr errMsg = "Classifier2::Classifier2 ***ERROR*** trainedModel is not valid.";
93  log.Level (-1) << endl << endl << errMsg << endl << endl;
94  throw KKException (errMsg);
95  }
96 
97  log.Level (20) << "Classifier2::Classifier2" << endl;
98  noiseMLClass = mlClasses->GetNoiseClass ();
99  unKnownMLClass = mlClasses->GetUnKnownClass ();
100 
101  if (trainedModel->ModelType () == Model::ModelTypes::OldSVM)
102  {
103  trainedModelOldSVM = dynamic_cast<ModelOldSVMPtr> (trainedModel);
104  if (trainedModelOldSVM)
105  trainedModelSVMModel = trainedModelOldSVM->SvmModel ();
106  }
107 
108  BuildSubClassifierIndex ();
109 }
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
MLClassPtr GetUnKnownClass()
Return a pointer to the MLClass object that represents the unknown Class in the list.
Definition: MLClass.cpp:893
MLClassPtr GetNoiseClass() const
Definition: MLClass.cpp:875
SVMModelPtr SvmModel() const
Definition: ModelOldSVM.h:92
ModelPtr TrainedModel() const
TrainingConfiguration2Const * Config()
A specialization of &#39;Model&#39;; meant to Wrap the original version of &#39;SvmModel&#39; class. It will allow us to use the original implementation using version 2.39 of LibSVM.
Definition: ModelOldSVM.h:34
RunLog & Level(kkint32 _level)
Definition: RunLog.cpp:220
MLClassListPtr MLClasses() const
void Abort(bool _abort)
bool ValidModel() const
Definition: Model.h:195
virtual ModelTypes ModelType() const =0
bool FeaturesAlreadyNormalized() const
Maintains a list of MLClass instances.
Definition: MLClass.h:233
Classifier2::~Classifier2 ( )
virtual

Definition at line 113 of file Classifier2.cpp.

114 {
115  delete mlClasses; mlClasses = NULL;
116  delete subClassifiers; subClassifiers = NULL;
117 }

Member Function Documentation

bool KKMLL::Classifier2::Abort ( ) const
inline

Definition at line 86 of file Classifier2.h.

86 {return abort;}
MLClassPtr Classifier2::ClassifyAExample ( FeatureVector example)

Definition at line 413 of file Classifier2.cpp.

References ClassifyAExample().

Referenced by KKMLL::ModelDual::Predict().

414 {
415  kkint32 numOfWinners = 0;
416  bool knownClassOneOfTheWinners = false;
417  return ClassifyAExample (example, numOfWinners, knownClassOneOfTheWinners);
418 }
__int32 kkint32
Definition: KKBaseTypes.h:88
MLClassPtr ClassifyAExample(FeatureVector &example)
void Classifier2::ClassifyAExample ( FeatureVector example,
MLClassPtr predClass1,
MLClassPtr predClass2,
kkint32 predClass1Votes,
kkint32 predClass2Votes,
double &  knownClassProb,
double &  predClass1Prob,
double &  predClass2Prob,
kkint32 numOfWinners,
double &  breakTie 
)

Definition at line 270 of file Classifier2.cpp.

References ClassifyAExample(), KKMLL::FeatureVector::MLClass(), and KKMLL::Model::Predict().

Referenced by ClassifyAExample(), and KKMLL::ModelDual::Predict().

281 {
282  bool knownClassOneOfTheWiners = false;
283 
284  predClass1 = NULL;
285  predClass2 = NULL;
286  knownClassProb = -1.0f;
287  predClass1Prob = -1.0f;
288  predClass2Prob = -1.0f;
289 
290  MLClassPtr origClass = example.MLClass ();
291 
292 
293  trainedModel->Predict (&example,
294  origClass,
295  predClass1,
296  predClass2,
297  predClass1Votes,
298  predClass2Votes,
299  knownClassProb,
300  predClass1Prob,
301  predClass2Prob,
302  numOfWinners,
303  knownClassOneOfTheWiners,
304  breakTie,
305  log
306  );
307 
308  if (!predClass1)
309  predClass1 = noiseMLClass;
310 
311 
312  if (subClassifiers)
313  {
314  Classifier2Ptr subClassifer = LookUpSubClassifietByClass (predClass1);
315  if (subClassifer)
316  {
317  MLClassPtr subPredClass1 = NULL;
318  MLClassPtr subPredClass2 = NULL;
319  kkint32 subPredClass1Votes = 0;
320  kkint32 subPredClass2Votes = 0;
321  double subKnownClassProb = 0.0;
322  double subPredClass1Prob = 0.0;
323  double subPredClass2Prob = 0.0;
324  kkint32 subNumOfWinners = 0;
325  double subBreakTie = 0.0;
326 
327  subClassifer->ClassifyAExample (example, subPredClass1, subPredClass2,
328  subPredClass1Votes, subPredClass2Votes, subKnownClassProb,
329  subPredClass1Prob, subPredClass2Prob, subNumOfWinners,
330  subBreakTie
331  );
332  predClass1 = subPredClass1;
333  predClass1Votes += subPredClass1Votes;
334  predClass1Prob *= subPredClass1Prob;
335  knownClassProb *= subKnownClassProb;
336  numOfWinners += subNumOfWinners;
337  breakTie += subBreakTie * (1.0 - breakTie);
338  }
339 
340  subClassifer = LookUpSubClassifietByClass (predClass2);
341  if (subClassifer)
342  {
343  MLClassPtr subPredClass1 = NULL;
344  MLClassPtr subPredClass2 = NULL;
345  kkint32 subPredClass1Votes = 0;
346  kkint32 subPredClass2Votes = 0;
347  double subKnownClassProb = 0.0;
348  double subPredClass1Prob = 0.0;
349  double subPredClass2Prob = 0.0;
350  kkint32 subNumOfWinners = 0;
351  double subBreakTie = 0.0;
352 
353  subClassifer->ClassifyAExample (example, subPredClass1, subPredClass2,
354  subPredClass1Votes, subPredClass2Votes, subKnownClassProb,
355  subPredClass1Prob, subPredClass2Prob, subNumOfWinners,
356  subBreakTie
357  );
358  predClass2 = subPredClass1;
359  predClass2Votes += subPredClass1Votes;
360  predClass2Prob *= subPredClass1Prob;
361  }
362  }
363 
364  example.MLClass (predClass1);
365 
366  return;
367 } /* ClassifyAExample */
__int32 kkint32
Definition: KKBaseTypes.h:88
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
void MLClass(MLClassPtr _mlClass)
Assign a class to this example.
Definition: FeatureVector.h:74
virtual MLClassPtr Predict(FeatureVectorPtr example, RunLog &log)=0
MLClassPtr ClassifyAExample(FeatureVector &example)
MLClassPtr Classifier2::ClassifyAExample ( FeatureVector example,
kkint32 numOfWinners,
bool &  knownClassOneOfTheWinners 
)

Definition at line 396 of file Classifier2.cpp.

Referenced by ClassifyAExample().

400 {
401  MLClassPtr predictedClass = NULL;
402 
403  // Lets first Normalize Feature Data.
404 
405  predictedClass = ClassifyAImageOneLevel (example, numOfWinners, knownClassOneOfTheWinners);
406 
407  return predictedClass;
408 } /* ClassifyAExample */
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
MLClassPtr Classifier2::ClassifyAExample ( FeatureVector example,
double &  probability,
kkint32 numOfWinners,
bool &  knownClassOneOfTheWinners,
double &  breakTie 
)

Definition at line 373 of file Classifier2.cpp.

379 {
380  MLClassPtr predictedClass = NULL;
381 
382  probability = 0.0;
383 
384  predictedClass = ClassifyAImageOneLevel (example,
385  probability,
386  numOfWinners,
387  knownClassOneOfTheWinners,
388  breakTie
389  );
390  return predictedClass;
391 } /* ClassifyAExample */
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
const KKStr& KKMLL::Classifier2::ConfigRootName ( ) const
inline

Definition at line 87 of file Classifier2.h.

Referenced by KKMLL::Classifier2List::LookUpByName().

87 {return configRootName;}
vector< ProbNamePair > Classifier2::FindWorstSupportVectors ( FeatureVectorPtr  example,
kkint32  numToFind,
MLClassPtr  c1,
MLClassPtr  c2 
)

For a given two class pair return the names of the 'numToFind' worst S/V's.

This method will iterate through all the S/V's removing them one at a time and re-compute the decision boundary and probability. It will then return the S/V's that when removed improve the probability in 'c1's the most.

Parameters
[in]exampleThe example that was classified incorrectly.
[in]numToFindThe number of the worst examples you are looking for.
[in]c1Class that the 'example; parameter should have been classed as.
[in]c2Class that it was classified as.

Definition at line 442 of file Classifier2.cpp.

447 {
448  if (!trainedModelSVMModel)
449  {
450  vector<ProbNamePair> results;
451  return results;
452  }
453 
454  return trainedModelSVMModel->FindWorstSupportVectors (example, numToFind, c1, c2);
455 }
std::vector< ProbNamePair > FindWorstSupportVectors(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
Definition: SVMModel.cpp:1653
vector< ProbNamePair > Classifier2::FindWorstSupportVectors2 ( FeatureVectorPtr  example,
kkint32  numToFind,
MLClassPtr  c1,
MLClassPtr  c2 
)

For a given two class pair return the names of the 'numToFind' worst S/V's.

This method will iterate through all the S/V's removing them one at a time and rebuild a new SVM then submit example for testing.

Parameters
[in]exampleThe example that was classified incorrectly.
[in]numToFindThe number of the worst examples you are looking for.
[in]c1Class that the 'example; parameter should have been classed as.
[in]c2Class that it was classified as.

Definition at line 460 of file Classifier2.cpp.

465 {
466  if (!trainedModelSVMModel)
467  {
468  vector<ProbNamePair> results;
469  return results;
470  }
471 
472  return trainedModelSVMModel->FindWorstSupportVectors2 (example, numToFind, c1, c2);
473 }
std::vector< ProbNamePair > FindWorstSupportVectors2(FeatureVectorPtr example, kkint32 numToFind, MLClassPtr c1, MLClassPtr c2)
For a given two class pair return the names of the &#39;numToFind&#39; worst S/V&#39;s.
Definition: SVMModel.cpp:1745
kkint32 Classifier2::MemoryConsumedEstimated ( ) const
virtual

Definition at line 120 of file Classifier2.cpp.

References KKMLL::MLClassList::MemoryConsumedEstimated().

Referenced by KKMLL::ModelDual::MemoryConsumedEstimated().

121 {
122  kkint32 memoryConsumedEstimated = sizeof (*this);
123  if (mlClasses) memoryConsumedEstimated += mlClasses->MemoryConsumedEstimated ();
124  return memoryConsumedEstimated;
125 } /* MemoryConsumedEstimated */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 MemoryConsumedEstimated() const
Definition: MLClass.cpp:616
void Classifier2::PredictRaw ( FeatureVectorPtr  example,
MLClassPtr predClass,
double &  dist 
)

Definition at line 478 of file Classifier2.cpp.

References PredictRaw(), and KKMLL::Model::PredictRaw().

Referenced by PredictRaw().

482 {
483  trainedModel->PredictRaw (example, predClass, dist);
484  if (subClassifiers)
485  {
486  Classifier2Ptr subClassifer = LookUpSubClassifietByClass (predClass);
487  if (subClassifer)
488  subClassifer->PredictRaw (example, predClass, dist);
489  }
490 } /* PredictRaw */
virtual void PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
Definition: Model.h:230
void PredictRaw(FeatureVectorPtr example, MLClassPtr &predClass, double &dist)
ClassProbList const * Classifier2::PriorProbability ( ) const

Returns the distribution of the training data used to build the classifier.

The caller will NOT own this list. Ownership will remain with 'trainingProcess' member of this class.

Definition at line 139 of file Classifier2.cpp.

References KKMLL::TrainingProcess2::PriorProbability().

140 {
141  if (!trainingProcess)
142  return NULL;
143 
144  return trainingProcess->PriorProbability ();
145 } /* PriorProbability */
ClassProbList const * PriorProbability() const
void Classifier2::ProbabilitiesByClass ( const MLClassList classes,
FeatureVectorPtr  example,
kkint32 votes,
double *  probabilities 
)

For a given feature vector return back the probabilities and votes for each class.

Parameters
classes[in] List of classes that we can be predicted for The ordering of 'votes' and 'probabilities' will be dictated by this list.
example[in] Feature Vector to make prediction on.
votes[out] Pointer to list of int's, must be as large as 'classes' The number of votes for each corresponding class will be stored hear.
probabilities[out] Pointer to list of double's, must be as large as 'classes' The probability for each corresponding class will be stored hear.

Definition at line 495 of file Classifier2.cpp.

References KKMLL::ClassProbList::LookUp(), ProbabilitiesByClass(), KKMLL::ClassProb::probability, and KKMLL::ClassProb::votes.

500 {
501  ClassProbListPtr predictions = ProbabilitiesByClass (example);
502 
503  kkuint32 numClasses = classes.size ();
504  for (kkuint32 x = 0; x < numClasses; ++x)
505  {
506  votes[x] = 0;
507  probabilities[x] = 0.0;
508 
509  MLClassPtr c = classes.IdxToPtr (x);
510  ClassProbPtr cp = predictions->LookUp (c);
511  if (cp)
512  {
513  votes[x] = (kkint32)(0.5f + cp->votes);
514  probabilities[x] = cp->probability;
515  }
516  }
517  delete predictions;
518  predictions= NULL;
519 } /* ProbabilitiesByClass */
__int32 kkint32
Definition: KKBaseTypes.h:88
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
EntryPtr IdxToPtr(kkuint32 idx) const
Definition: KKQueue.h:732
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
const ClassProbPtr LookUp(MLClassPtr targetClass) const
Definition: ClassProb.cpp:160
Used to record probability for a specified class; and a list of classes.
Definition: ClassProb.h:25
double probability
Definition: ClassProb.h:36
void ProbabilitiesByClass(const MLClassList &classes, FeatureVectorPtr example, kkint32 *votes, double *probabilities)
For a given feature vector return back the probabilities and votes for each class.
ClassProbListPtr Classifier2::ProbabilitiesByClass ( FeatureVectorPtr  example)

Definition at line 671 of file Classifier2.cpp.

References KKMLL::Model::ProbabilitiesByClass().

Referenced by KKMLL::ModelDual::ProbabilitiesByClass(), ProbabilitiesByClass(), KKMLL::ModelDual::ProbabilitiesByClassDual(), and KKMLL::ModelDual::ReconcileProbAndVotes().

672 {
673  if (!trainedModel)
674  return NULL;
675 
676  ClassProbListPtr results = trainedModel->ProbabilitiesByClass (example, log);
677  if (!results)
678  return NULL;
679 
680  ClassProbListPtr expandedResults = ProcessSubClassifersMethod2 (example, results);
681  delete results;
682  results = NULL;
683 
684 
685  return expandedResults;
686 } /* ProbabilitiesByClass */
virtual ClassProbListPtr ProbabilitiesByClass(FeatureVectorPtr example, RunLog &log)=0
void Classifier2::ProbabilitiesByClassDual ( FeatureVectorPtr  example,
KKStr classifier1Desc,
KKStr classifier2Desc,
ClassProbListPtr classifier1Results,
ClassProbListPtr classifier2Results 
)

Definition at line 704 of file Classifier2.cpp.

References KKMLL::Model::ProbabilitiesByClassDual().

710 {
711  if (trainedModel)
712  trainedModel->ProbabilitiesByClassDual (example, classifier1Desc, classifier2Desc, classifier1Results, classifier2Results, log);
713 }
virtual void ProbabilitiesByClassDual(FeatureVectorPtr example, KKStr &classifier1Desc, KKStr &classifier2Desc, ClassProbListPtr &classifier1Results, ClassProbListPtr &classifier2Results, RunLog &log)
Only applied to ModelDual classifier.
Definition: Model.cpp:830
void Classifier2::RetrieveCrossProbTable ( MLClassList classes,
double **  crossProbTable 
)

Definition at line 692 of file Classifier2.cpp.

References KKMLL::Model::RetrieveCrossProbTable().

Referenced by KKMLL::ModelDual::RetrieveCrossProbTable().

695 {
696  if (trainedModel)
697  trainedModel->RetrieveCrossProbTable (classes, crossProbTable, log);
698  }
virtual void RetrieveCrossProbTable(MLClassList &classes, double **crossProbTable, RunLog &log)
Definition: Model.cpp:755
SVM_SelectionMethod Classifier2::SelectionMethod ( ) const

Definition at line 129 of file Classifier2.cpp.

References KKMLL::Null, and KKMLL::ModelOldSVM::SelectionMethod().

130 {
131  if (!trainedModelOldSVM)
133  else
134  return trainedModelOldSVM->SelectionMethod ();
135 }
SVM_SelectionMethod SelectionMethod() const
vector< KKStr > Classifier2::SupportVectorNames ( MLClassPtr  c1,
MLClassPtr  c2 
)

Definition at line 423 of file Classifier2.cpp.

426 {
427  if (!trainedModelSVMModel)
428  {
429  vector<KKStr> results;
430  return results;
431  }
432  else
433  {
434  return trainedModelSVMModel->SupportVectorNames (c1, c2);
435  }
436 }
std::vector< KKStr > SupportVectorNames() const
Definition: SVMModel.cpp:1618

The documentation for this class was generated from the following files: