RandomClustering.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @file RandomClustering.cpp
  3. * @brief Clustering by randomly picking some samples from the set of features as representatives
  4. * @author Alexander Freytag
  5. * @date 03-06-2013 (dd-mm-yyyy)
  6. */
  7. #ifdef NICE_USELIB_OPENMP
  8. #include <omp.h>
  9. #endif
  10. #include <iostream>
  11. #include <map>
  12. #include "vislearning/math/distances/genericDistance.h"
  13. #include "vislearning/math/cluster/RandomClustering.h"
  14. #include <set>
  15. using namespace OBJREC;
  16. using namespace std;
  17. using namespace NICE;
  18. RandomClustering::RandomClustering(const int & _noClusters, const std::string & _distanceType) :
  19. noClusters(_noClusters), distanceType(_distanceType)
  20. {
  21. }
  22. RandomClustering::RandomClustering( const NICE::Config *conf, const std::string & _section)
  23. {
  24. this->noClusters = conf->gI( _section, "noClusters", 20);
  25. this->distanceType = conf->gS( _section, "distanceType", "euclidean" );
  26. this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  27. }
  28. RandomClustering::~RandomClustering()
  29. {
  30. }
  31. int RandomClustering::compute_prototypes(const NICE::VVector & _features, NICE::VVector & _prototypes,
  32. std::vector<double> & _weights, const std::vector<int> & _assignment)
  33. {
  34. int noFeatures ( _features.size() );
  35. std::set<int, std::greater<int> > chosenIdx;
  36. //empty init
  37. chosenIdx.clear();
  38. //pick k distinct cluster representatives randomly
  39. for (int cnt = 0; cnt < this->noClusters; cnt++)
  40. {
  41. int idx;
  42. do
  43. {
  44. idx = rand() % noFeatures;
  45. }
  46. while ( chosenIdx.find ( idx ) != chosenIdx.end() );
  47. //if a new (distinct) idx was computed, insert it into the set of randomly picked indicees
  48. chosenIdx.insert ( idx );
  49. }
  50. _prototypes.resize( this->noClusters );
  51. int clusterCnt ( 0 );
  52. for ( std::set<int>::const_iterator idxIt = chosenIdx.begin(); idxIt != chosenIdx.end(); idxIt++, clusterCnt++ )
  53. {
  54. _prototypes[clusterCnt] = _features[ *idxIt ];
  55. }
  56. return 0;
  57. }
  58. double RandomClustering::compute_assignments(const NICE::VVector & _features,
  59. const NICE::VVector & _prototypes,
  60. std::vector<int> & _assignment)
  61. {
  62. _assignment.resize( _features.size() );
  63. int index = 0;
  64. for (NICE::VVector::const_iterator i = _features.begin(); i != _features.end(); i++, index++)
  65. {
  66. const NICE::Vector & x = *i;
  67. double mindist = std::numeric_limits<double>::max();
  68. int minclass = 0;
  69. int c = 0;
  70. for (NICE::VVector::const_iterator j = _prototypes.begin(); j
  71. != _prototypes.end(); j++, c++)
  72. {
  73. const NICE::Vector & p = *j;
  74. double distance = this->distancefunction->calculate(p, x);
  75. if (distance < mindist)
  76. {
  77. minclass = c;
  78. mindist = distance;
  79. }
  80. }
  81. _assignment[index] = minclass;
  82. }
  83. return 0.0;
  84. }
  85. double RandomClustering::compute_weights(const NICE::VVector & _features,
  86. std::vector<double> & _weights,
  87. std::vector<int> & _assignment)
  88. {
  89. _weights.resize( this->noClusters );
  90. //initalization
  91. for (int k = 0; k < noClusters; k++)
  92. _weights[k] = 0;
  93. int j = 0;
  94. //increase weight for every assignment
  95. for (NICE::VVector::const_iterator i = _features.begin(); i != _features.end(); i++, j++)
  96. {
  97. int k = _assignment[j];
  98. _weights[k]++;
  99. }
  100. //normalize weights
  101. for (int k = 0; k < noClusters; k++)
  102. _weights[k] = _weights[k] / _features.size();
  103. return 0.0;
  104. }
  105. void RandomClustering::cluster(const NICE::VVector & _features,
  106. NICE::VVector & _prototypes,
  107. std::vector<double> & _weights,
  108. std::vector<int> & _assignment)
  109. {
  110. //randomly pick cluster centers
  111. this->compute_prototypes( _features, _prototypes, _weights, _assignment );
  112. //compute assignments for every cluster
  113. this->compute_assignments( _features, _prototypes, _assignment );
  114. //compute corresponding weights
  115. this->compute_weights( _features, _weights, _assignment );
  116. }