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> 26 :init_(init), metric_(metric)
28 allow_empty_ = allow_empty;
29 if ((init_ !=
"sampleinitialization")
30 && (init_ !=
"randompartition")
31 && (init_ !=
"refinedstart")){
32 throw std::invalid_argument(
"invalid initialization policy");}
34 if ((metric_ !=
"squaredeuclidean")
35 && (metric_ !=
"euclidean")
36 && (metric_ !=
"manhattan")
37 && (metric_ !=
"chebyshev")){
38 throw std::invalid_argument(
"invalid distance metric");
49 using namespace mlpack::metric;
50 using namespace mlpack::kmeans;
51 arma::Row<size_t> assignments;
52 arma::vec assignments_vec;
54 if (metric_ ==
"squaredeuclidean"){
55 if (init_ ==
"sampleinitialization"){
56 KMeans<SquaredEuclideanDistance, SampleInitialization, AllowEmptyClusters> k;
57 k.Cluster(data, clusters, assignments, centroids);
59 else if (init_ ==
"randompartition"){
60 KMeans<SquaredEuclideanDistance, RandomPartition, AllowEmptyClusters> k;
61 k.Cluster(data, clusters, assignments, centroids);
63 else if (init_ ==
"refinedstart"){
64 KMeans<SquaredEuclideanDistance, RefinedStart, AllowEmptyClusters> k;
65 k.Cluster(data, clusters, assignments, centroids);
68 else if (metric_ ==
"euclidean"){
69 if (init_ ==
"sampleinitialization"){
70 KMeans<EuclideanDistance, SampleInitialization, AllowEmptyClusters> k;
71 k.Cluster(data, clusters, assignments, centroids);
73 else if (init_ ==
"randompartition"){
74 KMeans<EuclideanDistance, RandomPartition, AllowEmptyClusters> k;
75 k.Cluster(data, clusters, assignments, centroids);
77 else if (init_ ==
"refinedstart"){
78 KMeans<EuclideanDistance, RefinedStart, AllowEmptyClusters> k;
79 k.Cluster(data, clusters, assignments, centroids);
82 else if (metric_ ==
"manhattan"){
83 if (init_ ==
"sampleinitialization"){
84 KMeans<ManhattanDistance, SampleInitialization, AllowEmptyClusters> k;
85 k.Cluster(data, clusters, assignments, centroids);
87 else if (init_ ==
"randompartition"){
88 KMeans<ManhattanDistance, RandomPartition, AllowEmptyClusters> k;
89 k.Cluster(data, clusters, assignments, centroids);
91 else if (init_ ==
"refinedstart"){
92 KMeans<ManhattanDistance, RefinedStart, AllowEmptyClusters> k;
93 k.Cluster(data, clusters, assignments, centroids);
96 else if (metric_ ==
"chebyshev"){
97 if (init_ ==
"sampleinitialization"){
98 KMeans<ChebyshevDistance, SampleInitialization, AllowEmptyClusters> k;
99 k.Cluster(data, clusters, assignments, centroids);
101 else if (init_ ==
"randompartition"){
102 KMeans<ChebyshevDistance, RandomPartition, AllowEmptyClusters> k;
103 k.Cluster(data, clusters, assignments, centroids);
105 else if (init_ ==
"refinedstart"){
106 KMeans<ChebyshevDistance, RefinedStart, AllowEmptyClusters> k;
107 k.Cluster(data, clusters, assignments, centroids);
112 if (metric_ ==
"squaredeuclidean"){
113 if (init_ ==
"sampleinitialization"){
114 KMeans<SquaredEuclideanDistance, SampleInitialization> k;
115 k.Cluster(data, clusters, assignments, centroids);
117 else if (init_ ==
"randompartition"){
118 KMeans<SquaredEuclideanDistance, RandomPartition> k;
119 k.Cluster(data, clusters, assignments, centroids);
121 else if (init_ ==
"refinedstart"){
122 KMeans<SquaredEuclideanDistance, RefinedStart> k;
123 k.Cluster(data, clusters, assignments, centroids);
126 else if (metric_ ==
"euclidean"){
127 if (init_ ==
"sampleinitialization"){
128 KMeans<EuclideanDistance, SampleInitialization> k;
129 k.Cluster(data, clusters, assignments, centroids);
131 else if (init_ ==
"randompartition"){
132 KMeans<EuclideanDistance, RandomPartition> k;
133 k.Cluster(data, clusters, assignments, centroids);
135 else if (init_ ==
"refinedstart"){
136 KMeans<EuclideanDistance, RefinedStart> k;
137 k.Cluster(data, clusters, assignments, centroids);
140 else if (metric_ ==
"manhattan"){
141 if (init_ ==
"sampleinitialization"){
142 KMeans<ManhattanDistance, SampleInitialization> k;
143 k.Cluster(data, clusters, assignments, centroids);
145 else if (init_ ==
"randompartition"){
146 KMeans<ManhattanDistance, RandomPartition> k;
147 k.Cluster(data, clusters, assignments, centroids);
149 else if (init_ ==
"refinedstart"){
150 KMeans<ManhattanDistance, RefinedStart> k;
151 k.Cluster(data, clusters, assignments, centroids);
154 else if (metric_ ==
"chebyshev"){
155 if (init_ ==
"sampleinitialization"){
156 KMeans<ChebyshevDistance, SampleInitialization> k;
157 k.Cluster(data, clusters, assignments, centroids);
159 else if (init_ ==
"randompartition"){
160 KMeans<ChebyshevDistance, RandomPartition> k;
161 k.Cluster(data, clusters, assignments, centroids);
163 else if (init_ ==
"refinedstart"){
164 KMeans<ChebyshevDistance, RefinedStart> k;
165 k.Cluster(data, clusters, assignments, centroids);
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;
arma::vec Cluster(const arma::mat &data, const size_t clusters, arma::mat ¢roids)
KMeansWrapper(const std::string &init, const std::string &metric, bool allow_empty)