| 30 | // ---------------------------------------------------------------------------------------- |
| 31 | |
| 32 | void test_clutering ( |
| 33 | ) |
| 34 | { |
| 35 | dlog << LINFO << " being test_clutering()"; |
| 36 | // Here we declare that our samples will be 2 dimensional column vectors. |
| 37 | typedef matrix<double,2,1> sample_type; |
| 38 | |
| 39 | // Now we are making a typedef for the kind of kernel we want to use. I picked the |
| 40 | // radial basis kernel because it only has one parameter and generally gives good |
| 41 | // results without much fiddling. |
| 42 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 43 | |
| 44 | // Here we declare an instance of the kcentroid object. The first argument to the constructor |
| 45 | // is the kernel we wish to use. The second is a parameter that determines the numerical |
| 46 | // accuracy with which the object will perform part of the learning algorithm. Generally |
| 47 | // smaller values give better results but cause the algorithm to run slower. You just have |
| 48 | // to play with it to decide what balance of speed and accuracy is right for your problem. |
| 49 | // Here we have set it to 0.01. |
| 50 | kcentroid<kernel_type> kc(kernel_type(0.1),0.01); |
| 51 | |
| 52 | // Now we make an instance of the kkmeans object and tell it to use kcentroid objects |
| 53 | // that are configured with the parameters from the kc object we defined above. |
| 54 | kkmeans<kernel_type> test(kc); |
| 55 | |
| 56 | std::vector<sample_type> samples; |
| 57 | std::vector<sample_type> initial_centers; |
| 58 | |
| 59 | sample_type m; |
| 60 | |
| 61 | dlib::rand rnd; |
| 62 | |
| 63 | print_spinner(); |
| 64 | // we will make 50 points from each class |
| 65 | const long num = 50; |
| 66 | |
| 67 | // make some samples near the origin |
| 68 | double radius = 0.5; |
| 69 | for (long i = 0; i < num; ++i) |
| 70 | { |
| 71 | double sign = 1; |
| 72 | if (rnd.get_random_double() < 0.5) |
| 73 | sign = -1; |
| 74 | m(0) = 2*radius*rnd.get_random_double()-radius; |
| 75 | m(1) = sign*sqrt(radius*radius - m(0)*m(0)); |
| 76 | |
| 77 | // add this sample to our set of samples we will run k-means |
| 78 | samples.push_back(m); |
| 79 | } |
| 80 | |
| 81 | // make some samples in a circle around the origin but far away |
| 82 | radius = 10.0; |
| 83 | for (long i = 0; i < num; ++i) |
| 84 | { |
| 85 | double sign = 1; |
| 86 | if (rnd.get_random_double() < 0.5) |
| 87 | sign = -1; |
| 88 | m(0) = 2*radius*rnd.get_random_double()-radius; |
| 89 | m(1) = sign*sqrt(radius*radius - m(0)*m(0)); |
no test coverage detected