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
39template<class Matrix_ = SimpleMatrix<double, int>, typename Cluster_ = int, typename Float_ = double>
40class InitializeRandom : public Initialize<Matrix_, Cluster_, Float_> {
41private:
42 InitializeRandomOptions my_options;
43
44public:
48 InitializeRandom(InitializeRandomOptions options) : my_options(std::move(options)) {}
49
53 InitializeRandom() = default;
54
55public:
61 return my_options;
62 }
63
64public:
65 Cluster_ run(const Matrix_& data, Cluster_ ncenters, Float_* centers) const {
66 std::mt19937_64 eng(my_options.seed);
67 auto nobs = data.num_observations();
68 std::vector<typename Matrix_::index_type> chosen(std::min(nobs, static_cast<typename Matrix_::index_type>(ncenters)));
69 aarand::sample(nobs, ncenters, chosen.begin(), eng);
70 internal::copy_into_array(data, chosen, centers);
71 return chosen.size();
72 }
73};
74
75}
76
77#endif
Interface for k-means initialization.
Initialize by sampling random observations without replacement.
Definition InitializeRandom.hpp:40
Cluster_ run(const Matrix_ &data, Cluster_ ncenters, Float_ *centers) const
Definition InitializeRandom.hpp:65
InitializeRandom(InitializeRandomOptions options)
Definition InitializeRandom.hpp:48
InitializeRandomOptions & get_options()
Definition InitializeRandom.hpp:60
Base class for initialization algorithms.
Definition Initialize.hpp:22
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