VCLogisticRegression.cpp 2.5 KB

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