source: trunk/test/kernel_test.cc @ 453

Last change on this file since 453 was 453, checked in by Peter, 17 years ago

changed namespace svm to classifier in test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1// $Id: kernel_test.cc 453 2005-12-15 21:49:23Z peter $
2
3
4// C++ tools include
5////////////////////
6#include <c++_tools/gslapi/matrix.h>
7#include <c++_tools/gslapi/vector.h>
8#include <c++_tools/classifier/KernelFunction.h>
9#include <c++_tools/classifier/PolynomialKernelFunction.h>
10#include <c++_tools/classifier/GaussianKernelFunction.h>
11#include <c++_tools/classifier/KernelView.h>
12#include <c++_tools/classifier/Kernel_MEV.h>
13#include <c++_tools/classifier/Kernel_SEV.h>
14
15#include <cmath>
16#include <cstdlib>
17#include <fstream>
18#include <iostream>
19#include <vector>
20
21using namespace theplu;
22
23bool test_MEV(const gslapi::matrix& data, const classifier::KernelFunction* kf, 
24              const gslapi::matrix& control, const double error_bound)
25{
26  classifier::Kernel_MEV kernel(data,*kf);
27  for(u_int i=0;i<control.rows();i++)
28    for(u_int j=0;j<control.columns();j++)
29      if (fabs(kernel(i,j)-control(i,j))>error_bound)
30        return false;
31
32  // checking view
33  std::vector<size_t> index(3);
34  index[0]=1;
35  index[1]=2;
36  index[2]=3;
37  classifier::KernelView(kernel,index);
38
39  return true;
40}
41
42bool test_SEV(const gslapi::matrix& data, const classifier::KernelFunction* kf, 
43              const gslapi::matrix& control, const double error_bound)
44{
45  classifier::Kernel_SEV kernel(data,*kf);
46  for(u_int i=0;i<control.rows();i++)
47    for(u_int j=0;j<control.columns();j++)
48      if (fabs(kernel(i,j)-control(i,j))>error_bound)
49        return false;
50
51  // checking view
52  std::vector<size_t> index(3);
53  index[0]=1;
54  index[1]=2;
55  index[2]=3;
56  classifier::KernelView(kernel,index);
57  return true;
58}
59
60
61int main(const int argc,const char* argv[])
62
63{ 
64  std::ostream* error;
65  if (argc>1 && argv[1]==std::string("-v"))
66    error = &std::cerr;
67  else {
68    error = new std::ofstream("/dev/null");
69    if (argc>1)
70      std::cout << "kernel_test -v : for printing extra information\n";
71  }
72  *error << "testing kernels" << std::endl;
73  bool ok = true;
74
75  // Peter, the hardcoded number below should be changed.
76  double error_bound = 1e-8; 
77  std::ifstream is("data/nm_data_centralized.txt");
78  gslapi::matrix transposed_data(is);
79  is.close();
80  // Because how the kernel is treated is changed, data must be transposed.
81  gslapi::matrix data=transposed_data;
82
83  is.open("data/nm_kernel.txt");
84  gslapi::matrix kernel_matlab(is);
85  is.close();
86  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
87  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound)
88        & test_SEV(data,kf,kernel_matlab,error_bound));
89  delete kf;
90 
91  is.open("data/nm_kernel2.txt");
92  gslapi::matrix kernel_matlab2(is);
93  is.close();
94  kf = new classifier::PolynomialKernelFunction(2); 
95  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound)
96        & test_SEV(data,kf,kernel_matlab2,error_bound));
97  delete kf;
98
99  // Checking that a GaussianKernelFunction object can be built at
100  // compile time.
101  kf = new classifier::GaussianKernelFunction(); 
102  delete kf;
103
104  if (error!=&std::cerr)
105    delete error;
106
107  if (ok=true) 
108    return 0;
109  return -1;
110}
Note: See TracBrowser for help on using the repository browser.