jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Public Member Functions | Private Member Functions | Private Attributes | List of all members
jubatus::core::clustering::kmeans_clustering_method Class Reference

#include <kmeans_clustering_method.hpp>

Inheritance diagram for jubatus::core::clustering::kmeans_clustering_method:
Inheritance graph
Collaboration diagram for jubatus::core::clustering::kmeans_clustering_method:
Collaboration graph

Public Member Functions

void batch_update (wplist points)
 
wplist get_cluster (size_t cluster_id, const wplist &points) const
 
std::vector< wplistget_clusters (const wplist &points) const
 
std::vector< common::sfv_tget_k_center () const
 
common::sfv_t get_nearest_center (const common::sfv_t &point) const
 
int64_t get_nearest_center_index (const common::sfv_t &point) const
 
 kmeans_clustering_method (size_t k)
 
void online_update (wplist points)
 
 ~kmeans_clustering_method ()
 
- Public Member Functions inherited from jubatus::core::clustering::clustering_method
virtual ~clustering_method ()
 

Private Member Functions

void do_batch_update (wplist &points)
 
void initialize_centers (wplist &points)
 

Private Attributes

size_t k_
 
std::vector< common::sfv_tkcenters_
 

Detailed Description

Definition at line 27 of file kmeans_clustering_method.hpp.

Constructor & Destructor Documentation

jubatus::core::clustering::kmeans_clustering_method::kmeans_clustering_method ( size_t  k)
explicit
jubatus::core::clustering::kmeans_clustering_method::~kmeans_clustering_method ( )

Definition at line 36 of file kmeans_clustering_method.cpp.

36  {
37 }

Member Function Documentation

void jubatus::core::clustering::kmeans_clustering_method::batch_update ( wplist  points)
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 39 of file kmeans_clustering_method.cpp.

References do_batch_update(), initialize_centers(), and kcenters_.

39  {
40  if (points.empty()) {
41  kcenters_.clear();
42  return;
43  }
44  initialize_centers(points);
45  do_batch_update(points);
46 }

Here is the call graph for this function:

void jubatus::core::clustering::kmeans_clustering_method::do_batch_update ( wplist points)
private

Definition at line 66 of file kmeans_clustering_method.cpp.

References jubatus::core::clustering::dist(), k_, kcenters_, jubatus::core::clustering::min_dist(), jubatus::core::clustering::scalar_dot(), and jubatus::core::clustering::scalar_mul_and_add().

Referenced by batch_update().

66  {
67  static jubatus::util::math::random::mtrand r;
68  bool terminated = false;
69  if (points.size() < k_) {
70  return;
71  }
72  while (!terminated) {
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) {
76  pair<int64_t, double> m = min_dist((*it).data, kcenters_);
77  scalar_mul_and_add(it->data, it->weight, kcenters_new[m.first]);
78  center_count[m.first] += it->weight;
79  }
80  terminated = true;
81  for (size_t i = 0; i < k_; ++i) {
82  if (center_count[i] == 0) {
83  kcenters_new[i] = kcenters_[i];
84  continue;
85  }
86  kcenters_new[i] = scalar_dot(kcenters_new[i], 1.0 / center_count[i]);
87  double d = dist(kcenters_new[i], kcenters_[i]);
88  if (d > 1e-9) {
89  terminated = false;
90  }
91  }
92  kcenters_ = kcenters_new;
93  }
94 }
void scalar_mul_and_add(const common::sfv_t &left, float s, common::sfv_t &right)
Definition: util.cpp:62
common::sfv_t scalar_dot(const common::sfv_t &p, double s)
Definition: util.cpp:143
double dist(const common::sfv_t &p1, const common::sfv_t &p2)
Definition: util.cpp:151
pair< size_t, double > min_dist(const common::sfv_t &p, const vector< common::sfv_t > &P)
Definition: util.cpp:182

Here is the call graph for this function:

