KMeans.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file KMeans.h
  3. * @brief K-Means
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 29-10-2007 (dd-mm-yyyy)
  6. */
  7. #ifndef KMEANSINCLUDE
  8. #define KMEANSINCLUDE
  9. // nice-core includes
  10. #include <core/basics/Exception.h>
  11. #include <core/basics/Config.h>
  12. //
  13. #include <core/vector/Distance.h>
  14. #include "core/vector/VectorT.h"
  15. #include "core/vector/MatrixT.h"
  16. #include <core/vector/Distance.h>
  17. #include "ClusterAlgorithm.h"
  18. namespace OBJREC {
  19. /** K-Means */
  20. /**
  21. * @class K-Means
  22. * @brief K-Means
  23. * @author Erik Rodner, Alexander Freytag
  24. * @date 29-10-2007 (dd-mm-yyyy)
  25. */
  26. class KMeans : public ClusterAlgorithm
  27. {
  28. protected:
  29. /************************
  30. *
  31. * protected variables
  32. *
  33. **************************/
  34. //! desired number of clusters
  35. int noClusters;
  36. //! specify which distance to use for calculating assignments
  37. std::string distanceType;
  38. //! the actual distance metric
  39. NICE::VectorDistance<double> *distancefunction;
  40. //! maximum difference between prototype-solutions of two iterations for convergence
  41. double d_minDelta;
  42. //! maximum number of iterations until convergence
  43. int i_maxIterations;
  44. /************************
  45. *
  46. * protected methods
  47. *
  48. **************************/
  49. //! compute the distance between two features using the specified distance metric
  50. double vectorDistance(const NICE::Vector &vector1, const NICE::Vector &vector2, uint distancetype);
  51. //! compute assignments of all given features wrt to the currently known prototypes (cluster centroids) == ~ E-step
  52. double compute_assignments ( const NICE::VVector & features,
  53. const NICE::VVector & prototypes,
  54. std::vector<int> & assignment );
  55. //! compute number of assignments for every currently found cluster
  56. double compute_weights ( const NICE::VVector & features,
  57. std::vector<double> & weights,
  58. std::vector<int> & assignment );
  59. //! compute the difference between prototypes of previous iteration and those currently found
  60. double compute_delta ( const NICE::VVector & oldprototypes,
  61. const NICE::VVector & prototypes );
  62. //! compute (update) prototypes given the current assignments == ~ M-step
  63. int compute_prototypes ( const NICE::VVector & features,
  64. NICE::VVector & prototypes,
  65. std::vector<double> & weights,
  66. const std::vector<int> & assignment );
  67. //! have an initial guess, i.e., randomly pick some features as initial cluster centroids
  68. void initial_guess ( const NICE::VVector & features,
  69. NICE::VVector & prototypes );
  70. //! give additional information for the current iteration
  71. void print_iteration ( int iterations,
  72. NICE::VVector & prototypes,
  73. double delta );
  74. public:
  75. ///////////////////// ///////////////////// /////////////////////
  76. // CONSTRUCTORS / DESTRUCTORS
  77. ///////////////////// ///////////////////// /////////////////////
  78. /**
  79. * @brief default constructor
  80. * @date 12-02-2014 (dd-mm-yyyy )
  81. * @author Alexander Freytag
  82. */
  83. KMeans ( );
  84. /**
  85. * @brief simple constructor
  86. * @param _noClusters the number of clusters to be computed
  87. * @param _distanceMode a string specifying the distance function to be used (default: euclidean)
  88. */
  89. KMeans( const int & _noClusters , const std::string & _distanceMode="euclidean");
  90. /**
  91. * @brief standard constructor
  92. * @param conf config file specifying all relevant variable settings
  93. * @param _section name of the section within the configfile where the settings can be found (default: KMeans)
  94. */
  95. KMeans( const NICE::Config * _conf, const std::string & _confSection = "KMeans");
  96. /** simple destructor */
  97. virtual ~KMeans();
  98. /**
  99. * @brief Jobs previously performed in the config-version of the constructor, read settings etc.
  100. * @author Alexander Freytag
  101. * @date 12-02-2014 ( dd-mm-yyyy )
  102. */
  103. void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "KMeans");
  104. ///////////////////// ///////////////////// /////////////////////
  105. // CLUSTERING STUFF
  106. ///////////////////// ///////////////////// //////////////////
  107. /**
  108. *@brief this is the actual method that performs the clustering for a given set of features
  109. *@author Erik Rodner, Alexander Freytag
  110. *@date 29-10-2007 (dd-mm-yyyy)
  111. *@param features input features to be clustered
  112. *@param prototypes computed prototypes (cluster medoids) for the given samples
  113. *@param weights number of assignments for every cluster
  114. *@param assignment explicite assignments of features to computed cluster medoids
  115. */
  116. void cluster ( const NICE::VVector & features,
  117. NICE::VVector & prototypes,
  118. std::vector<double> & weights,
  119. std::vector<int> & assignment );
  120. ///////////////////// INTERFACE PERSISTENT /////////////////////
  121. // interface specific methods for store and restore
  122. ///////////////////// INTERFACE PERSISTENT /////////////////////
  123. /**
  124. * @brief Load object from external file (stream)
  125. * @author Alexander Freytag
  126. * @date 12-02-2014 ( dd-mm-yyyy )
  127. */
  128. void restore ( std::istream & is, int format = 0 );
  129. /**
  130. * @brief Save object to external file (stream)
  131. * @author Alexander Freytag
  132. * @date 12-02-2014 ( dd-mm-yyyy )
  133. */
  134. void store ( std::ostream & os, int format = 0 ) const;
  135. /**
  136. * @brief Clear object
  137. * @author Alexander Freytag
  138. * @date 12-02-2014 ( dd-mm-yyyy )
  139. */
  140. void clear ();
  141. };
  142. } // namespace
  143. #endif