22 #include "../common/exception.hpp"
30 namespace clustering {
49 if (points.size() <
k_) {
54 vector<double> weights;
57 for (wplist::iterator it = points.begin(); it != points.end(); ++it) {
59 weights.push_back(m.second * it->weight);
67 static jubatus::util::math::random::mtrand r;
68 bool terminated =
false;
69 if (points.size() <
k_) {
73 vector<common::sfv_t> kcenters_new(
k_);
74 vector<double> center_count(
k_, 0);
75 for (wplist::iterator it = points.begin(); it != points.end(); ++it) {
78 center_count[m.first] += it->weight;
81 for (
size_t i = 0; i <
k_; ++i) {
82 if (center_count[i] == 0) {
86 kcenters_new[i] =
scalar_dot(kcenters_new[i], 1.0 / center_count[i]);
115 const wplist& points)
const {
116 if (cluster_id >=
k_) {
123 const wplist& points)
const {
124 vector<wplist> ret(
k_);
125 for (wplist::const_iterator it = points.begin(); it != points.end(); ++it) {
127 ret[m.first].push_back(*it);
std::vector< wplist > get_clusters(const wplist &points) const
void scalar_mul_and_add(const common::sfv_t &left, float s, common::sfv_t &right)
common::sfv_t scalar_dot(const common::sfv_t &p, double s)
void batch_update(wplist points)
std::vector< common::sfv_t > kcenters_
double dist(const common::sfv_t &p1, const common::sfv_t &p2)
kmeans_clustering_method(size_t k)
void do_batch_update(wplist &points)
common::sfv_t get_nearest_center(const common::sfv_t &point) const
wplist get_cluster(size_t cluster_id, const wplist &points) const
std::vector< common::sfv_t > get_k_center() const
pair< size_t, double > min_dist(const common::sfv_t &p, const vector< common::sfv_t > &P)
int64_t get_nearest_center_index(const common::sfv_t &point) const
std::vector< std::pair< std::string, float > > sfv_t
void online_update(wplist points)
void initialize_centers(wplist &points)
std::vector< weighted_point > wplist
~kmeans_clustering_method()