| 127 | // ---------------------------------------------------------------------------------------- |
| 128 | |
| 129 | void test_clutering ( |
| 130 | ) |
| 131 | { |
| 132 | dlog << LINFO << " being test_clutering()"; |
| 133 | // Here we declare that our samples will be 2 dimensional column vectors. |
| 134 | typedef matrix<double,2,1> sample_type; |
| 135 | |
| 136 | // Now we are making a typedef for the kind of kernel we want to use. I picked the |
| 137 | // radial basis kernel because it only has one parameter and generally gives good |
| 138 | // results without much fiddling. |
| 139 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 140 | |
| 141 | // Here we declare an instance of the kcentroid object. The first argument to the constructor |
| 142 | // is the kernel we wish to use. The second is a parameter that determines the numerical |
| 143 | // accuracy with which the object will perform part of the learning algorithm. Generally |
| 144 | // smaller values give better results but cause the algorithm to run slower. You just have |
| 145 | // to play with it to decide what balance of speed and accuracy is right for your problem. |
| 146 | // Here we have set it to 0.01. |
| 147 | kcentroid<kernel_type> kc(kernel_type(0.1),0.01); |
| 148 | |
| 149 | // Now we make an instance of the kkmeans object and tell it to use kcentroid objects |
| 150 | // that are configured with the parameters from the kc object we defined above. |
| 151 | kkmeans<kernel_type> test(kc); |
| 152 | |
| 153 | std::vector<sample_type> samples; |
| 154 | std::vector<sample_type> initial_centers; |
| 155 | |
| 156 | sample_type m; |
| 157 | |
| 158 | dlib::rand rnd; |
| 159 | |
| 160 | print_spinner(); |
| 161 | // we will make 50 points from each class |
| 162 | const long num = 50; |
| 163 | |
| 164 | // make some samples near the origin |
| 165 | double radius = 0.5; |
| 166 | for (long i = 0; i < num; ++i) |
| 167 | { |
| 168 | double sign = 1; |
| 169 | if (rnd.get_random_double() < 0.5) |
| 170 | sign = -1; |
| 171 | m(0) = 2*radius*rnd.get_random_double()-radius; |
| 172 | m(1) = sign*sqrt(radius*radius - m(0)*m(0)); |
| 173 | |
| 174 | // add this sample to our set of samples we will run k-means |
| 175 | samples.push_back(m); |
| 176 | } |
| 177 | |
| 178 | // make some samples in a circle around the origin but far away |
| 179 | radius = 10.0; |
| 180 | for (long i = 0; i < num; ++i) |
| 181 | { |
| 182 | double sign = 1; |
| 183 | if (rnd.get_random_double() < 0.5) |
| 184 | sign = -1; |
| 185 | m(0) = 2*radius*rnd.get_random_double()-radius; |
| 186 | m(1) = sign*sqrt(radius*radius - m(0)*m(0)); |
no test coverage detected