computeNormalizedHistogramFeatures.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file computeNormalizedHistogramFeatures.cpp
  3. * @brief simply compute randomly generated, normalized histogram features
  4. * @author Alexander Freytag
  5. * @date 07-05-2012 (dd-mm-yyyy)
  6. */
  7. #include <vector>
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <core/basics/Config.h>
  12. #include <core/vector/MatrixT.h>
  13. #include <core/vector/VectorT.h>
  14. #include <core/vector/Algorithms.h>
  15. #include <core/vector/MatrixT.h>
  16. using namespace std;
  17. using namespace NICE;
  18. int main (int argc, char* argv[])
  19. {
  20. Config conf ( argc, argv );
  21. int n = conf.gI("main", "nrOfExamplesPerClass", 10);
  22. int d = conf.gI("main", "nrOfDimensions", 20);
  23. int m = conf.gI("main", "nrOfClasses", 2);
  24. string destination = conf.gS( "main", "destination", "/tmp/features.data");
  25. bool saveLastDimension = conf.gB( "main", "saveLastDimension", false);
  26. if (d < m)
  27. {
  28. std::cerr << "Sry, you specified less dimensions than number of classes" << std::endl;
  29. return -1;
  30. }
  31. if (d <= 1 )
  32. {
  33. std::cerr << "Sry, you have to specify at least two dimensions" << std::endl;
  34. return -1;
  35. }
  36. NICE::Vector y (n*m);
  37. NICE::Vector yBin (n*m);
  38. NICE::Matrix data;
  39. if (saveLastDimension)
  40. {
  41. data.resize( n*m, d-1 );
  42. }
  43. else
  44. {
  45. data.resize( n*m, d-1 );
  46. }
  47. data.set( 0.0 );
  48. int classCount(0);
  49. for (int i = 0; i < n*m; i++)
  50. {
  51. double meanPrimary ( std::min(0.25, 1.0/(d-1) ) );
  52. double meanSecondary ( 0.75 / (2.0*d) );
  53. double stdDev( 0.75 / (3.0*d) );
  54. double sum(0.0);
  55. double sampleValue(0.0);
  56. for (int dim = 0; dim < d-1; dim++)
  57. {
  58. do
  59. {
  60. sampleValue = fabs( randGaussDouble ( stdDev ) );
  61. if (dim == classCount)
  62. sampleValue += meanPrimary;
  63. else
  64. sampleValue += meanSecondary;
  65. } while ( ( sum + sampleValue ) >= 1.0) ; //pay attention that the normalization works properly
  66. sum += sampleValue;
  67. data(i, dim) = sampleValue;
  68. }
  69. //normalization
  70. if ( saveLastDimension )
  71. {
  72. data( i, d-1 ) = 1.0 - sum;
  73. std::cerr << "i: " << i << " d-1: " << d-1 << " sum: " << sum << " 1.0 - sum: " << 1.0-sum << std::endl;
  74. }
  75. //save the corresponding label
  76. y[i] = classCount;
  77. if (classCount < m/2.0)
  78. yBin[i] = 0;
  79. else
  80. yBin[i] = 1;
  81. if ( (i % n ) == (n-1))
  82. {
  83. classCount++;
  84. }
  85. }
  86. std::filebuf fb;
  87. fb.open (destination.c_str(),ios::out);
  88. std::ostream os(&fb);
  89. //
  90. os << data << std::endl;
  91. os << yBin << std::endl;
  92. os << y;
  93. //
  94. fb.close();
  95. return 0;
  96. }