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

Function main

examples/kkmeans_ex.cpp:32–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30using namespace dlib;
31
32int main()
33{
34 // Here we declare that our samples will be 2 dimensional column vectors.
35 // (Note that if you don't know the dimensionality of your vectors at compile time
36 // you can change the 2 to a 0 and then set the size at runtime)
37 typedef matrix<double,2,1> sample_type;
38
39 // Now we are making a typedef for the kind of kernel we want to use. I picked the
40 // radial basis kernel because it only has one parameter and generally gives good
41 // results without much fiddling.
42 typedef radial_basis_kernel<sample_type> kernel_type;
43
44
45 // Here we declare an instance of the kcentroid object. It is the object used to
46 // represent each of the centers used for clustering. The kcentroid has 3 parameters
47 // you need to set. The first argument to the constructor is the kernel we wish to
48 // use. The second is a parameter that determines the numerical accuracy with which
49 // the object will perform part of the learning algorithm. Generally, smaller values
50 // give better results but cause the algorithm to attempt to use more dictionary vectors
51 // (and thus run slower and use more memory). The third argument, however, is the
52 // maximum number of dictionary vectors a kcentroid is allowed to use. So you can use
53 // it to control the runtime complexity.
54 kcentroid<kernel_type> kc(kernel_type(0.1),0.01, 8);
55
56 // Now we make an instance of the kkmeans object and tell it to use kcentroid objects
57 // that are configured with the parameters from the kc object we defined above.
58 kkmeans<kernel_type> test(kc);
59
60 std::vector<sample_type> samples;
61 std::vector<sample_type> initial_centers;
62
63 sample_type m;
64
65 dlib::rand rnd;
66
67 // we will make 50 points from each class
68 const long num = 50;
69
70 // make some samples near the origin
71 double radius = 0.5;
72 for (long i = 0; i < num; ++i)
73 {
74 double sign = 1;
75 if (rnd.get_random_double() < 0.5)
76 sign = -1;
77 m(0) = 2*radius*rnd.get_random_double()-radius;
78 m(1) = sign*sqrt(radius*radius - m(0)*m(0));
79
80 // add this sample to our set of samples we will run k-means
81 samples.push_back(m);
82 }
83
84 // make some samples in a circle around the origin but far away
85 radius = 10.0;
86 for (long i = 0; i < num; ++i)
87 {
88 double sign = 1;
89 if (rnd.get_random_double() < 0.5)

Callers

nothing calls this directly

Calls 12

pick_initial_centersFunction · 0.85
spectral_clusterFunction · 0.85
get_random_doubleMethod · 0.80
set_number_of_centersMethod · 0.80
testClass · 0.70
sqrtFunction · 0.50
matFunction · 0.50
push_backMethod · 0.45
get_kernelMethod · 0.45
trainMethod · 0.45
sizeMethod · 0.45
dictionary_sizeMethod · 0.45

Tested by

no test coverage detected