RegKNN.cpp 2.9 KB

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