HierarchicalClustering.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Hierarchical Clustering.
6  *
7  *
8  *
9  * \author T. Glasmachers
10  * \date 2011
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 
35 #ifndef SHARK_MODELS_CLUSTERING_HIERARCHICALCLUSTERING_H
36 #define SHARK_MODELS_CLUSTERING_HIERARCHICALCLUSTERING_H
37 
38 
41 
42 
43 namespace shark {
44 
45 
46 ///
47 /// \brief Clusters defined by a binary space partitioning tree.
48 ///
49 /// \par
50 /// Binary space-partitioning is a simple and fast way of
51 /// clustering.
52 ///
53 /// \par
54 /// It is not clear how the unfolding of the tree can be
55 /// expressed as a flat parameter vector. Therefore, the
56 /// parameter vector of this model is empty.
57 ///
58 template < class InputT>
60 {
61 public:
66 
67  /// \brief Constructor
68  ///
69  /// \par
70  /// Construct a hierarchy of clusters from a binary tree.
71  ///
72  /// \param tree tree object underlying the clustering
73  HierarchicalClustering(const tree_type* tree)
74  : mep_tree(tree){
75  SHARK_RUNTIME_CHECK(tree, "[HierarchicalClustering] Tree must not be NULL");
76  }
77 
78  /// \brief From INameable: return the class name.
79  std::string name() const
80  { return "HierarchicalClustering"; }
81 
82  Shape inputShape() const{
83  return Shape();
84  }
85 
86  /// Return the number of clusters.
87  std::size_t numberOfClusters() const{
88  return (mep_tree->nodes() + 1) / 2;
89  }
90 
91  /// Return the best matching cluster for very pattern in the batch.
92  BatchOutputType hardMembership(BatchInputType const& patterns) const{
93  std::size_t numPatterns = batchSize(patterns);
94  BatchOutputType memberships(numPatterns);
95  for(std::size_t i = 0; i != numPatterns; ++i){
96  tree_type const* tree = mep_tree;
97  memberships(i) = 0;
98  while (tree->hasChildren()){
99  if (tree->isLeft(row(patterns,i))){
100  tree = tree->left();
101  }
102  else{
103  memberships(i) += unsigned((tree->left()->nodes() + 1) / 2);
104  tree = tree->right();
105  }
106  }
107  }
108  return memberships;
109  }
110 
111  /// from IParameterizable
112  RealVector parameterVector() const{
113  return RealVector();
114  }
115 
116  /// from IParameterizable
117  void setParameterVector(RealVector const& newParameters){
118  SHARK_ASSERT(newParameters.size() == 0);
119  }
120 
121  /// from IParameterizable
122  std::size_t numberOfParameters() const{
123  return 0;
124  }
125 
126 protected:
127  /// binary tree
128  tree_type const* mep_tree;
129 };
130 
131 
132 }
133 #endif