RegKNN.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file RegKNN.cpp
  3. * @brief Implementation of k-Nearest-Neighbor algorithm for regression purposes
  4. * @author Frank Prüfer
  5. * @date 08/29/2013
  6. */
  7. #ifdef NICE_USELIB_OPENMP
  8. #include <omp.h>
  9. #endif
  10. #include <iostream>
  11. #include "vislearning/regression/npregression/RegKNN.h"
  12. #include "vislearning/math/mathbase/FullVector.h"
  13. using namespace OBJREC;
  14. using namespace std;
  15. using namespace NICE;
  16. RegKNN::RegKNN ( const Config *_conf, NICE::VectorDistance<double> *_distancefunc ) : distancefunc (_distancefunc)
  17. {
  18. K = _conf->gI("RegKNN", "K", 1 );
  19. if ( _distancefunc == NULL )
  20. distancefunc = new EuclidianDistance<double>();
  21. }
  22. RegKNN::RegKNN ( const RegKNN & src ) : RegressionAlgorithm ( src )
  23. {
  24. dataSet = src.dataSet;
  25. labelSet = src.labelSet;
  26. distancefunc = src.distancefunc;
  27. K = src.K;
  28. }
  29. RegKNN::~RegKNN ()
  30. {
  31. }
  32. RegKNN* RegKNN::clone ( void ) const
  33. {
  34. return new RegKNN(*this);
  35. }
  36. void RegKNN::teach ( const NICE::VVector & _dataSet, const NICE::Vector & _labelSet)
  37. {
  38. fprintf (stderr, "teach using all !\n");
  39. //NOTE this is crucial if we clear _teachSet afterwards!
  40. //therefore, take care NOT to call _techSet.clear() somewhere out of this method
  41. this->dataSet = _dataSet;
  42. this->labelSet = _labelSet.std_vector();
  43. std::cerr << "number of known training samples: " << this->dataSet.size() << std::endl;
  44. }
  45. void RegKNN::teach ( const NICE::Vector & x, const double & y )
  46. {
  47. std::cerr << "RegKNN::teach one new example" << std::endl;
  48. for ( size_t i = 0 ; i < x.size() ; i++ )
  49. if ( isnan(x[i]) )
  50. {
  51. fprintf (stderr, "There is a NAN value within this vector: x[%d] = %f\n", (int)i, x[i]);
  52. cerr << x << endl;
  53. exit(-1);
  54. }
  55. dataSet.push_back ( x );
  56. labelSet.push_back ( y );
  57. std::cerr << "number of known training samples: " << dataSet.size()<< std::endl;
  58. }
  59. double RegKNN::predict ( const NICE::Vector & x )
  60. {
  61. FullVector distances(dataSet.size());
  62. if ( dataSet.size() <= 0 )
  63. {
  64. fprintf (stderr, "RegKNN: please use the teach method first\n");
  65. exit(-1);
  66. }
  67. #pragma omp parallel for
  68. for(uint i = 0; i < dataSet.size(); i++)
  69. {
  70. double distance = distancefunc->calculate (x,dataSet[i]);
  71. if ( isnan(distance) )
  72. {
  73. fprintf (stderr, "RegKNN::predict: NAN value found !!\n");
  74. cerr << x << endl;
  75. cerr << dataSet[i] << endl;
  76. }
  77. // #pragma omp critical
  78. distances[i] = distance;
  79. }
  80. std::vector<int> ind;
  81. distances.getSortedIndices(ind);
  82. double response = 0.0;
  83. if ( dataSet.size() < K )
  84. {
  85. cerr << K << endl;
  86. K = dataSet.size();
  87. cerr<<"RegKNN: Not enough datapoints! Setting K to: "<< K <<endl;
  88. }
  89. if ( distances[ind[0]] == 0.0 ) {
  90. cerr<<"RegKNN: Warning: datapoint was already seen during training... using its label as prediction."<<endl;
  91. return labelSet[ind[0]];
  92. }
  93. double maxElement = distances.max(); //normalize distances
  94. distances.multiply(1.0/maxElement);
  95. double weightSum = 0.0;
  96. for(uint i = 0; i < K; i++)
  97. {
  98. response += 1.0/distances[ind[i]] * labelSet[ind[i]];
  99. weightSum += 1.0/distances[ind[i]];
  100. }
  101. return ( response / weightSum );
  102. }