kmeans.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_KMEANS_KMEANS_HPP
13 #define MLPACK_METHODS_KMEANS_KMEANS_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
20 #include "naive_kmeans.hpp"
21 
23 
24 namespace mlpack {
25 namespace kmeans {
26 
68 template<typename MetricType = metric::EuclideanDistance,
69  typename InitialPartitionPolicy = SampleInitialization,
70  typename EmptyClusterPolicy = MaxVarianceNewCluster,
71  template<class, class> class LloydStepType = NaiveKMeans,
72  typename MatType = arma::mat>
73 class KMeans
74 {
75  public:
89  KMeans(const size_t maxIterations = 1000,
90  const MetricType metric = MetricType(),
91  const InitialPartitionPolicy partitioner = InitialPartitionPolicy(),
92  const EmptyClusterPolicy emptyClusterAction = EmptyClusterPolicy());
93 
94 
108  void Cluster(const MatType& data,
109  const size_t clusters,
110  arma::Row<size_t>& assignments,
111  const bool initialGuess = false);
112 
126  void Cluster(const MatType& data,
127  size_t clusters,
128  arma::mat& centroids,
129  const bool initialGuess = false);
130 
151  void Cluster(const MatType& data,
152  const size_t clusters,
153  arma::Row<size_t>& assignments,
154  arma::mat& centroids,
155  const bool initialAssignmentGuess = false,
156  const bool initialCentroidGuess = false);
157 
159  size_t MaxIterations() const { return maxIterations; }
161  size_t& MaxIterations() { return maxIterations; }
162 
164  const MetricType& Metric() const { return metric; }
166  MetricType& Metric() { return metric; }
167 
169  const InitialPartitionPolicy& Partitioner() const { return partitioner; }
171  InitialPartitionPolicy& Partitioner() { return partitioner; }
172 
174  const EmptyClusterPolicy& EmptyClusterAction() const
175  { return emptyClusterAction; }
177  EmptyClusterPolicy& EmptyClusterAction() { return emptyClusterAction; }
178 
180  template<typename Archive>
181  void serialize(Archive& ar, const uint32_t version);
182 
183  private:
185  size_t maxIterations;
187  MetricType metric;
189  InitialPartitionPolicy partitioner;
191  EmptyClusterPolicy emptyClusterAction;
192 };
193 
194 } // namespace kmeans
195 } // namespace mlpack
196 
197 // Include implementation.
198 #include "kmeans_impl.hpp"
199 
200 #endif // MLPACK_METHODS_KMEANS_KMEANS_HPP
void serialize(Archive &ar, const uint32_t version)
Serialize the k-means object.
void Cluster(const MatType &data, const size_t clusters, arma::Row< size_t > &assignments, const bool initialGuess=false)
Perform k-means clustering on the data, returning a list of cluster assignments.
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
InitialPartitionPolicy & Partitioner()
Modify the initial partitioning policy.
Definition: kmeans.hpp:171
KMeans(const size_t maxIterations=1000, const MetricType metric=MetricType(), const InitialPartitionPolicy partitioner=InitialPartitionPolicy(), const EmptyClusterPolicy emptyClusterAction=EmptyClusterPolicy())
Create a K-Means object and (optionally) set the parameters which K-Means will be run with...
const InitialPartitionPolicy & Partitioner() const
Get the initial partitioning policy.
Definition: kmeans.hpp:169
size_t & MaxIterations()
Set the maximum number of iterations.
Definition: kmeans.hpp:161
const MetricType & Metric() const
Get the distance metric.
Definition: kmeans.hpp:164
MetricType & Metric()
Modify the distance metric.
Definition: kmeans.hpp:166
const EmptyClusterPolicy & EmptyClusterAction() const
Get the empty cluster policy.
Definition: kmeans.hpp:174
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: kmeans.hpp:159
EmptyClusterPolicy & EmptyClusterAction()
Modify the empty cluster policy.
Definition: kmeans.hpp:177
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
This class implements K-Means clustering, using a variety of possible implementations of Lloyd&#39;s algo...
Definition: kmeans.hpp:73