KMedian.h 4.3 KB

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