VCNearestNeighbour.cpp 3.9 KB

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