kmeans
A C++ library for k-means
Loading...
Searching...
No Matches
InitializeRandom.hpp
Go to the documentation of this file.
1#ifndef KMEANS_INITIALIZE_RANDOM_HPP
2#define KMEANS_INITIALIZE_RANDOM_HPP
3
4#include <algorithm>
5#include <cstdint>
6#include <random>
7
8#include "Initialize.hpp"
9#include "copy_into_array.hpp"
10
11#include "aarand/aarand.hpp"
12
19namespace kmeans {
20
28 uint64_t seed = 6523u;
29};
30
42template<typename Index_, typename Data_, typename Cluster_, typename Float_, class Matrix_ = Matrix<Index_, Data_> >
43class InitializeRandom final : public Initialize<Index_, Data_, Cluster_, Float_, Matrix_> {
44private:
45 InitializeRandomOptions my_options;
46
47public:
51 InitializeRandom(InitializeRandomOptions options) : my_options(std::move(options)) {}
52
56 InitializeRandom() = default;
57
58public:
64 return my_options;
65 }
66
67public:
71 Cluster_ run(const Matrix_& data, Cluster_ ncenters, Float_* centers) const {
72 std::mt19937_64 eng(my_options.seed);
73 auto nobs = data.num_observations();
74 std::vector<Index_> chosen(std::min(nobs, static_cast<Index_>(ncenters)));
75 aarand::sample(nobs, ncenters, chosen.begin(), eng);
76 internal::copy_into_array(data, chosen, centers);
77 return chosen.size();
78 }
82};
83
84}
85
86#endif
Interface for k-means initialization.
Initialize by sampling random observations without replacement.
Definition InitializeRandom.hpp:43
InitializeRandom(InitializeRandomOptions options)
Definition InitializeRandom.hpp:51
InitializeRandomOptions & get_options()
Definition InitializeRandom.hpp:63
Interface for k-means initialization algorithms.
Definition Initialize.hpp:27
virtual Cluster_ run(const Matrix_ &data, Cluster_ num_centers, Float_ *centers) const =0
Namespace for k-means clustering.
Definition compute_wcss.hpp:12
Options to use for InitializeRandom.
Definition InitializeRandom.hpp:24
uint64_t seed
Definition InitializeRandom.hpp:28