VCNearestNeighbour.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @file VCNearestNeighbour.cpp
  3. * @brief Simple Nearest Neighbour Implementation
  4. * @author Erik Rodner
  5. * @date 10/25/2007
  6. */
  7. #include <iostream>
  8. #include <queue>
  9. #include "vislearning/classifier/vclassifier/VCNearestNeighbour.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. #undef DEBUG_VCN
  14. VCNearestNeighbour::VCNearestNeighbour ( const Config *_conf, NICE::VectorDistance<double> *_distancefunc )
  15. : VecClassifier ( _conf ), distancefunc (_distancefunc)
  16. {
  17. K = _conf->gI("VCNearestNeighbour", "K", 1 );
  18. if ( _distancefunc == NULL )
  19. distancefunc = new EuclidianDistance<double>();
  20. }
  21. VCNearestNeighbour::VCNearestNeighbour ( const VCNearestNeighbour & src ) : VecClassifier()
  22. {
  23. if ( src.teachSet.size() )
  24. fthrow(Exception, "It is not yet possible to clone an already trained nearest neighbour classifier.");
  25. distancefunc = src.distancefunc;
  26. K = src.K;
  27. maxClassNo = src.maxClassNo;
  28. }
  29. VCNearestNeighbour::~VCNearestNeighbour()
  30. {
  31. }
  32. /** classify using simple vector */
  33. ClassificationResult VCNearestNeighbour::classify ( const NICE::Vector & x ) const
  34. {
  35. double mindist = std::numeric_limits<double>::max();
  36. int minclass = 0;
  37. FullVector mindists ( maxClassNo + 1 );
  38. mindists.set ( mindist );
  39. if ( teachSet.count() <= 0 ) {
  40. fprintf (stderr, "VCNearestNeighbour: please train this classifier before classifying\n");
  41. exit(-1);
  42. }
  43. priority_queue< pair<double, int> > examples;
  44. LOOP_ALL(teachSet)
  45. {
  46. EACH(classno,y)
  47. double distance = distancefunc->calculate ( x, y );
  48. if ( isnan(distance) )
  49. {
  50. fprintf (stderr, "VCNearestNeighbour::classify: NAN value found !!\n");
  51. cerr << x << endl;
  52. cerr << y << endl;
  53. }
  54. if ( mindists[classno] > distance )
  55. mindists[classno] = distance;
  56. if ( mindist > distance )
  57. {
  58. minclass = classno;
  59. mindist = distance;
  60. }
  61. if ( K > 1 )
  62. examples.push ( pair<double, int> ( -distance, classno ) );
  63. }
  64. if ( mindist == 0.0 )
  65. fprintf (stderr, "VCNearestNeighbour::classify WARNING distance is zero, reclassification?\n");
  66. #ifdef DEBUG_VCN
  67. for ( int i = 0 ; i < mindists.size() ; i++ )
  68. fprintf (stderr, "class %d : %f\n", i, mindists.get(i) );
  69. #endif
  70. if ( K > 1 ) {
  71. FullVector votes ( maxClassNo + 1 );
  72. votes.set(0.0);
  73. for ( int k = 0 ; k < K ; k++ )
  74. {
  75. const pair<double, int> & t = examples.top();
  76. votes[ t.second ]++;
  77. examples.pop();
  78. }
  79. votes.normalize();
  80. return ClassificationResult ( votes.maxElement(), votes );
  81. } else {
  82. for ( int i = 0 ; i < mindists.size() ; i++ )
  83. {
  84. mindists[i] = 1.0 / (mindists[i] + 1.0);
  85. }
  86. mindists.normalize();
  87. return ClassificationResult ( minclass, mindists );
  88. }
  89. }
  90. /** classify using a simple vector */
  91. void VCNearestNeighbour::teach ( const LabeledSetVector & _teachSet )
  92. {
  93. fprintf (stderr, "teach using all !\n");
  94. maxClassNo = _teachSet.getMaxClassno();
  95. teachSet = _teachSet;
  96. }
  97. void VCNearestNeighbour::teach ( int classno, const NICE::Vector & x )
  98. {
  99. fprintf (stderr, "teach!\n");
  100. for ( size_t i = 0 ; i < x.size() ; i++ )
  101. if ( isnan(x[i]) )
  102. {
  103. fprintf (stderr, "There is a NAN value in within this vector: x[%d] = %f\n", (int)i, x[i]);
  104. cerr << x << endl;
  105. exit(-1);
  106. }
  107. if ( classno > maxClassNo ) maxClassNo = classno;
  108. teachSet.add ( classno, x );
  109. }
  110. void VCNearestNeighbour::finishTeaching()
  111. {
  112. }
  113. VCNearestNeighbour *VCNearestNeighbour::clone() const
  114. {
  115. VCNearestNeighbour *myclone = new VCNearestNeighbour ( *this );
  116. return myclone;
  117. }
  118. void VCNearestNeighbour::clear ()
  119. {
  120. teachSet.clear();
  121. }
  122. void VCNearestNeighbour::store ( std::ostream & os, int format ) const
  123. {
  124. teachSet.store ( os, format );
  125. }
  126. void VCNearestNeighbour::restore ( std::istream & is, int format )
  127. {
  128. teachSet.restore ( is, format );
  129. maxClassNo = teachSet.getMaxClassno();
  130. }