Here is the caller graph for this function:

wplist jubatus::core::clustering::kmeans_clustering_method::get_cluster ( size_t  cluster_id,
const wplist points 
) const
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 113 of file kmeans_clustering_method.cpp.

References get_clusters(), and k_.

115  {
116  if (cluster_id >= k_) {
117  return wplist();
118  }
119  return get_clusters(points)[cluster_id];
120 }
std::vector< wplist > get_clusters(const wplist &points) const
std::vector< weighted_point > wplist
Definition: types.hpp:55

Here is the call graph for this function:

vector< wplist > jubatus::core::clustering::kmeans_clustering_method::get_clusters ( const wplist points) const
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 122 of file kmeans_clustering_method.cpp.

References k_, kcenters_, and jubatus::core::clustering::min_dist().

Referenced by get_cluster().

123  {
124  vector<wplist> ret(k_);
125  for (wplist::const_iterator it = points.begin(); it != points.end(); ++it) {
126  pair<int64_t, double> m = min_dist(it->data, kcenters_);
127  ret[m.first].push_back(*it);
128  }
129  return ret;
130 }
pair< size_t, double > min_dist(const common::sfv_t &p, const vector< common::sfv_t > &P)
Definition: util.cpp:182

Here is the call graph for this function:

Here is the caller graph for this function:

vector< common::sfv_t > jubatus::core::clustering::kmeans_clustering_method::get_k_center ( ) const
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 99 of file kmeans_clustering_method.cpp.

References kcenters_.

99  {
100  return kcenters_;
101 }
common::sfv_t jubatus::core::clustering::kmeans_clustering_method::get_nearest_center ( const common::sfv_t point) const
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 108 of file kmeans_clustering_method.cpp.

References get_nearest_center_index(), and kcenters_.

109  {
110  return kcenters_[get_nearest_center_index(point)];
111 }
int64_t get_nearest_center_index(const common::sfv_t &point) const

Here is the call graph for this function:

int64_t jubatus::core::clustering::kmeans_clustering_method::get_nearest_center_index ( const common::sfv_t point) const
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 103 of file kmeans_clustering_method.cpp.

References kcenters_, and jubatus::core::clustering::min_dist().

Referenced by get_nearest_center().

104  {
105  return min_dist(point, kcenters_).first;
106 }
pair< size_t, double > min_dist(const common::sfv_t &p, const vector< common::sfv_t > &P)
Definition: util.cpp:182

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::clustering::kmeans_clustering_method::initialize_centers ( wplist points)
private

Definition at line 48 of file kmeans_clustering_method.cpp.

References k_, kcenters_, and jubatus::core::clustering::min_dist().

Referenced by batch_update().

48  {
49  if (points.size() < k_) {
50  return;
51  }
52  kcenters_.clear();
53  kcenters_.push_back(points[0].data);
54  vector<double> weights;
55  while (kcenters_.size() < k_) {
56  weights.clear();
57  for (wplist::iterator it = points.begin(); it != points.end(); ++it) {
58  pair<int64_t, double> m = min_dist((*it).data, kcenters_);
59  weights.push_back(m.second * it->weight);
60  }
61  discrete_distribution d(weights.begin(), weights.end());
62  kcenters_.push_back(points[d()].data);
63  }
64 }
pair< size_t, double > min_dist(const common::sfv_t &p, const vector< common::sfv_t > &P)
Definition: util.cpp:182

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::clustering::kmeans_clustering_method::online_update ( wplist  points)
virtual

Implements jubatus::core::clustering::clustering_method.

Definition at line 96 of file kmeans_clustering_method.cpp.

96  {
97 }

Member Data Documentation

size_t jubatus::core::clustering::kmeans_clustering_method::k_
private
std::vector<common::sfv_t> jubatus::core::clustering::kmeans_clustering_method::kcenters_
private

The documentation for this class was generated from the following files: