| 28 | |
| 29 | |
| 30 | int main() |
| 31 | { |
| 32 | |
| 33 | // This first typedef declares a matrix with 4 rows and 1 column. It will be the |
| 34 | // object that contains each of our 4 dimensional samples. |
| 35 | typedef matrix<double, 4, 1> sample_type; |
| 36 | |
| 37 | |
| 38 | |
| 39 | // Now let's make some vector objects that can hold our samples |
| 40 | std::vector<sample_type> samples; |
| 41 | std::vector<double> labels; |
| 42 | |
| 43 | dlib::rand rnd; |
| 44 | |
| 45 | for (int x = -30; x <= 30; ++x) |
| 46 | { |
| 47 | for (int y = -30; y <= 30; ++y) |
| 48 | { |
| 49 | sample_type samp; |
| 50 | |
| 51 | // the first two features are just the (x,y) position of our points and so |
| 52 | // we expect them to be good features since our two classes here are points |
| 53 | // close to the origin and points far away from the origin. |
| 54 | samp(0) = x; |
| 55 | samp(1) = y; |
| 56 | |
| 57 | // This is a worthless feature since it is just random noise. It should |
| 58 | // be indicated as worthless by the rank_features() function below. |
| 59 | samp(2) = rnd.get_random_double(); |
| 60 | |
| 61 | // This is a version of the y feature that is corrupted by random noise. It |
| 62 | // should be ranked as less useful than features 0, and 1, but more useful |
| 63 | // than the above feature. |
| 64 | samp(3) = y*0.2 + (rnd.get_random_double()-0.5)*10; |
| 65 | |
| 66 | // add this sample into our vector of samples. |
| 67 | samples.push_back(samp); |
| 68 | |
| 69 | // if this point is less than 15 from the origin then label it as a +1 class point. |
| 70 | // otherwise it is a -1 class point |
| 71 | if (sqrt((double)x*x + y*y) <= 15) |
| 72 | labels.push_back(+1); |
| 73 | else |
| 74 | labels.push_back(-1); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | // Here we normalize all the samples by subtracting their mean and dividing by their standard deviation. |
| 80 | // This is generally a good idea since it often heads off numerical stability problems and also |
| 81 | // prevents one large feature from smothering others. |
| 82 | const sample_type m(mean(mat(samples))); // compute a mean vector |
| 83 | const sample_type sd(reciprocal(stddev(mat(samples)))); // compute a standard deviation vector |
| 84 | // now normalize each sample |
| 85 | for (unsigned long i = 0; i < samples.size(); ++i) |
| 86 | samples[i] = pointwise_multiply(samples[i] - m, sd); |
| 87 |
nothing calls this directly
no test coverage detected