RegressionNode.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file RegressionNode.cpp
  3. * @brief regression node
  4. * @author Sven Sickert
  5. * @date 06/19/2013
  6. */
  7. #include <iostream>
  8. #include "vislearning/regression/randomforest/RegressionNode.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. RegressionNode::~RegressionNode()
  13. {
  14. }
  15. RegressionNode::RegressionNode ()
  16. {
  17. left = NULL;
  18. right = NULL;
  19. f = 0;
  20. counter = 0;
  21. }
  22. RegressionNode *RegressionNode::getLeafNode (
  23. const NICE::Vector & x,
  24. int depth )
  25. {
  26. if ( (!depth) || ((left == NULL) && (right == NULL)) )
  27. return this;
  28. double val = x[f];
  29. if ( val < threshold )
  30. if ( left != NULL )
  31. return left->getLeafNode ( x, depth - 1 );
  32. else
  33. return this;
  34. else
  35. if ( right != NULL )
  36. return right->getLeafNode( x, depth - 1 );
  37. else
  38. return this;
  39. }
  40. void RegressionNode::traverse (
  41. const NICE::Vector & x,
  42. double & _predVal
  43. )
  44. {
  45. RegressionNode *leaf = getLeafNode ( x );
  46. _predVal = leaf->predVal;
  47. }
  48. void RegressionNode::statistics ( int & depth, int & count ) const
  49. {
  50. int dl, cl;
  51. if ( left != NULL )
  52. {
  53. left->statistics ( dl, cl );
  54. dl++;
  55. } else {
  56. dl = 0;
  57. cl = 0;
  58. }
  59. if ( right != NULL )
  60. {
  61. right->statistics( depth, count );
  62. depth++;
  63. } else {
  64. depth = 0;
  65. count = 0;
  66. }
  67. depth = (depth > dl) ? depth : dl;
  68. count += cl + 1;
  69. }
  70. void RegressionNode::indexDescendants (
  71. map<RegressionNode *, pair<long, int> > & index,
  72. long & maxindex,
  73. int depth ) const
  74. {
  75. if ( left != NULL )
  76. {
  77. maxindex++;
  78. index.insert ( pair<RegressionNode *, pair<long, int> > ( left, pair<long, int>(maxindex, depth + 1) ) );
  79. left->indexDescendants ( index, maxindex, depth+1 );
  80. }
  81. if ( right != NULL )
  82. {
  83. maxindex++;
  84. index.insert ( pair<RegressionNode *, pair<long, int> > ( right, pair<long, int>(maxindex, depth + 1) ) );
  85. right->indexDescendants ( index, maxindex, depth+1 );
  86. }
  87. }
  88. void RegressionNode::nodePrediction(
  89. const Vector & y,
  90. const vector<int> & selection )
  91. {
  92. double mean = 0.0;
  93. for (int i = 0; i < (int)selection.size(); i++)
  94. {
  95. mean += y[ selection[i] ];
  96. }
  97. mean = mean/selection.size();
  98. double sum_squares = 0.0;
  99. for (int i = 0; i < (int)selection.size(); i++)
  100. {
  101. double diff = y[ selection[i] ] - mean;
  102. sum_squares += diff*diff;
  103. }
  104. lsError = sum_squares;
  105. predVal = mean;
  106. }
  107. void RegressionNode::resetCounters ()
  108. {
  109. counter = 0;
  110. if ( left != NULL ) left->resetCounters();
  111. if ( right != NULL ) right->resetCounters();
  112. }
  113. void RegressionNode::copy ( RegressionNode *node )
  114. {
  115. left = node->left;
  116. right = node->right;
  117. threshold = node->threshold;
  118. f = node->f;
  119. predVal = node->predVal;
  120. lsError = node->lsError;
  121. trainExamplesIndices = node->trainExamplesIndices;
  122. }
  123. bool RegressionNode::isLeaf () const
  124. {
  125. return ( (right == NULL) && (left == NULL) );
  126. }