KMeans.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @file KMeans.cpp
  3. * @brief K-Means
  4. * @author Erik Rodner
  5. * @date 10/29/2007
  6. */
  7. #include <vislearning/nice_nonvis.h>
  8. #include <iostream>
  9. #include "vislearning/math/cluster/KMeans.h"
  10. #include "vislearning/math/distances/genericDistance.h"
  11. #include <set>
  12. using namespace OBJREC;
  13. using namespace std;
  14. using namespace NICE;
  15. #undef DEBUG_KMEANS
  16. KMeans::KMeans(int _noClasses, string _distanceType) :
  17. noClasses(_noClasses), distanceType(_distanceType)
  18. {
  19. //srand(time(NULL));
  20. distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  21. }
  22. KMeans::~KMeans()
  23. {
  24. }
  25. void KMeans::initial_guess(const VVector & features, VVector & prototypes)
  26. {
  27. int j = 0;
  28. std::set<int, std::greater<int> > mark;
  29. for (VVector::iterator i = prototypes.begin(); i != prototypes.end(); i++, j++)
  30. {
  31. int k;
  32. do
  33. {
  34. k = rand() % features.size();
  35. } while (mark.find(k) != mark.end());
  36. mark.insert(mark.begin(), k);
  37. *i = features[k];
  38. }
  39. }
  40. int KMeans::compute_prototypes(const VVector & features, VVector & prototypes,
  41. std::vector<double> & weights, const std::vector<int> & assignment)
  42. {
  43. int j = 0;
  44. // fprintf (stderr, "KMeans::compute_prototypes: init noClasses=%d\n", noClasses);
  45. for (int k = 0; k < noClasses; k++)
  46. {
  47. prototypes[k].set(0);
  48. weights[k] = 0;
  49. }
  50. // fprintf (stderr, "KMeans::compute_prototypes: compute means\n");
  51. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, j++)
  52. {
  53. int k = assignment[j];
  54. NICE::Vector & p = prototypes[k];
  55. const NICE::Vector & x = *i;
  56. #ifdef DEBUG_KMEANS
  57. fprintf(
  58. stderr,
  59. "KMeans::compute_prototypes: std::vector %d has assignment %d\n",
  60. j, k);
  61. #endif
  62. p += x;
  63. #ifdef DEBUG_KMEANS
  64. cerr << "vector was : " << x << endl;
  65. cerr << "prototype for this class is now : " << p << endl;
  66. #endif
  67. weights[k]++;
  68. }
  69. // fprintf (stderr, "KMeans::compute_prototypes: scaling\n");
  70. for (int k = 0; k < noClasses; k++)
  71. {
  72. NICE::Vector & p = prototypes[k];
  73. #ifdef DEBUG_KMEANS
  74. cerr << "prototype for this class before scaling : " << p << endl;
  75. #endif
  76. if (weights[k] <= 0)
  77. {
  78. return -1;
  79. }
  80. p *= (1.0 / weights[k]);
  81. weights[k] = weights[k] / features.size();
  82. #ifdef DEBUG_KMEANS
  83. cerr << "prototype for this class after scaling with " << weights[k]
  84. << " : " << p << endl;
  85. #endif
  86. }
  87. return 0;
  88. }
  89. double KMeans::compute_delta(const VVector & oldprototypes,
  90. const VVector & prototypes)
  91. {
  92. double distance = 0;
  93. for (uint k = 0; k < oldprototypes.size(); k++)
  94. {
  95. distance
  96. += distancefunction->calculate(oldprototypes[k], prototypes[k]);
  97. #ifdef DEBUG_KMEANS
  98. fprintf(stderr, "KMeans::compute_delta: Distance:",
  99. distancefunction->calculate(oldprototypes[k], prototypes[k]));
  100. #endif
  101. }
  102. return distance;
  103. }
  104. double KMeans::compute_assignments(const VVector & features,
  105. const VVector & prototypes, std::vector<int> & assignment)
  106. {
  107. int index = 0;
  108. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, index++)
  109. {
  110. const NICE::Vector & x = *i;
  111. double mindist = std::numeric_limits<double>::max();
  112. int minclass = 0;
  113. int c = 0;
  114. #ifdef DEBUG_KMEANS
  115. fprintf(stderr, "computing nearest prototype for std::vector %d\n",
  116. index);
  117. #endif
  118. for (VVector::const_iterator j = prototypes.begin(); j
  119. != prototypes.end(); j++, c++)
  120. {
  121. const NICE::Vector & p = *j;
  122. double distance = distancefunction->calculate(p, x);
  123. #ifdef DEBUG_KMEANS
  124. fprintf(stderr, "KMeans::compute_delta: Distance:",
  125. distancefunction->calculate(p, x));
  126. #endif
  127. #ifdef DEBUG_KMEANS
  128. cerr << p << endl;
  129. cerr << x << endl;
  130. fprintf(stderr, "distance to prototype %d is %f\n", c, distance);
  131. #endif
  132. if (distance < mindist)
  133. {
  134. minclass = c;
  135. mindist = distance;
  136. }
  137. }
  138. assignment[index] = minclass;
  139. }
  140. return 0.0;
  141. }
  142. double KMeans::compute_weights(const VVector & features,
  143. std::vector<double> & weights, std::vector<int> & assignment)
  144. {
  145. for (int k = 0; k < noClasses; k++)
  146. weights[k] = 0;
  147. int j = 0;
  148. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, j++)
  149. {
  150. int k = assignment[j];
  151. weights[k]++;
  152. }
  153. for (int k = 0; k < noClasses; k++)
  154. weights[k] = weights[k] / features.size();
  155. return 0.0;
  156. }
  157. void KMeans::cluster(const VVector & features, VVector & prototypes,
  158. std::vector<double> & weights, std::vector<int> & assignment)
  159. {
  160. VVector oldprototypes;
  161. prototypes.clear();
  162. weights.clear();
  163. assignment.clear();
  164. weights.resize(noClasses, 0);
  165. assignment.resize(features.size(), 0);
  166. int dimension;
  167. if ((int) features.size() >= noClasses)
  168. dimension = features[0].size();
  169. else
  170. {
  171. fprintf(stderr,
  172. "FATAL ERROR: Not enough feature vectors provided for kMeans\n");
  173. exit(-1);
  174. }
  175. for (int k = 0; k < noClasses; k++)
  176. {
  177. prototypes.push_back(Vector(dimension));
  178. prototypes[k].set(0);
  179. }
  180. KMeans_Restart:
  181. initial_guess(features, prototypes);
  182. int iterations = 0;
  183. double delta = std::numeric_limits<double>::max();
  184. const double minDelta = 1e-5;
  185. const int maxIterations = 200;
  186. do
  187. {
  188. iterations++;
  189. compute_assignments(features, prototypes, assignment);
  190. if (iterations > 1)
  191. oldprototypes = prototypes;
  192. #ifdef DEBUG_KMEANS
  193. fprintf(stderr, "KMeans::cluster compute_prototypes\n");
  194. #endif
  195. if (compute_prototypes(features, prototypes, weights, assignment) < 0)
  196. {
  197. fprintf(stderr, "KMeans::cluster restart\n");
  198. goto KMeans_Restart;
  199. }
  200. #ifdef DEBUG_KMEANS
  201. fprintf(stderr, "KMeans::cluster compute_delta\n");
  202. #endif
  203. if (iterations > 1)
  204. delta = compute_delta(oldprototypes, prototypes);
  205. #ifdef DEBUG_KMEANS
  206. print_iteration(iterations, prototypes, delta);
  207. #endif
  208. } while ((delta > minDelta) && (iterations < maxIterations));
  209. #ifdef DEBUG_KMEANS
  210. fprintf(stderr, "KMeans::cluster: iterations = %d, delta = %f\n",
  211. iterations, delta);
  212. #endif
  213. compute_weights(features, weights, assignment);
  214. }
  215. void KMeans::print_iteration(int iterations, VVector & prototypes, double delta)
  216. {
  217. if (iterations > 1)
  218. fprintf(stderr, "KMeans::cluster: iteration=%d delta=%f\n", iterations,
  219. delta);
  220. else
  221. fprintf(stderr, "KMeans::cluster: iteration=%d\n", iterations);
  222. int k = 0;
  223. for (VVector::const_iterator i = prototypes.begin(); i != prototypes.end(); i++, k++)
  224. {
  225. fprintf(stderr, "class (%d)\n", k);
  226. cerr << "prototype = " << (*i) << endl;
  227. }
  228. }