RegressionAlgorithmKernel.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @file RegressionAlgorithmKernel.cpp
  3. * @brief interface for a regression algorithm which is based on kernels
  4. * @author Erik Rodner
  5. * @date 12/09/2009
  6. */
  7. #include <iostream>
  8. #include "RegressionAlgorithmKernel.h"
  9. using namespace std;
  10. using namespace NICE;
  11. using namespace OBJREC;
  12. RegressionAlgorithmKernel::RegressionAlgorithmKernel( const Config *_conf, Kernel *kernelFunction ) : conf( *_conf )
  13. {
  14. this->kernelFunction = kernelFunction;
  15. }
  16. RegressionAlgorithmKernel::RegressionAlgorithmKernel( const RegressionAlgorithmKernel & src )
  17. {
  18. if ( src.kernelFunction != NULL )
  19. this->kernelFunction = src.kernelFunction->clone();
  20. else
  21. this->kernelFunction = NULL;
  22. this->X = src.X;
  23. this->y = src.y;
  24. this->conf = src.conf;
  25. }
  26. RegressionAlgorithmKernel::~RegressionAlgorithmKernel()
  27. {
  28. }
  29. void RegressionAlgorithmKernel::teach ( const VVector & X, const NICE::Vector & y )
  30. {
  31. if ( kernelFunction == NULL )
  32. fthrow( Exception, "RegressionAlgorithmKernel::teach: To use this function, you have to specify a kernel function using the constructor" );
  33. this->y = y;
  34. this->X = X;
  35. KernelData *kernelData = new KernelData ( &conf );
  36. kernelFunction->calcKernelData ( this->X, kernelData );
  37. kernelData->updateCholeskyFactorization();
  38. teach ( kernelData, this->y );
  39. delete kernelData;
  40. }
  41. double RegressionAlgorithmKernel::predict ( const NICE::Vector & x )
  42. {
  43. if ( kernelFunction == NULL )
  44. fthrow( Exception, "RegressionAlgorithmKernel::predict: To use this function, you have to specify a kernel function using the constructor" );
  45. if ( this->X.size() == 0 )
  46. fthrow( Exception, "RegressionAlgorithmKernel::predict: To use this function, you have to use teach(const VVector&, const Vector&) !" );
  47. NICE::Vector kstar;
  48. kernelFunction->calcKernelVector ( this->X, x, kstar );
  49. double kstarstar = kernelFunction->K(x,x);
  50. return predictKernel (kstar, kstarstar);
  51. }