Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Models
Clustering
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
* <https://shark-ml.github.io/Shark/>
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
39
#include <
shark/Models/Clustering/AbstractClustering.h
>
40
#include <
shark/Models/Trees/BinaryTree.h
>
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
/// \ingroup clustering
59
template
<
class
InputT>
60
class
HierarchicalClustering
:
public
AbstractClustering
<InputT>
61
{
62
public
:
63
typedef
AbstractClustering<InputT>
base_type
;
64
typedef
BinaryTree<InputT>
tree_type
;
65
typedef
typename
base_type::BatchInputType
BatchInputType
;
66
typedef
typename
base_type::BatchOutputType
BatchOutputType
;
67
68
/// \brief Constructor
69
///
70
/// \par
71
/// Construct a hierarchy of clusters from a binary tree.
72
///
73
/// \param tree tree object underlying the clustering
74
HierarchicalClustering
(
const
tree_type
* tree)
75
:
mep_tree
(tree){
76
SHARK_RUNTIME_CHECK
(tree,
"[HierarchicalClustering] Tree must not be NULL"
);
77
}
78
79
/// \brief From INameable: return the class name.
80
std::string
name
()
const
81
{
return
"HierarchicalClustering"
; }
82
83
Shape
inputShape
()
const
{
84
return
Shape
();
85
}
86
87
/// Return the number of clusters.
88
std::size_t
numberOfClusters
()
const
{
89
return
(
mep_tree
->
nodes
() + 1) / 2;
90
}
91
92
/// Return the best matching cluster for very pattern in the batch.
93
BatchOutputType
hardMembership
(
BatchInputType
const
& patterns)
const
{
94
std::size_t numPatterns =
batchSize
(patterns);
95
BatchOutputType
memberships(numPatterns);
96
for
(std::size_t i = 0; i != numPatterns; ++i){
97
tree_type
const
* tree =
mep_tree
;
98
memberships(i) = 0;
99
while
(tree->
hasChildren
()){
100
if
(tree->
isLeft
(row(patterns,i))){
101
tree = tree->
left
();
102
}
103
else
{
104
memberships(i) += unsigned((tree->
left
()->
nodes
() + 1) / 2);
105
tree = tree->
right
();
106
}
107
}
108
}
109
return
memberships;
110
}
111
112
/// from IParameterizable
113
RealVector
parameterVector
()
const
{
114
return
RealVector();
115
}
116
117
/// from IParameterizable
118
void
setParameterVector
(RealVector
const
& newParameters){
119
SHARK_ASSERT
(newParameters.size() == 0);
120
}
121
122
/// from IParameterizable
123
std::size_t
numberOfParameters
()
const
{
124
return
0;
125
}
126
127
protected
:
128
/// binary tree
129
tree_type
const
*
mep_tree
;
130
};
131
132
133
}
134
#endif