| 33 | } |
| 34 | |
| 35 | int main() |
| 36 | { |
| 37 | // Here we declare that our samples will be 2 dimensional column vectors. |
| 38 | // (Note that if you don't know the dimensionality of your vectors at compile time |
| 39 | // you can change the 2 to a 0 and then set the size at runtime) |
| 40 | typedef matrix<double,2,1> sample_type; |
| 41 | |
| 42 | // Now we are making a typedef for the kind of kernel we want to use. I picked the |
| 43 | // radial basis kernel because it only has one parameter and generally gives good |
| 44 | // results without much fiddling. |
| 45 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 46 | |
| 47 | // Here we declare an instance of the kcentroid object. The kcentroid has 3 parameters |
| 48 | // you need to set. The first argument to the constructor is the kernel we wish to |
| 49 | // use. The second is a parameter that determines the numerical accuracy with which |
| 50 | // the object will perform the centroid estimation. Generally, smaller values |
| 51 | // give better results but cause the algorithm to attempt to use more dictionary vectors |
| 52 | // (and thus run slower and use more memory). The third argument, however, is the |
| 53 | // maximum number of dictionary vectors a kcentroid is allowed to use. So you can use |
| 54 | // it to control the runtime complexity. |
| 55 | kcentroid<kernel_type> test(kernel_type(0.1),0.01, 15); |
| 56 | |
| 57 | |
| 58 | // now we train our object on a few samples of the sinc function. |
| 59 | sample_type m; |
| 60 | for (double x = -15; x <= 8; x += 1) |
| 61 | { |
| 62 | m(0) = x; |
| 63 | m(1) = sinc(x); |
| 64 | test.train(m); |
| 65 | } |
| 66 | |
| 67 | running_stats<double> rs; |
| 68 | |
| 69 | // Now let's output the distance from the centroid to some points that are from the sinc function. |
| 70 | // These numbers should all be similar. We will also calculate the statistics of these numbers |
| 71 | // by accumulating them into the running_stats object called rs. This will let us easily |
| 72 | // find the mean and standard deviation of the distances for use below. |
| 73 | cout << "Points that are on the sinc function:\n"; |
| 74 | m(0) = -1.5; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 75 | m(0) = -1.5; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 76 | m(0) = -0; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 77 | m(0) = -0.5; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 78 | m(0) = -4.1; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 79 | m(0) = -1.5; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 80 | m(0) = -0.5; m(1) = sinc(m(0)); cout << " " << test(m) << endl; rs.add(test(m)); |
| 81 | |
| 82 | cout << endl; |
| 83 | // Let's output the distance from the centroid to some points that are NOT from the sinc function. |
| 84 | // These numbers should all be significantly bigger than previous set of numbers. We will also |
| 85 | // use the rs.scale() function to find out how many standard deviations they are away from the |
| 86 | // mean of the test points from the sinc function. So in this case our criterion for "significantly bigger" |
| 87 | // is > 3 or 4 standard deviations away from the above points that actually are on the sinc function. |
| 88 | cout << "Points that are NOT on the sinc function:\n"; |
| 89 | m(0) = -1.5; m(1) = sinc(m(0))+4; cout << " " << test(m) << " is " << rs.scale(test(m)) << " standard deviations from sinc." << endl; |
| 90 | m(0) = -1.5; m(1) = sinc(m(0))+3; cout << " " << test(m) << " is " << rs.scale(test(m)) << " standard deviations from sinc." << endl; |
| 91 | m(0) = -0; m(1) = -sinc(m(0)); cout << " " << test(m) << " is " << rs.scale(test(m)) << " standard deviations from sinc." << endl; |
| 92 | m(0) = -0.5; m(1) = -sinc(m(0)); cout << " " << test(m) << " is " << rs.scale(test(m)) << " standard deviations from sinc." << endl; |