MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/svm_ex.cpp:28–254  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27
28int main()
29{
30 // The svm 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 object that
34 // contains each of our 2 dimensional samples. (Note that if you wanted more than 2
35 // features in this vector you can simply change the 2 to something else. Or if you
36 // don't know how many features you want until runtime then you can put a 0 here and
37 // 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. In
41 // this case I have selected the radial basis kernel that can operate on our 2D
42 // 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 by looping
51 // over a bunch of points and labeling them according to their distance from the
52 // 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
73 // standard deviation. This is generally a good idea since it often heads off
74 // numerical stability problems and also prevents one large feature from smothering
75 // others. Doing this doesn't matter much in this example so I'm just doing this here
76 // so you can see an easy way to accomplish this with 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 // Now that we have some data we want to train on it. However, there are two

Callers

nothing calls this directly

Calls 13

randomize_samplesFunction · 0.85
maximum_nuFunction · 0.85
cross_validate_trainerFunction · 0.85
serializeFunction · 0.70
deserializeFunction · 0.70
sqrtFunction · 0.50
reduced2Function · 0.50
push_backMethod · 0.45
trainMethod · 0.45
sizeMethod · 0.45
set_kernelMethod · 0.45

Tested by

no test coverage detected