| 26 | |
| 27 | |
| 28 | int main() |
| 29 | { |
| 30 | // The rvm functions use column vectors to contain a lot of the data on which they |
| 31 | // operate. So the first thing we do here is declare a convenient typedef. |
| 32 | |
| 33 | // This typedef declares a matrix with 2 rows and 1 column. It will be the |
| 34 | // object that contains each of our 2 dimensional samples. (Note that if you wanted |
| 35 | // more than 2 features in this vector you can simply change the 2 to something else. |
| 36 | // Or if you don't know how many features you want until runtime then you can put a 0 |
| 37 | // here and use the matrix.set_size() member function) |
| 38 | typedef matrix<double, 2, 1> sample_type; |
| 39 | |
| 40 | // This is a typedef for the type of kernel we are going to use in this example. |
| 41 | // In this case I have selected the radial basis kernel that can operate on our |
| 42 | // 2D sample_type objects |
| 43 | typedef radial_basis_kernel<sample_type> kernel_type; |
| 44 | |
| 45 | |
| 46 | // Now we make objects to contain our samples and their respective labels. |
| 47 | std::vector<sample_type> samples; |
| 48 | std::vector<double> labels; |
| 49 | |
| 50 | // Now let's put some data into our samples and labels objects. We do this |
| 51 | // by looping over a bunch of points and labeling them according to their |
| 52 | // distance from the origin. |
| 53 | for (int r = -20; r <= 20; ++r) |
| 54 | { |
| 55 | for (int c = -20; c <= 20; ++c) |
| 56 | { |
| 57 | sample_type samp; |
| 58 | samp(0) = r; |
| 59 | samp(1) = c; |
| 60 | samples.push_back(samp); |
| 61 | |
| 62 | // if this point is less than 10 from the origin |
| 63 | if (sqrt((double)r*r + c*c) <= 10) |
| 64 | labels.push_back(+1); |
| 65 | else |
| 66 | labels.push_back(-1); |
| 67 | |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // Here we normalize all the samples by subtracting their mean and dividing by their standard deviation. |
| 73 | // This is generally a good idea since it often heads off numerical stability problems and also |
| 74 | // prevents one large feature from smothering others. Doing this doesn't matter much in this example |
| 75 | // so I'm just doing this here so you can see an easy way to accomplish this with |
| 76 | // the library. |
| 77 | vector_normalizer<sample_type> normalizer; |
| 78 | // let the normalizer learn the mean and standard deviation of the samples |
| 79 | normalizer.train(samples); |
| 80 | // now normalize each sample |
| 81 | for (unsigned long i = 0; i < samples.size(); ++i) |
| 82 | samples[i] = normalizer(samples[i]); |
| 83 | |
| 84 | |
| 85 |
nothing calls this directly
no test coverage detected