All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
k_means_clustering.hpp
Go to the documentation of this file.
1 
8 #ifndef PAAL_K_MEANS_CLUSTERING_HPP
9 #define PAAL_K_MEANS_CLUSTERING_HPP
10 
12 #include "paal/utils/irange.hpp"
13 
14 #include <boost/range/algorithm_ext/iota.hpp>
15 #include <boost/range/empty.hpp>
16 
17 #include <vector>
18 
19 namespace paal {
20 
24 template <typename Cluster, typename OutputIterator>
25 void centroid_minimalize_w_c_s_s(Cluster && cluster, OutputIterator out) {
26  assert(!boost::empty(cluster));
27  using point_t = range_to_elem_t<Cluster>;
28  using coordinate_t = range_to_elem_t<point_t>;
29 
30  auto dim = boost::size(*std::begin(cluster));
31  for(auto idx : irange(dim)) {
32  coordinate_t res{};
33  for (auto && point : cluster) {
34  res += std::begin(point)[idx];
35  }
36  *out = res / boost::size(cluster);
37  ++out;
38  }
39 }
40 
48 template <typename Clusters, typename OutputIterator>
49 void centroids_minimalize_w_c_s_s(Clusters && clusters, OutputIterator out) {
50  assert(!boost::empty(clusters));
51  assert(!boost::empty(*std::begin(clusters)));
52 
53  using cluster_t = range_to_elem_t<Clusters>;
54  using point_t = range_to_elem_t<cluster_t>;
55  using coordinate_t = range_to_elem_t<point_t>;
56 
57  auto size = boost::size(*std::begin(*begin(clusters)));
58  for (auto && cluster : clusters) {
59  std::vector<coordinate_t> point(size);
60  centroid_minimalize_w_c_s_s(cluster, point.begin());
61  *out = point;
62  ++out;
63  }
64 }
65 
83 template <typename Points, class Centers, class OutputIterator,
84  class Visitor = k_means_visitor>
85 auto k_means(Points &&points, Centers &&centers, OutputIterator result,
86  Visitor visitor = Visitor{}) {
87  using point_t = range_to_elem_t<Points>;
88  using center_t = range_to_elem_t<Centers>;
89 
90  center_t center{ *std::begin(centers) };
91 
92  return k_means(
93  points, centers,
94  [&](std::vector<point_t> const & points)->center_t const & {
95  centroid_minimalize_w_c_s_s(points, std::begin(center));
96  return center;
97  },
98  [&](point_t const &point) { return closest_to(point, centers); },
99  result, utils::equal_to{}, visitor);
100 }
101 
102 }
103 
104 #endif /* PAAL_K_MEANS_CLUSTERING_HPP */
typename boost::range_value< Range >::type range_to_elem_t
for given range returns type of its element
void centroid_minimalize_w_c_s_s(Cluster &&cluster, OutputIterator out)
return centroid that minimize within-cluster sum of squares
equal_to functor
Definition: functors.hpp:556
auto closest_to(Point &&point, Centers &&centers)
auto k_means(Points &&points, Centers &&centers, OutputIterator result, Visitor visitor=Visitor{})
this is solve k_means_clustering problem and return vector of cluster example:
auto irange(T begin, T end)
irange
Definition: irange.hpp:22
void centroids_minimalize_w_c_s_s(Clusters &&clusters, OutputIterator out)
centroid minimize within cluster sum of squares