All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
k_means_clustering_engine_example.cpp
Go to the documentation of this file.
1 
10 #include "paal/utils/functors.hpp"
11 
12 #include <vector>
13 #include <limits>
14 #include <cmath>
15 
16 int main() {
17  // in this example cetroid is always chosen from the original points
18  // in this version of algorithm, user is required to provide functors which are used
19  // to run k_medians. That is closest_to and centroid
20 
21  using Point = std::vector<double>;
22 
23  // sample data
24  const int NUMBER_OF_CLUSTER = 2;
25  std::vector<Point> points = { { 0, 0 },
26  { 0, 3 },
27  { 4, 0 } };
28  std::vector<Point> centers(NUMBER_OF_CLUSTER);
29  paal::get_random_centers(points, NUMBER_OF_CLUSTER, centers.begin());
30  std::vector<std::pair<Point, int>> point_cluster_pairs(points.size());
31 
32  //this centroid chooses centers from points
33  auto centroid = [&](const std::vector<std::vector<double>> &points) {
34  double best_dist = std::numeric_limits<double>::max();
35  std::vector<double> result;
36  for (auto && center : points) {
37  double dist = 1e99;
38  for (auto && point : points) {
39  dist = std::min(dist, paal::distance_square(point, center));
40  }
41 
42  if (dist < best_dist) {
43  best_dist = dist;
44  result = center;
45  }
46  }
47  assert(!result.empty());
48  return result;
49  };
50 
51  //default closest_to
52  auto closest_to = [&](const Point & point) {
53  return paal::closest_to(point, centers);
54  };
55 
56  // solution
57  paal::k_means(points,
58  centers, centroid, closest_to,
59  point_cluster_pairs.begin());
61 }
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:
This file contains set of simple useful functors or functor adapters.
auto get_random_centers(Points &&points, int number_of_centers, OutputIterator out, RNG &&rng=std::default_random_engine{})
auto distance_square(RangeLeft &&lrange, RangeRight &&rrange)
int main()
[K Means Clustering Engine Example]