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