testRegressionRDFGP.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @file testRegressionRDFGP.cpp
  3. * @brief test of RDF with GP
  4. * @author Sven Sickert
  5. * @date 07/02/2013
  6. */
  7. #include <sstream>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include "core/basics/Config.h"
  16. #include "core/vector/VectorT.h"
  17. #include "core/vector/VVector.h"
  18. #include "vislearning/baselib/ICETools.h"
  19. #include "vislearning/regression/regcombination/RegPreRandomForests.h"
  20. #include "vislearning/regression/gpregression/RegGaussianProcess.h"
  21. #include "vislearning/math/kernels/KernelExp.h"
  22. using namespace OBJREC;
  23. using namespace NICE;
  24. using namespace std;
  25. void csvline_populate ( vector<string> &record,
  26. const string& line,
  27. char delimiter )
  28. {
  29. int linepos=0;
  30. int inquotes=false;
  31. char c;
  32. int linemax=line.length();
  33. string curstring;
  34. record.clear();
  35. while(line[linepos]!=0 && linepos < linemax)
  36. {
  37. c = line[linepos];
  38. if (!inquotes && curstring.length()==0 && c=='"')
  39. {
  40. //beginquotechar
  41. inquotes=true;
  42. }
  43. else if (inquotes && c=='"')
  44. {
  45. //quotechar
  46. if ( (linepos+1 <linemax) && (line[linepos+1]=='"') )
  47. {
  48. //encountered 2 double quotes in a row (resolves to 1 double quote)
  49. curstring.push_back(c);
  50. linepos++;
  51. }
  52. else
  53. {
  54. //endquotechar
  55. inquotes=false;
  56. }
  57. }
  58. else if (!inquotes && c==delimiter)
  59. {
  60. //end of field
  61. record.push_back( curstring );
  62. curstring="";
  63. }
  64. else if (!inquotes && (c=='\r' || c=='\n') )
  65. {
  66. record.push_back( curstring );
  67. return;
  68. }
  69. else
  70. {
  71. curstring.push_back(c);
  72. }
  73. linepos++;
  74. }
  75. record.push_back( curstring );
  76. }
  77. void loadData( NICE::VVector &Data,
  78. NICE::Vector &y,
  79. const string &path,
  80. const string &xdat,
  81. const string &ydat )
  82. {
  83. vector<string> row;
  84. string line;
  85. cerr<<"Preloading Data...";
  86. ifstream in( (path+xdat).c_str() );
  87. if ( in.fail() )
  88. {
  89. cout << "File not found" <<endl;
  90. exit(EXIT_FAILURE);
  91. }
  92. int numData = 0;
  93. while ( getline(in, line) && in.good() )
  94. {
  95. csvline_populate(row, line, ',');
  96. vector<double> vec;
  97. for (int i = 0; i < (int)row.size(); i++)
  98. {
  99. double dval = 0.0;
  100. dval = atof(row[i].data() );
  101. vec.push_back(dval);
  102. }
  103. NICE::Vector nvec(vec);
  104. Data.push_back(nvec);
  105. numData++;
  106. }
  107. in.close();
  108. cerr<<"Finished."<<endl<<"Starting to get preloaded Labels...";
  109. in.open( (path+ydat).c_str() );
  110. if ( in.fail() )
  111. {
  112. cout << "File not found! Setting default value 0.0..." <<endl;
  113. y.resize(numData);
  114. y.set(0.0);
  115. }
  116. else
  117. {
  118. y.resize(numData);
  119. int count = 0;
  120. while(getline(in, line) && in.good() )
  121. {
  122. csvline_populate(row, line, ',');
  123. for ( int i = 0; i < (int)row.size(); i++ )
  124. {
  125. double dval = 0.0;
  126. dval = atof(row[i].data() );
  127. y.set(count,dval);
  128. count++;
  129. }
  130. }
  131. in.close();
  132. }
  133. cerr<<"Finished."<<endl;
  134. }
  135. void testFrame ( Config confRDF,
  136. NICE::VVector &xdata,
  137. NICE::Vector &y )
  138. {
  139. cerr<<"\nStarting test framework..."<<endl;
  140. /*------------Initialize Variables-----------*/
  141. ofstream storeEvalData;
  142. double trainRatio = confRDF.gD( "debug", "training_ratio", .9 );
  143. int trainingSize = (int)(trainRatio*xdata.size());
  144. int testingSize = xdata.size() - trainingSize;
  145. vector<int> indices;
  146. for ( int i = 0; i < (int)xdata.size(); i++ )
  147. indices.push_back(i);
  148. int nfolds = confRDF.gI( "debug", "nfolds", 10 );
  149. Vector mef_v ( nfolds );
  150. Vector corr_v ( nfolds );
  151. Vector resub_v ( nfolds );
  152. Vector diff_v ( nfolds );
  153. bool saveForest = confRDF.gB( "debug", "save_forest", false );
  154. string leafReg = confRDF.gS( "PreRandomForest", "leaf_regression", "gp" );
  155. KernelExp *kernel_template = new KernelExp ( confRDF.gD("Kernel", "log_rbf_gamma", -2.5), 0.0 );
  156. /*------------Store Configuration------------*/
  157. string filename = confRDF.gS( "debug", "filename" );
  158. if ( saveForest )
  159. {
  160. cout << "Configuration will be stored in: " << filename << "_config" << endl;
  161. storeEvalData.open ( (filename+"_config").c_str() );
  162. storeEvalData << "random_split_tests=" << confRDF.gI ( "RTBRandom", "random_split_tests" ) << endl;
  163. storeEvalData << "random_features=" << confRDF.gI ( "RTBRandom", "random_features" ) << endl;
  164. storeEvalData << "max_depth=" << confRDF.gI ( "RTBRandom", "max_depth" ) << endl;
  165. storeEvalData << "random_split_mode=" << confRDF.gS ( "RTBRandom", "random_split_mode" ) << endl;
  166. storeEvalData << "min_examples=" << confRDF.gI ( "RTBRandom", "min_examples" ) << endl;
  167. storeEvalData << "number_of_trees=" << confRDF.gI ( "RandomForest", "number_of_trees" ) << endl;
  168. storeEvalData << "features_per_tree=" << confRDF.gD ( "RandomForest", "features_per_tree" ) << endl;
  169. storeEvalData << "samples_per_tree=" << confRDF.gD ( "RandomForest", "samples_per_tree" ) << endl;
  170. storeEvalData << "builder=" << confRDF.gS ( "RandomForest", "builder" ) << endl;
  171. storeEvalData << "minimum_error_reduction=" << confRDF.gD ( "RandomForest", "minimum_error_reduction" ) << endl;
  172. storeEvalData << "log_rbf_gamma=" << confRDF.gD ( "Kernel", "log_rbf_gamma" ) << endl;
  173. storeEvalData.close();
  174. } else
  175. {
  176. cout << "Configuration will not be stored." << endl;
  177. }
  178. /*------------Setting up PreRDF--------------*/
  179. for ( int k = 0; k < nfolds; k++)
  180. {
  181. string fold;
  182. ostringstream convert;
  183. convert << k;
  184. fold = convert.str();
  185. cout << "\nFOLD " << k << ":\n======" << endl;
  186. cerr << "Initializing leaf regression method " << leafReg << "...";
  187. RegressionAlgorithm *leafRegression = NULL;
  188. Kernel *kernel_function = NULL;
  189. if ( leafReg == "GaussProcess" )
  190. {
  191. kernel_function = new KernelExp ( *(kernel_template) );
  192. leafRegression = new RegGaussianProcess( &confRDF, kernel_function, "GPRegression" );
  193. }
  194. else if ( leafReg == "none" ) {
  195. cerr << "\ntestRegressionRDFGP::testFrame: No leaf regression method set! Using RandomForest prediction..." << endl;
  196. } else {
  197. cerr << "\ntestRegressionRDFGP::testFrame: No valid leaf regression method set! Aborting..." << endl;
  198. exit(-1);
  199. }
  200. cerr << "Finished." << endl;
  201. cerr << "Initializing PreRDF for regression...";
  202. RegPreRandomForests *prf = new RegPreRandomForests ( &confRDF, "PreRandomForest", leafRegression );
  203. cerr << "Finished." << endl;
  204. cerr << "Teaching the PreRDF for regression...";
  205. NICE::VVector trainData, testData;
  206. NICE::Vector trainVals ( trainingSize );
  207. NICE::Vector testVals ( testingSize );
  208. random_shuffle( indices.begin(), indices.end() );
  209. for ( int i = 0; i < trainingSize; i++ )
  210. {
  211. trainData.push_back ( xdata[ indices[i] ] );
  212. trainVals.set( i, y[ indices[i] ] );
  213. }
  214. for ( int j = 0; j < testingSize; j++ )
  215. {
  216. testData.push_back ( xdata[ indices[j+trainingSize] ] );
  217. testVals.set( j, y[ indices[j+trainingSize] ] );
  218. }
  219. prf->teach ( trainData, trainVals );
  220. cerr << "Finished." << endl;
  221. /*-------------Testing RDF-GP--------------*/
  222. cerr << "\nGetting prediction values for all data points...";
  223. NICE::Vector predictionValues( testingSize );
  224. predictionValues.set ( 0.0 );
  225. for ( int j = 0; j < testingSize; j++ )
  226. {
  227. predictionValues[j] = prf->predict( testData[j] );
  228. }
  229. cerr << "Finished." << endl;
  230. /*---------------Evaluation----------------*/
  231. NICE::Vector diff = testVals - predictionValues;
  232. double mod_var = diff.StdDev()*diff.StdDev();
  233. double tar_var = testVals.StdDev()*testVals.StdDev();
  234. mef_v.set( k, (1-mod_var/tar_var) );
  235. NICE::Vector meanv( predictionValues.size() );
  236. meanv.set( diff.Mean() );
  237. NICE::Vector lhs = diff - meanv;
  238. meanv.set( testVals.Mean() );
  239. NICE::Vector rhs = testVals - meanv;
  240. lhs *= rhs;
  241. double corr = lhs.Mean() / sqrt( diff.StdDev()*diff.StdDev()*testVals.StdDev()*testVals.StdDev() );
  242. corr_v.set( k, corr );
  243. diff *= diff;
  244. diff_v.set( k, diff.Mean());
  245. resub_v.set( k, (diff.Mean() / tar_var) );
  246. if (kernel_function != NULL)
  247. delete kernel_function;
  248. }
  249. /*------------------Output-------------------*/
  250. cout << "\nSimple Cross Validation Stats:\n==============================" << endl;
  251. cout << " Modelling Efficiency: " << mef_v.Mean() << endl;
  252. cout << " Correlation: " << corr_v.Mean() << endl;
  253. cout << " Mean Square Error: " << diff_v.Mean() << endl;
  254. cout << " Standardized MSE: " << resub_v.Mean() << endl;
  255. /*-----------------Cleaning------------------*/
  256. delete kernel_template;
  257. }
  258. int main (int argc, char **argv) {
  259. Config conf ( argc, argv ); //Config for RFGP
  260. string path = conf.gS( "debug", "path", "." );
  261. string dataset = conf.gS( "debug", "dataset", "flux" );
  262. NICE::VVector xdata;
  263. NICE::Vector y;
  264. loadData(xdata, y, path, (dataset+"_x.csv"), (dataset+"_y.csv") ); //load all data
  265. testFrame( conf, xdata, y );
  266. return 0;
  267. }