source: branches/0.4-stable/test/ensemble_test.cc @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 13 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1// $Id: ensemble_test.cc 1392 2008-07-28 19:35:30Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
5  Copyright (C) 2008 Peter Johansson, Markus Ringnér
6
7  This file is part of the yat library, http://dev.thep.lu.se/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 "Suite.h"
26
27#include "yat/utility/Matrix.h"
28#include "yat/classifier/SubsetGenerator.h"
29#include "yat/classifier/CrossValidationSampler.h"
30#include "yat/classifier/EnsembleBuilder.h"
31#include "yat/classifier/Kernel.h"
32#include "yat/classifier/KernelLookup.h"
33#include "yat/classifier/Kernel_SEV.h"
34#include "yat/classifier/Kernel_MEV.h"
35#include "yat/classifier/MatrixLookup.h"
36#include "yat/classifier/MatrixLookupWeighted.h"
37#include "yat/classifier/NCC.h"
38#include "yat/classifier/PolynomialKernelFunction.h"
39#include "yat/classifier/SVM.h"
40#include "yat/statistics/AUC.h"
41#include "yat/statistics/EuclideanDistance.h"
42
43#include <cassert>
44#include <fstream>
45#include <iostream>
46#include <cstdlib>
47#include <limits>
48
49
50int main(int argc, char* argv[])
51{ 
52  using namespace theplu::yat;
53  test::Suite suite(argc, argv);
54 
55  suite.err() << "testing ensemble" << std::endl;
56
57  suite.err() << "loading data" << std::endl;
58  std::ifstream is(test::filename("data/nm_data_centralized.txt").c_str());
59  utility::Matrix data_core(is);
60  is.close();
61
62  suite.err() << "create MatrixLookup" << std::endl;
63  classifier::MatrixLookup data(data_core);
64  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 
65  suite.err() << "Building kernel" << std::endl;
66  classifier::Kernel_SEV kernel(data,*kf);
67
68
69  suite.err() << "load target" << std::endl;
70  is.open(test::filename("data/nm_target_bin.txt").c_str());
71  classifier::Target target(is);
72  is.close();
73  assert(data.columns()==target.size());
74
75  {
76    suite.err() << "create ensemble of ncc" << std::endl;
77    classifier::NCC<statistics::EuclideanDistance> ncc;
78    classifier::CrossValidationSampler sampler(target,3,3);
79    classifier::SubsetGenerator<classifier::MatrixLookup> subdata(sampler,data);
80    classifier::EnsembleBuilder<classifier::SupervisedClassifier,
81      classifier::MatrixLookup> ensemble(ncc, data, sampler);
82    suite.err() << "build ensemble" << std::endl;
83    ensemble.build();
84    std::vector<std::vector<statistics::Averager> > result;
85    ensemble.predict(data, result);
86  }
87
88  {
89    suite.err() << "create ensemble of ncc" << std::endl;
90    classifier::MatrixLookupWeighted data_weighted(data);
91    classifier::NCC<statistics::EuclideanDistance> ncc;
92    classifier::CrossValidationSampler sampler(target,3,3);
93    classifier::SubsetGenerator<classifier::MatrixLookupWeighted> 
94      subdata(sampler,data_weighted);
95    classifier::EnsembleBuilder<classifier::SupervisedClassifier,
96      classifier::MatrixLookupWeighted> ensemble(ncc, data_weighted, sampler);
97    suite.err() << "build ensemble" << std::endl;
98    ensemble.build();
99    std::vector<std::vector<statistics::Averager> > result;
100    ensemble.predict(data_weighted, result);
101  }
102
103  suite.err() << "create KernelLookup" << std::endl;
104  classifier::KernelLookup kernel_lookup(kernel);
105  suite.err() << "create svm" << std::endl;
106  classifier::SVM svm;
107  suite.err() << "create Subsets" << std::endl;
108  classifier::CrossValidationSampler sampler(target,3,3);
109  classifier::SubsetGenerator<classifier::KernelLookup> cv(sampler,
110                                                           kernel_lookup);
111
112  suite.err() << "create ensemble" << std::endl;
113  classifier::EnsembleBuilder<classifier::SVM, classifier::KernelLookup> 
114    ensemble(svm, kernel_lookup, sampler);
115  suite.err() << "build ensemble" << std::endl;
116  ensemble.build();
117  std::vector<std::vector<statistics::Averager> > result;
118  ensemble.predict(kernel_lookup, result);
119 
120  utility::Vector out(target.size(),0);
121  for (size_t i = 0; i<out.size(); ++i)
122    out(i)=ensemble.validate()[0][i].mean(); 
123  statistics::AUC roc;
124  suite.err() << roc.score(target,out) << std::endl;
125
126  {
127    suite.err() << "create ensemble" << std::endl;
128    classifier::EnsembleBuilder<classifier::SVM, classifier::KernelLookup> 
129      ensemble(svm, kernel_lookup, sampler);
130    suite.err() << "test validate() before build()\n";
131    ensemble.validate();
132    std::vector<std::vector<statistics::Averager> > result;
133    suite.err() << "test predict() before build()\n";
134    ensemble.predict(kernel_lookup, result);
135  }
136  delete kf;
137
138  return suite.return_value();
139}
Note: See TracBrowser for help on using the repository browser.