kmeans
A C++ library for k-means
Loading...
Searching...
No Matches
kmeans.hpp
Go to the documentation of this file.
1#ifndef KMEANS_KMEANS_HPP
2#define KMEANS_KMEANS_HPP
3
4#include "Details.hpp"
5#include "Refine.hpp"
6#include "Initialize.hpp"
7#include "Matrix.hpp"
8#include "SimpleMatrix.hpp"
9
11#include "InitializeRandom.hpp"
13#include "InitializeNone.hpp"
14
16#include "RefineLloyd.hpp"
17#include "RefineMiniBatch.hpp"
18
19#include "compute_wcss.hpp"
20
31namespace kmeans {
32
52template<typename Index_, typename Data_, typename Cluster_, typename Float_, class Matrix_ = Matrix<Index_, Data_> >
54 const Matrix_& data,
57 Cluster_ num_centers,
58 Float_* centers,
59 Cluster_* clusters)
60{
61 auto actual_centers = initialize.run(data, num_centers, centers);
62 auto output = refine.run(data, actual_centers, centers, clusters);
63 output.sizes.resize(num_centers); // restoring the full size.
64 return output;
65}
66
86template<typename Index_, typename Data_, typename Cluster_, typename Float_>
88 const Matrix<Index_, Data_>& data,
89 const Initialize<Index_, Data_, Cluster_, Float_, Matrix<Index_, Data_> >& initialize,
90 const Refine<Index_, Data_, Cluster_, Float_, Matrix<Index_, Data_> >& refine,
91 Cluster_ num_centers,
92 Float_* centers,
93 Cluster_* clusters)
94{
95 return compute<Index_, Data_, Cluster_, Float_, Matrix<Index_, Data_> >(data, initialize, refine, num_centers, centers, clusters);
96}
97
101template<typename Index_, typename Cluster_, typename Float_>
102struct Results {
106 template<typename Dim_>
107 Results(Dim_ num_dimensions, Index_ num_observations, Cluster_ num_centers) :
108 centers(num_dimensions * num_centers), clusters(num_observations) {}
109
110 Results() = default;
118 std::vector<Cluster_> clusters;
119
124 std::vector<Float_> centers;
125
130};
131
150template<typename Index_, typename Data_, typename Cluster_, typename Float_, class Matrix_ = Matrix<Index_, Data_> >
152 const Matrix_& data,
155 Cluster_ num_centers)
156{
158 output.clusters.resize(data.num_observations());
159 output.centers.resize(static_cast<size_t>(num_centers) * static_cast<size_t>(data.num_dimensions()));
160 output.details = compute(data, initialize, refine, num_centers, output.centers.data(), output.clusters.data());
161 return output;
162}
163
181template<typename Index_, typename Data_, typename Cluster_, typename Float_>
183 const Matrix<Index_, Data_>& data,
184 const Initialize<Index_, Data_, Cluster_, Float_, Matrix<Index_, Data_> >& initialize,
185 const Refine<Index_, Data_, Cluster_, Float_, Matrix<Index_, Data_> >& refine,
186 Cluster_ num_centers)
187{
188 return compute<Index_, Data_, Cluster_, Float_, Matrix<Index_, Data_> >(data, initialize, refine, num_centers);
189}
190
191}
192
193#endif
Report detailed clustering statistics.
Class for kmeans++ initialization.
Class for no initialization.
Class for random initialization.
k-means initialization with variance partitioning.
Interface for k-means initialization.
Interface for matrix inputs.
Implements the Hartigan-Wong algorithm for k-means clustering.
Implements the Lloyd algorithm for k-means clustering.
Implements the mini-batch algorithm for k-means clustering.
Interface for k-means refinement.
Wrapper for a simple dense matrix.
Interface for k-means initialization algorithms.
Definition Initialize.hpp:27
virtual Cluster_ run(const Matrix_ &data, Cluster_ num_centers, Float_ *centers) const =0
Interface for matrix data.
Definition Matrix.hpp:130
Interface for k-means refinement algorithms.
Definition Refine.hpp:26
virtual Details< Index_ > run(const Matrix_ &data, Cluster_ num_centers, Float_ *centers, Cluster_ *clusters) const =0
Compute within-cluster sum of squares.
Namespace for k-means clustering.
Definition compute_wcss.hpp:12
Details< Index_ > compute(const Matrix_ &data, const Initialize< Index_, Data_, Cluster_, Float_, Matrix_ > &initialize, const Refine< Index_, Data_, Cluster_, Float_, Matrix_ > &refine, Cluster_ num_centers, Float_ *centers, Cluster_ *clusters)
Definition kmeans.hpp:53
Additional statistics from the k-means algorithm.
Definition Details.hpp:20
Full statistics from k-means clustering.
Definition kmeans.hpp:102
std::vector< Float_ > centers
Definition kmeans.hpp:124
Details< Index_ > details
Definition kmeans.hpp:129
std::vector< Cluster_ > clusters
Definition kmeans.hpp:118