HierarchicalClustering.cpp
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Example program for 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 
38 
39 
40 using namespace shark;
41 
42 
43 int main(int argc, char** argv)
44 {
45  // create artificial data
46  std::size_t trainingSize = 20;
47  std::size_t testSize = 30;
48  std::vector<RealVector> tr_d(trainingSize, RealVector(1, 0.0));
49  std::vector<RealVector> te_d(testSize, RealVector(1, 0.0));
50  for (std::size_t i=0; i<trainingSize; i++)
51  tr_d[i](0) = 100.0 * (i + 0.5) / (double)trainingSize;
52  for (std::size_t i=0; i<testSize; i++)
53  te_d[i](0) = random::uni(random::globalRng,0.0, 100.0);
54 
57 
58  // construct a hierarchical clustering with at most 3 points per cluster
59  LCTree<RealVector> tree(training, TreeConstruction(0, 3));
60  HierarchicalClustering<RealVector> clustering(&tree);
61  HardClusteringModel<RealVector> model(&clustering);
62 
63  // output statistics
64  std::cout << "number of tree nodes: " << tree.nodes() << std::endl;
65  std::cout << "number of clusters: " << clustering.numberOfClusters() << std::endl;
66 
67  // output cluster assignments
68  std::cout << "\ntraining data:\n";
69  for (std::size_t i = 0; i != trainingSize; i++){
70  unsigned int cluster = model(training.element(i));
71  std::cout << " point " << training.element(i)(0) << " --> cluster " << cluster << std::endl;
72  }
73  std::cout << "\ntest data:\n";
74  for (std::size_t i=0; i<testSize; i++){
75  unsigned int cluster = model(test.element(i));
76  std::cout << " point " << test.element(i)(0) << " --> cluster " << cluster << std::endl;
77  }
78 }