GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
HierarchicalClustering.h
Go to the documentation of this file.
1 
29 #ifndef GRT_HIERARCHICAL_CLUSTERING_HEADER
30 #define GRT_HIERARCHICAL_CLUSTERING_HEADER
31 
32 #include "../../CoreModules/Clusterer.h"
33 
34 GRT_BEGIN_NAMESPACE
35 
36 class GRT_API ClusterInfo{
37 public:
38  ClusterInfo(){
39  uniqueClusterID = 0;
40  clusterVariance = 0;
41  }
42  ClusterInfo(const ClusterInfo &rhs){
43  *this = rhs;
44  }
45  ~ClusterInfo(){
46 
47  }
48 
49  ClusterInfo& operator=(const ClusterInfo &rhs){
50 
51  if( this != &rhs ){
52  this->uniqueClusterID = rhs.uniqueClusterID;
53  this->clusterVariance = rhs.clusterVariance;
54  this->indexs = rhs.indexs;
55  }
56 
57  return *this;
58  }
59 
60  UINT& operator[](const UINT &i){
61  return indexs[i];
62  }
63 
64  UINT operator[](const UINT &i) const{
65  return indexs[i];
66  }
67 
68  void addSampleToCluster(UINT i){
69  indexs.push_back( i );
70  }
71 
72  UINT getUniqueClusterID() const{
73  return uniqueClusterID;
74  }
75 
76  UINT getNumSamplesInCluster() const{
77  return (UINT)indexs.size();
78  }
79 
80  Float getClusterVariance() const{
81  return clusterVariance;
82  }
83 
84  unsigned int uniqueClusterID;
85  Float clusterVariance;
86  Vector< UINT > indexs;
87 };
88 
89 class GRT_API ClusterLevel{
90 public:
91  ClusterLevel(){
92  level = 0;
93  }
94 
95  ClusterLevel(const ClusterLevel &rhs){
96  this->level = rhs.level;
97  this->clusters = rhs.clusters;
98  }
99 
100  ~ClusterLevel(){
101 
102  }
103 
104  ClusterLevel& operator=(const ClusterLevel &rhs){
105 
106  if( this != &rhs ){
107  this->level = rhs.level;
108  this->clusters = rhs.clusters;
109  }
110 
111  return *this;
112  }
113 
114  ClusterInfo& operator[](const UINT &i){
115  return clusters[i];
116  }
117 
118  ClusterInfo operator[](const UINT &i) const{
119  return clusters[i];
120  }
121 
122  UINT getLevel() const{
123  return level;
124  }
125 
126  UINT getNumClusters() const{
127  return (UINT)clusters.size();
128  }
129 
130  unsigned int level;
131  Vector< ClusterInfo > clusters;
132 };
133 
134 class GRT_API HierarchicalClustering : public Clusterer {
135 
136 public:
141 
148 
152  virtual ~HierarchicalClustering();
153 
160  HierarchicalClustering &operator=(const HierarchicalClustering &rhs);
161 
169  virtual bool deepCopyFrom(const Clusterer *clusterer);
170 
177  virtual bool reset();
178 
184  virtual bool clear();
185 
192  virtual bool train_(MatrixFloat &trainingData);
193 
200  virtual bool train_(ClassificationData &trainingData);
201 
208  virtual bool train_(UnlabelledData &trainingData);
209 
217  virtual bool saveModelToFile( std::fstream &file ) const;
218 
226  virtual bool loadModelFromFile( std::fstream &file );
227 
228  bool printModel();
229 
230  Vector< ClusterLevel > getClusters(){ return clusters; }
231 
232  //Tell the compiler we are using the base class train method to stop hidden virtual function warnings
233  using MLBase::saveModelToFile;
234  using MLBase::loadModelFromFile;
235 
241  static std::string getId();
242 
243 protected:
244  inline Float SQR(const Float &a) {return a*a;};
245  Float squaredEuclideanDistance(const Float *a,const Float *b);
246  Float computeClusterDistance( const ClusterInfo &clusterA, const ClusterInfo &clusterB );
247  Float computeClusterVariance( const ClusterInfo &cluster, const MatrixFloat &data );
248 
249  UINT M; //Number of training examples
250  UINT N; //Number of dimensions
251  Vector< ClusterLevel > clusters;
252  MatrixFloat distanceMatrix; //The distance matrix
253 
254 private:
256  static const std::string id;
257 
258 };
259 
260 GRT_END_NAMESPACE
261 
262 #endif //GRT_HIERARCHICAL_CLUSTERING_HEADER
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool reset() override
Definition: Clusterer.cpp:130
virtual bool clear() override
Definition: Clusterer.cpp:144
virtual bool deepCopyFrom(const Clusterer *clusterer)
Definition: Clusterer.h:59
virtual bool train_(MatrixFloat &trainingData) override
Definition: Clusterer.cpp:116