RegressionAlgorithmKernel.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }
  25. RegressionAlgorithmKernel::~RegressionAlgorithmKernel()
  26. {
  27. }
  28. void RegressionAlgorithmKernel::teach ( const VVector & X, const NICE::Vector & y )
  29. {
  30. if ( kernelFunction == NULL )
  31. fthrow( Exception, "RegressionAlgorithmKernel::teach: To use this function, you have to specify a kernel function using the constructor" );
  32. this->y = y;
  33. this->X = X;
  34. KernelData *kernelData = new KernelData ( &conf );
  35. kernelFunction->calcKernelData ( this->X, kernelData );
  36. kernelData->updateCholeskyFactorization();
  37. teach ( kernelData, this->y );
  38. }
  39. double RegressionAlgorithmKernel::predict ( const NICE::Vector & x )
  40. {
  41. if ( kernelFunction == NULL )
  42. fthrow( Exception, "RegressionAlgorithmKernel::predict: To use this function, you have to specify a kernel function using the constructor" );
  43. if ( this->X.size() == 0 )
  44. fthrow( Exception, "RegressionAlgorithmKernel::predict: To use this function, you have to use teach(const VVector&, const Vector&) !" );
  45. NICE::Vector kstar;
  46. kernelFunction->calcKernelVector ( this->X, x, kstar );
  47. double kstarstar = kernelFunction->K(x,x);
  48. return predictKernel (kstar, kstarstar);
  49. }