1 | // $Id: ensemble_test.cc 865 2007-09-10 19:41:04Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.thep.lu.se/trac/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "yat/utility/matrix.h" |
---|
26 | #include "yat/classifier/SubsetGenerator.h" |
---|
27 | #include "yat/classifier/CrossValidationSampler.h" |
---|
28 | #include "yat/classifier/EnsembleBuilder.h" |
---|
29 | #include "yat/classifier/Kernel.h" |
---|
30 | #include "yat/classifier/KernelLookup.h" |
---|
31 | #include "yat/classifier/Kernel_SEV.h" |
---|
32 | #include "yat/classifier/Kernel_MEV.h" |
---|
33 | #include "yat/classifier/MatrixLookup.h" |
---|
34 | #include "yat/classifier/NCC.h" |
---|
35 | #include "yat/statistics/PearsonDistance.h" |
---|
36 | #include "yat/classifier/PolynomialKernelFunction.h" |
---|
37 | #include "yat/classifier/SVM.h" |
---|
38 | #include "yat/statistics/AUC.h" |
---|
39 | |
---|
40 | #include <cassert> |
---|
41 | #include <fstream> |
---|
42 | #include <iostream> |
---|
43 | #include <cstdlib> |
---|
44 | #include <limits> |
---|
45 | |
---|
46 | |
---|
47 | int main(const int argc,const char* argv[]) |
---|
48 | { |
---|
49 | using namespace theplu::yat; |
---|
50 | |
---|
51 | std::ostream* error; |
---|
52 | if (argc>1 && argv[1]==std::string("-v")) |
---|
53 | error = &std::cerr; |
---|
54 | else { |
---|
55 | error = new std::ofstream("/dev/null"); |
---|
56 | if (argc>1) |
---|
57 | std::cout << "ensemble_test -v : for printing extra information\n"; |
---|
58 | } |
---|
59 | *error << "testing ensemble" << std::endl; |
---|
60 | bool ok = true; |
---|
61 | |
---|
62 | *error << "loading data" << std::endl; |
---|
63 | std::ifstream is("data/nm_data_centralized.txt"); |
---|
64 | utility::matrix data_core(is); |
---|
65 | is.close(); |
---|
66 | |
---|
67 | *error << "create MatrixLookup" << std::endl; |
---|
68 | classifier::MatrixLookup data(data_core); |
---|
69 | classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); |
---|
70 | *error << "Building kernel" << std::endl; |
---|
71 | classifier::Kernel_SEV kernel(data,*kf); |
---|
72 | |
---|
73 | |
---|
74 | *error << "load target" << std::endl; |
---|
75 | is.open("data/nm_target_bin.txt"); |
---|
76 | classifier::Target target(is); |
---|
77 | is.close(); |
---|
78 | assert(data.columns()==target.size()); |
---|
79 | |
---|
80 | *error << "create KernelLookup" << std::endl; |
---|
81 | classifier::KernelLookup kernel_lookup(kernel); |
---|
82 | *error << "create svm" << std::endl; |
---|
83 | classifier::SVM svm(kernel_lookup, target); |
---|
84 | *error << "create Subsets" << std::endl; |
---|
85 | classifier::CrossValidationSampler sampler(target,3,3); |
---|
86 | classifier::SubsetGenerator cv(sampler,kernel_lookup); |
---|
87 | *error << "create ensemble" << std::endl; |
---|
88 | classifier::EnsembleBuilder ensemble(svm,sampler); |
---|
89 | *error << "build ensemble" << std::endl; |
---|
90 | ensemble.build(); |
---|
91 | |
---|
92 | utility::vector out(target.size(),0); |
---|
93 | for (size_t i = 0; i<out.size(); ++i) |
---|
94 | out(i)=ensemble.validate()[0][i].mean(); |
---|
95 | statistics::AUC roc; |
---|
96 | *error << roc.score(target,out) << std::endl; |
---|
97 | |
---|
98 | delete kf; |
---|
99 | |
---|
100 | if (error!=&std::cerr) |
---|
101 | delete error; |
---|
102 | |
---|
103 | if(ok) |
---|
104 | return 0; |
---|
105 | return -1; |
---|
106 | |
---|
107 | } |
---|