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 "MockMatrix.hpp"
8
10#include "InitializeRandom.hpp"
12#include "InitializeNone.hpp"
13
15#include "RefineLloyd.hpp"
16#include "RefineMiniBatch.hpp"
17
18#include "compute_wcss.hpp"
19
30namespace kmeans {
31
48template<class Matrix_, typename Cluster_, typename Float_>
50 const Matrix_& data,
54 Float_* centers,
55 Cluster_* clusters)
56{
58 auto output = refine.run(data, actual_centers, centers, clusters);
59 output.sizes.resize(num_centers); // restoring the full size.
60 return output;
61}
62
66template<typename Cluster_, typename Float_, typename Index_>
67struct Results {
71 template<typename Dim_>
72 Results(Dim_ num_dimensions, Index_ num_observations, Cluster_ num_centers) :
73 centers(num_dimensions * num_centers), clusters(num_observations) {}
74
75 Results() = default;
83 std::vector<Cluster_> clusters;
84
89 std::vector<Float_> centers;
90
95};
96
112template<class Matrix_, typename Cluster_, typename Float_>
114 const Matrix_& data,
118{
120 output.clusters.resize(data.num_observations());
121 output.centers.resize(static_cast<size_t>(num_centers) * static_cast<size_t>(data.num_dimensions()));
122 output.details = compute(data, initialize, refine, num_centers, output.centers.data(), output.clusters.data());
123 return output;
124}
125
126}
127
128#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.
Expectations 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.
Implements the variance partitioning method of Su and Dy (2007).
Definition InitializeVariancePartition.hpp:164
Cluster_ run(const Matrix_ &data, Cluster_ ncenters, Float_ *centers) const
Definition InitializeVariancePartition.hpp:189
Compute within-cluster sum of squares.
Namespace for k-means clustering.
Definition compute_wcss.hpp:12
Details< typename Matrix_::index_type > compute(const Matrix_ &data, const Initialize< Matrix_, Cluster_, Float_ > &initialize, const Refine< Matrix_, Cluster_, Float_ > &refine, Cluster_ num_centers, Float_ *centers, Cluster_ *clusters)
Definition kmeans.hpp:49
Full statistics from k-means clustering.
Definition kmeans.hpp:67
std::vector< Float_ > centers
Definition kmeans.hpp:89
std::vector< Cluster_ > clusters
Definition kmeans.hpp:83
Details< Index_ > details
Definition kmeans.hpp:94