KMeans.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <core/basics/Config.h>
  10. #include <core/vector/Distance.h>
  11. #include "core/vector/VectorT.h"
  12. #include "core/vector/MatrixT.h"
  13. #include "ClusterAlgorithm.h"
  14. #include <core/vector/Distance.h>
  15. namespace OBJREC {
  16. /** K-Means */
  17. /**
  18. * @class K-Means
  19. * @brief K-Means
  20. * @author Erik Rodner, Alexander Freytag
  21. * @date 29-10-2007 (dd-mm-yyyy)
  22. */
  23. class KMeans : public ClusterAlgorithm
  24. {
  25. protected:
  26. /************************
  27. *
  28. * protected variables
  29. *
  30. **************************/
  31. //! desired number of clusters
  32. int noClusters;
  33. //! specify which distance to use for calculating assignments
  34. std::string distanceType;
  35. //! the actual distance metric
  36. NICE::VectorDistance<double> *distancefunction;
  37. //! maximum difference between prototype-solutions of two iterations for convergence
  38. double d_minDelta;
  39. //! maximum number of iterations until convergence
  40. int i_maxIterations;
  41. /************************
  42. *
  43. * protected methods
  44. *
  45. **************************/
  46. //! compute the distance between two features using the specified distance metric
  47. double vectorDistance(const NICE::Vector &vector1, const NICE::Vector &vector2, uint distancetype);
  48. //! compute assignments of all given features wrt to the currently known prototypes (cluster centroids) == ~ E-step
  49. double compute_assignments ( const NICE::VVector & features,
  50. const NICE::VVector & prototypes,
  51. std::vector<int> & assignment );
  52. //! compute number of assignments for every currently found cluster
  53. double compute_weights ( const NICE::VVector & features,
  54. std::vector<double> & weights,
  55. std::vector<int> & assignment );
  56. //! compute the difference between prototypes of previous iteration and those currently found
  57. double compute_delta ( const NICE::VVector & oldprototypes,
  58. const NICE::VVector & prototypes );
  59. //! compute (update) prototypes given the current assignments == ~ M-step
  60. int compute_prototypes ( const NICE::VVector & features,
  61. NICE::VVector & prototypes,
  62. std::vector<double> & weights,
  63. const std::vector<int> & assignment );
  64. //! have an initial guess, i.e., randomly pick some features as initial cluster centroids
  65. void initial_guess ( const NICE::VVector & features,
  66. NICE::VVector & prototypes );
  67. //! give additional information for the current iteration
  68. void print_iteration ( int iterations,
  69. NICE::VVector & prototypes,
  70. double delta );
  71. public:
  72. /**
  73. * @brief simple constructor
  74. * @param _noClusters the number of clusters to be computed
  75. * @param _distanceMode a string specifying the distance function to be used (default: euclidean)
  76. */
  77. KMeans( const int & _noClusters , const std::string & _distanceMode="euclidean");
  78. /**
  79. * @brief standard constructor
  80. * @param conf config file specifying all relevant variable settings
  81. * @param _section name of the section within the configfile where the settings can be found (default: KMeans)
  82. */
  83. KMeans( const NICE::Config *conf, const std::string & _section = "KMeans");
  84. /** simple destructor */
  85. virtual ~KMeans();
  86. /**
  87. *@brief this is the actual method that performs the clustering for a given set of features
  88. *@author Erik Rodner, Alexander Freytag
  89. *@date 29-10-2007 (dd-mm-yyyy)
  90. *@param features input features to be clustered
  91. *@param prototypes computed prototypes (cluster medoids) for the given samples
  92. *@param weights number of assignments for every cluster
  93. *@param assignment explicite assignments of features to computed cluster medoids
  94. */
  95. void cluster ( const NICE::VVector & features,
  96. NICE::VVector & prototypes,
  97. std::vector<double> & weights,
  98. std::vector<int> & assignment );
  99. };
  100. } // namespace
  101. #endif