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

Function test_anomaly_detection

dlib/test/svm.cpp:370–484  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

368// ----------------------------------------------------------------------------------------
369
370 void test_anomaly_detection (
371 )
372 {
373 dlog << LINFO << " begin test_anomaly_detection()";
374 // Here we declare that our samples will be 2 dimensional column vectors.
375 typedef matrix<double,2,1> sample_type;
376
377 // Now we are making a typedef for the kind of kernel we want to use. I picked the
378 // radial basis kernel because it only has one parameter and generally gives good
379 // results without much fiddling.
380 typedef radial_basis_kernel<sample_type> kernel_type;
381
382 // Here we declare an instance of the kcentroid object. The first argument to the constructor
383 // is the kernel we wish to use. The second is a parameter that determines the numerical
384 // accuracy with which the object will perform part of the learning algorithm. Generally
385 // smaller values give better results but cause the algorithm to run slower. You just have
386 // to play with it to decide what balance of speed and accuracy is right for your problem.
387 // Here we have set it to 0.01.
388 kcentroid<kernel_type> test(kernel_type(0.1),0.01);
389
390
391 svm_one_class_trainer<kernel_type> one_class_trainer;
392 one_class_trainer.set_nu(0.4);
393 one_class_trainer.set_kernel(kernel_type(0.2));
394
395 std::vector<sample_type> samples;
396
397 // now we train our object on a few samples of the sinc function.
398 sample_type m;
399 for (double x = -15; x <= 8; x += 1)
400 {
401 m(0) = x;
402 m(1) = sinc(x);
403 test.train(m);
404 samples.push_back(m);
405 }
406
407 decision_function<kernel_type> df = one_class_trainer.train(samples);
408
409 running_stats<double> rs;
410
411 // Now lets output the distance from the centroid to some points that are from the sinc function.
412 // These numbers should all be similar. We will also calculate the statistics of these numbers
413 // by accumulating them into the running_stats object called rs. This will let us easily
414 // find the mean and standard deviation of the distances for use below.
415 dlog << LDEBUG << "Points that are on the sinc function:\n";
416 m(0) = -1.5; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
417 m(0) = -1.5; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
418 m(0) = -0; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
419 m(0) = -0.5; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
420 m(0) = -4.1; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
421 m(0) = -1.5; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
422 m(0) = -0.5; m(1) = sinc(m(0)); dlog << LDEBUG << " " << test(m); rs.add(test(m));
423
424 m(0) = -1.5; m(1) = sinc(m(0)); DLIB_TEST_MSG(rs.scale(test(m)) < 2, rs.scale(test(m)));
425 m(0) = -1.5; m(1) = sinc(m(0)); DLIB_TEST_MSG(rs.scale(test(m)) < 2, rs.scale(test(m)));
426 m(0) = -0; m(1) = sinc(m(0)); DLIB_TEST_MSG(rs.scale(test(m)) < 2, rs.scale(test(m)));
427 m(0) = -0.5; m(1) = sinc(m(0)); DLIB_TEST_MSG(rs.scale(test(m)) < 2, rs.scale(test(m)));

Callers 1

perform_testMethod · 0.85

Calls 8

scaleMethod · 0.80
sincFunction · 0.70
testFunction · 0.70
set_nuMethod · 0.45
set_kernelMethod · 0.45
trainMethod · 0.45
push_backMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected