Vespucci  1.0.0
kmeanswrapper.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
21 #include <mlpack/methods/kmeans/kmeans.hpp>
22 #include <mlpack/methods/kmeans/random_partition.hpp>
23 #include <mlpack/methods/kmeans/refined_start.hpp>
24 #include <mlpack/methods/kmeans/allow_empty_clusters.hpp>
25 Vespucci::Math::KMeansWrapper::KMeansWrapper(const std::string &init, const std::string &metric, bool allow_empty)
26  :init_(init), metric_(metric)
27 {
28  allow_empty_ = allow_empty;
29  if ((init_ != "sampleinitialization")
30  && (init_ != "randompartition")
31  && (init_ != "refinedstart")){
32  throw std::invalid_argument("invalid initialization policy");}
33 
34  if ((metric_ != "squaredeuclidean")
35  && (metric_ != "euclidean")
36  && (metric_ != "manhattan")
37  && (metric_ != "chebyshev")){
38  throw std::invalid_argument("invalid distance metric");
39  }
40 }
41 
43 {
44 
45 }
46 
47 arma::vec Vespucci::Math::KMeansWrapper::Cluster(const arma::mat &data, const size_t clusters, arma::mat &centroids)
48 {
49  using namespace mlpack::metric;
50  using namespace mlpack::kmeans;
51  arma::Row<size_t> assignments;
52  arma::vec assignments_vec;
53  if (allow_empty_){
54  if (metric_ == "squaredeuclidean"){
55  if (init_ == "sampleinitialization"){
56  KMeans<SquaredEuclideanDistance, SampleInitialization, AllowEmptyClusters> k;
57  k.Cluster(data, clusters, assignments, centroids);
58  }
59  else if (init_ == "randompartition"){
60  KMeans<SquaredEuclideanDistance, RandomPartition, AllowEmptyClusters> k;
61  k.Cluster(data, clusters, assignments, centroids);
62  }
63  else if (init_ == "refinedstart"){
64  KMeans<SquaredEuclideanDistance, RefinedStart, AllowEmptyClusters> k;
65  k.Cluster(data, clusters, assignments, centroids);
66  }
67  }
68  else if (metric_ == "euclidean"){
69  if (init_ == "sampleinitialization"){
70  KMeans<EuclideanDistance, SampleInitialization, AllowEmptyClusters> k;
71  k.Cluster(data, clusters, assignments, centroids);
72  }
73  else if (init_ == "randompartition"){
74  KMeans<EuclideanDistance, RandomPartition, AllowEmptyClusters> k;
75  k.Cluster(data, clusters, assignments, centroids);
76  }
77  else if (init_ == "refinedstart"){
78  KMeans<EuclideanDistance, RefinedStart, AllowEmptyClusters> k;
79  k.Cluster(data, clusters, assignments, centroids);
80  }
81  }
82  else if (metric_ == "manhattan"){
83  if (init_ == "sampleinitialization"){
84  KMeans<ManhattanDistance, SampleInitialization, AllowEmptyClusters> k;
85  k.Cluster(data, clusters, assignments, centroids);
86  }
87  else if (init_ == "randompartition"){
88  KMeans<ManhattanDistance, RandomPartition, AllowEmptyClusters> k;
89  k.Cluster(data, clusters, assignments, centroids);
90  }
91  else if (init_ == "refinedstart"){
92  KMeans<ManhattanDistance, RefinedStart, AllowEmptyClusters> k;
93  k.Cluster(data, clusters, assignments, centroids);
94  }
95  }
96  else if (metric_ == "chebyshev"){
97  if (init_ == "sampleinitialization"){
98  KMeans<ChebyshevDistance, SampleInitialization, AllowEmptyClusters> k;
99  k.Cluster(data, clusters, assignments, centroids);
100  }
101  else if (init_ == "randompartition"){
102  KMeans<ChebyshevDistance, RandomPartition, AllowEmptyClusters> k;
103  k.Cluster(data, clusters, assignments, centroids);
104  }
105  else if (init_ == "refinedstart"){
106  KMeans<ChebyshevDistance, RefinedStart, AllowEmptyClusters> k;
107  k.Cluster(data, clusters, assignments, centroids);
108  }
109  }
110  }
111  else{
112  if (metric_ == "squaredeuclidean"){
113  if (init_ == "sampleinitialization"){
114  KMeans<SquaredEuclideanDistance, SampleInitialization> k;
115  k.Cluster(data, clusters, assignments, centroids);
116  }
117  else if (init_ == "randompartition"){
118  KMeans<SquaredEuclideanDistance, RandomPartition> k;
119  k.Cluster(data, clusters, assignments, centroids);
120  }
121  else if (init_ == "refinedstart"){
122  KMeans<SquaredEuclideanDistance, RefinedStart> k;
123  k.Cluster(data, clusters, assignments, centroids);
124  }
125  }
126  else if (metric_ == "euclidean"){
127  if (init_ == "sampleinitialization"){
128  KMeans<EuclideanDistance, SampleInitialization> k;
129  k.Cluster(data, clusters, assignments, centroids);
130  }
131  else if (init_ == "randompartition"){
132  KMeans<EuclideanDistance, RandomPartition> k;
133  k.Cluster(data, clusters, assignments, centroids);
134  }
135  else if (init_ == "refinedstart"){
136  KMeans<EuclideanDistance, RefinedStart> k;
137  k.Cluster(data, clusters, assignments, centroids);
138  }
139  }
140  else if (metric_ == "manhattan"){
141  if (init_ == "sampleinitialization"){
142  KMeans<ManhattanDistance, SampleInitialization> k;
143  k.Cluster(data, clusters, assignments, centroids);
144  }
145  else if (init_ == "randompartition"){
146  KMeans<ManhattanDistance, RandomPartition> k;
147  k.Cluster(data, clusters, assignments, centroids);
148  }
149  else if (init_ == "refinedstart"){
150  KMeans<ManhattanDistance, RefinedStart> k;
151  k.Cluster(data, clusters, assignments, centroids);
152  }
153  }
154  else if (metric_ == "chebyshev"){
155  if (init_ == "sampleinitialization"){
156  KMeans<ChebyshevDistance, SampleInitialization> k;
157  k.Cluster(data, clusters, assignments, centroids);
158  }
159  else if (init_ == "randompartition"){
160  KMeans<ChebyshevDistance, RandomPartition> k;
161  k.Cluster(data, clusters, assignments, centroids);
162  }
163  else if (init_ == "refinedstart"){
164  KMeans<ChebyshevDistance, RefinedStart> k;
165  k.Cluster(data, clusters, assignments, centroids);
166  }
167  }
168  }
169 
170  assignments_vec.set_size(assignments.n_elem);
171  for (arma::uword i = 0; i < assignments.n_elem; ++i)
172  assignments_vec(i) = double(assignments(i) + 1);
173  return assignments_vec;
174 }
arma::vec Cluster(const arma::mat &data, const size_t clusters, arma::mat &centroids)
KMeansWrapper(const std::string &init, const std::string &metric, bool allow_empty)