8 #ifndef PAAL_K_MEANS_CLUSTERING_HPP
9 #define PAAL_K_MEANS_CLUSTERING_HPP
14 #include <boost/range/algorithm_ext/iota.hpp>
15 #include <boost/range/empty.hpp>
24 template <
typename Cluster,
typename OutputIterator>
26 assert(!boost::empty(cluster));
30 auto dim = boost::size(*std::begin(cluster));
31 for(
auto idx :
irange(dim)) {
33 for (
auto && point : cluster) {
34 res += std::begin(point)[idx];
36 *out = res / boost::size(cluster);
48 template <
typename Clusters,
typename OutputIterator>
50 assert(!boost::empty(clusters));
51 assert(!boost::empty(*std::begin(clusters)));
57 auto size = boost::size(*std::begin(*begin(clusters)));
58 for (
auto && cluster : clusters) {
59 std::vector<coordinate_t> point(size);
83 template <
typename Points,
class Centers,
class OutputIterator,
84 class Visitor = k_means_visitor>
85 auto k_means(Points &&points, Centers &¢ers, OutputIterator result,
86 Visitor visitor = Visitor{}) {
87 using point_t = range_to_elem_t<Points>;
88 using center_t = range_to_elem_t<Centers>;
90 center_t center{ *std::begin(centers) };
94 [&](std::vector<point_t>
const & points)->center_t
const & {
98 [&](point_t
const &point) {
return closest_to(point, centers); },
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
auto closest_to(Point &&point, Centers &¢ers)
auto k_means(Points &&points, Centers &¢ers, 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
void centroids_minimalize_w_c_s_s(Clusters &&clusters, OutputIterator out)
centroid minimize within cluster sum of squares