FPCDecisionTree.cpp 2.0 KB

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