| 22 | |
| 23 | template <typename sample_type> |
| 24 | void run_test( |
| 25 | const std::vector<sample_type>& seed_centers |
| 26 | ) |
| 27 | { |
| 28 | print_spinner(); |
| 29 | |
| 30 | |
| 31 | sample_type samp; |
| 32 | |
| 33 | std::vector<sample_type> samples; |
| 34 | |
| 35 | |
| 36 | for (unsigned long j = 0; j < seed_centers.size(); ++j) |
| 37 | { |
| 38 | for (int i = 0; i < 250; ++i) |
| 39 | { |
| 40 | samp = randm(seed_centers[0].size(),1,rnd) - 0.5; |
| 41 | samples.push_back(samp + seed_centers[j]); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | randomize_samples(samples); |
| 46 | |
| 47 | { |
| 48 | std::vector<sample_type> centers; |
| 49 | pick_initial_centers(seed_centers.size(), centers, samples, linear_kernel<sample_type>()); |
| 50 | |
| 51 | find_clusters_using_kmeans(samples, centers); |
| 52 | |
| 53 | DLIB_TEST(centers.size() == seed_centers.size()); |
| 54 | |
| 55 | std::vector<int> hits(centers.size(),0); |
| 56 | for (unsigned long i = 0; i < samples.size(); ++i) |
| 57 | { |
| 58 | unsigned long best_idx = 0; |
| 59 | double best_dist = 1e100; |
| 60 | for (unsigned long j = 0; j < centers.size(); ++j) |
| 61 | { |
| 62 | if (length(samples[i] - centers[j]) < best_dist) |
| 63 | { |
| 64 | best_dist = length(samples[i] - centers[j]); |
| 65 | best_idx = j; |
| 66 | } |
| 67 | } |
| 68 | hits[best_idx]++; |
| 69 | } |
| 70 | |
| 71 | for (unsigned long i = 0; i < hits.size(); ++i) |
| 72 | { |
| 73 | DLIB_TEST(hits[i] == 250); |
| 74 | } |
| 75 | } |
| 76 | { |
| 77 | std::vector<sample_type> centers; |
| 78 | pick_initial_centers(seed_centers.size(), centers, samples, linear_kernel<sample_type>()); |
| 79 | |
| 80 | find_clusters_using_angular_kmeans(samples, centers); |
| 81 |
no test coverage detected