RegKNN.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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()
  23. {
  24. }
  25. void RegKNN::teach ( const NICE::VVector & _dataSet, const NICE::Vector & _labelSet)
  26. {
  27. fprintf (stderr, "teach using all !\n");
  28. //NOTE this is crucial if we clear _teachSet afterwards!
  29. //therefore, take care NOT to call _techSet.clear() somewhere out of this method
  30. this->dataSet = _dataSet;
  31. this->labelSet = _labelSet.std_vector();
  32. std::cerr << "number of known training samples: " << this->dataSet.size() << std::endl;
  33. }
  34. void RegKNN::teach ( const NICE::Vector & x, const double & y )
  35. {
  36. std::cerr << "RegKNN::teach one new example" << std::endl;
  37. for ( size_t i = 0 ; i < x.size() ; i++ )
  38. if ( isnan(x[i]) )
  39. {
  40. fprintf (stderr, "There is a NAN value in within this vector: x[%d] = %f\n", (int)i, x[i]);
  41. cerr << x << endl;
  42. exit(-1);
  43. }
  44. dataSet.push_back ( x );
  45. labelSet.push_back ( y );
  46. std::cerr << "number of known training samples: " << dataSet.size()<< std::endl;
  47. }
  48. double RegKNN::predict ( const NICE::Vector & x )
  49. {
  50. FullVector distances(dataSet.size());
  51. if ( dataSet.size() <= 0 ) {
  52. fprintf (stderr, "RegKNN: please use the train method first\n");
  53. exit(-1);
  54. }
  55. #pragma omp parallel for
  56. for(uint i = 0; i < dataSet.size(); i++){
  57. double distance = distancefunc->calculate (x,dataSet[i]);
  58. if ( isnan(distance) ){
  59. fprintf (stderr, "RegKNN::predict: NAN value found !!\n");
  60. cerr << x << endl;
  61. cerr << dataSet[i] << endl;
  62. }
  63. // #pragma omp critical
  64. distances[i] = distance;
  65. }
  66. std::vector<int> ind;
  67. distances.getSortedIndices(ind);
  68. double response = 0.0;
  69. if ( dataSet.size() < K ){
  70. K = dataSet.size();
  71. cerr<<"RegKNN: Not enough datapoints! Setting K to: "<< K <<endl;
  72. }
  73. if ( distances[ind[0]] == 0.0 ) {
  74. cerr<<"RegKNN: Warning: datapoint was already seen during training... using its label as prediction."<<endl;
  75. return labelSet[ind[0]];
  76. }
  77. double maxElement = distances.max(); //normalize distances
  78. distances.multiply(1.0/maxElement);
  79. double weightSum = 0.0;
  80. for(uint i = 0; i < K; i++){
  81. response += 1.0/distances[ind[i]] * labelSet[ind[i]];
  82. weightSum += 1.0/distances[ind[i]];
  83. }
  84. return ( response / weightSum );
  85. }