KMeans.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * @brief simple constructor
  77. * @param _noClusters the number of clusters to be computed
  78. * @param _distanceMode a string specifying the distance function to be used (default: euclidean)
  79. */
  80. KMeans( const int & _noClusters , const std::string & _distanceMode="euclidean");
  81. /**
  82. * @brief standard constructor
  83. * @param conf config file specifying all relevant variable settings
  84. * @param _section name of the section within the configfile where the settings can be found (default: KMeans)
  85. */
  86. KMeans( const NICE::Config *conf, const std::string & _section = "KMeans");
  87. /** simple destructor */
  88. virtual ~KMeans();
  89. /**
  90. *@brief this is the actual method that performs the clustering for a given set of features
  91. *@author Erik Rodner, Alexander Freytag
  92. *@date 29-10-2007 (dd-mm-yyyy)
  93. *@param features input features to be clustered
  94. *@param prototypes computed prototypes (cluster medoids) for the given samples
  95. *@param weights number of assignments for every cluster
  96. *@param assignment explicite assignments of features to computed cluster medoids
  97. */
  98. void cluster ( const NICE::VVector & features,
  99. NICE::VVector & prototypes,
  100. std::vector<double> & weights,
  101. std::vector<int> & assignment );
  102. };
  103. } // namespace
  104. #endif