RegKNN.cpp 3.1 KB

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