VCLogisticRegression.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file VCLogisticRegression.cpp
  3. * @brief Logistics Regression
  4. * @author Erik Rodner
  5. * @date 08/03/2009
  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/vclassifier/VCLogisticRegression.h"
  14. #include "vislearning/math/fit/FitSigmoid.h"
  15. using namespace OBJREC;
  16. using namespace std;
  17. using namespace NICE;
  18. VCLogisticRegression::VCLogisticRegression()
  19. {
  20. mlestimation = false;
  21. sigmoidA = 1.0;
  22. sigmoidB = 0.0;
  23. }
  24. VCLogisticRegression::VCLogisticRegression( const Config *conf )
  25. {
  26. mlestimation = conf->gB("VCLogisticRegression", "mlestimation", false );
  27. sigmoidA = 1.0;
  28. sigmoidB = 0.0;
  29. }
  30. VCLogisticRegression::~VCLogisticRegression()
  31. {
  32. clear();
  33. }
  34. /** classify using simple vector */
  35. ClassificationResult VCLogisticRegression::classify ( const NICE::Vector & x ) const
  36. {
  37. if ( x.size() != 1 )
  38. fthrow( Exception, "VCLogisticRegression: this classifier is only suitable for one dimensional feature vectors\n" );
  39. double sigmoidValue = 1.0 / ( 1.0 + exp(sigmoidA * x[0] + sigmoidB ) );
  40. FullVector scores ( 2 );
  41. scores.set ( 0.0 );
  42. scores[1] = sigmoidValue;
  43. scores[0] = 1.0 - sigmoidValue;
  44. return ClassificationResult ( scores.maxElement(), scores );
  45. }
  46. /** classify using a simple vector */
  47. void VCLogisticRegression::teach ( const LabeledSetVector & _teachSet )
  48. {
  49. maxClassNo = _teachSet.getMaxClassno();
  50. if ( (_teachSet.size() != 2) || (maxClassNo != 1) )
  51. fthrow ( Exception, "VCLogisticRegression: the training set is not correctly labeled with 0/1" );
  52. std::vector < pair< int, double > > results;
  53. LOOP_ALL(_teachSet)
  54. {
  55. EACH(classno,x);
  56. if ( x.size() != 1 )
  57. fthrow( Exception, "VCLogisticRegression: this classifier is only suitable for one dimensional feature vectors\n" );
  58. int yi = classno;
  59. results.push_back ( pair<int, double> ( yi, x[0] ) );
  60. }
  61. FitSigmoid::fitProbabilities ( results, sigmoidA, sigmoidB, mlestimation );
  62. }
  63. void VCLogisticRegression::clear ()
  64. {
  65. }
  66. void VCLogisticRegression::store ( std::ostream & os, int format ) const
  67. {
  68. fthrow ( Exception, "Persistent interface not yet implemented !" );
  69. }
  70. void VCLogisticRegression::restore ( std::istream & is, int format )
  71. {
  72. fthrow ( Exception, "Persistent interface not yet implemented !" );
  73. }
  74. VCLogisticRegression *VCLogisticRegression::clone(void) const
  75. {
  76. VCLogisticRegression *classifier = new VCLogisticRegression();
  77. classifier->mlestimation = this->mlestimation;
  78. classifier->sigmoidA = this->sigmoidA;
  79. classifier->sigmoidB = this->sigmoidB;
  80. return classifier;
  81. }