KMedian.h 3.9 KB

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