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