FPCDecisionTree.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @file FPCDecisionTree.cpp
  3. * @brief simple decision tree classifier
  4. * @author Erik Rodner
  5. * @date 04/21/2008
  6. */
  7. #include <iostream>
  8. #include "vislearning/classifier/fpclassifier/randomforest/FPCDecisionTree.h"
  9. #include "vislearning/features/fpfeatures/HaarFeature.h"
  10. #include "vislearning/classifier/fpclassifier/randomforest/DTBStandard.h"
  11. #include "vislearning/classifier/fpclassifier/randomforest/DTBRandom.h"
  12. #include "vislearning/classifier/fpclassifier/randomforest/DTBPruning.h"
  13. using namespace OBJREC;
  14. using namespace std;
  15. using namespace NICE;
  16. FPCDecisionTree::FPCDecisionTree( const Config *_conf, std::string section ) : conf(_conf), dt(NULL)
  17. {
  18. std::string builder_method = conf->gS(section, "builder", "standard" );
  19. std::string builder_section = conf->gS(section, "builder_section" );
  20. if ( builder_method == "standard" )
  21. builder = new DTBStandard ( conf, builder_section );
  22. else if (builder_method == "random" )
  23. builder = new DTBRandom ( conf, builder_section );
  24. else {
  25. fprintf (stderr, "DecisionTreeBuilder %s not yet implemented !\n",
  26. builder_method.c_str() );
  27. exit(-1);
  28. }
  29. double minimum_entropy = conf->gD(section, "minimum_entropy", 0.0 );
  30. if ( minimum_entropy != 0.0 )
  31. builder = new DTBPruning ( _conf, section, builder );
  32. }
  33. FPCDecisionTree::~FPCDecisionTree()
  34. {
  35. delete builder;
  36. if ( dt != NULL )
  37. delete dt;
  38. }
  39. ClassificationResult FPCDecisionTree::classify ( Example & pce )
  40. {
  41. FullVector distribution;
  42. dt->traverse ( pce, distribution );
  43. int classno = distribution.maxElement();
  44. distribution.normalize();
  45. return ClassificationResult ( classno, distribution );
  46. }
  47. void FPCDecisionTree::train ( FeaturePool & fp,
  48. Examples & examples )
  49. {
  50. maxClassNo = examples.getMaxClassNo();
  51. if ( dt != NULL )
  52. delete dt;
  53. dt = new DecisionTree ( conf, maxClassNo );
  54. builder->build ( *dt, fp, examples, maxClassNo );
  55. }