RegressionAlgorithmKernel.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. teachKernel ( kernelData, this->y );
  39. }
  40. double RegressionAlgorithmKernel::predict ( const NICE::Vector & x )
  41. {
  42. if ( kernelFunction == NULL )
  43. fthrow( Exception, "RegressionAlgorithmKernel::predict: To use this function, you have to specify a kernel function using the constructor" );
  44. if ( this->X.size() == 0 )
  45. fthrow( Exception, "RegressionAlgorithmKernel::predict: To use this function, you have to use teach(const VVector&, const Vector&) !" );
  46. NICE::Vector kstar;
  47. kernelFunction->calcKernelVector ( this->X, x, kstar );
  48. double kstarstar = kernelFunction->K(x,x);
  49. return predictKernel (kstar, kstarstar);
  50. }