source: trunk/test/feature_selection_test.cc @ 1658

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

refs #396 - fixing feature_selector_test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1// $Id: feature_selection_test.cc 1584 2008-10-15 19:42:36Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
6  Copyright (C) 2008 Peter Johansson
7
8  This file is part of the yat library, http://dev.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 3 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with yat. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "Suite.h"
25
26#include "yat/classifier/DataLookupWeighted1D.h"
27#include "yat/classifier/FeatureSelectorIR.h"
28#include "yat/classifier/FeatureSelectorRandom.h"
29#include "yat/classifier/MatrixLookupWeighted.h"
30#include "yat/classifier/Target.h"
31#include "yat/statistics/SNRScore.h"
32
33#include "yat/utility/Matrix.h"
34#include "yat/utility/MatrixWeighted.h"
35
36#include <algorithm>
37#include <cmath>
38#include <fstream>
39#include <iterator>
40#include <iostream>
41#include <string>
42
43using namespace theplu::yat;
44
45int main(int argc, char* argv[])
46{ 
47  test::Suite suite(argc, argv);
48  suite.err() << "testing feature_selection" << std::endl;
49
50  statistics::SNRScore snr;
51  classifier::FeatureSelectorIR f(snr, 12);
52  classifier::FeatureSelectorRandom f2(12);
53
54  suite.err() << "Reading in Sorlie data to identify top gene ..." << std::endl;
55  std::ifstream is(test::filename("data/sorlie_centroid_data.txt").c_str());
56  utility::Matrix data(is,'\t');
57  is.close();
58
59  is.open(test::filename("data/sorlie_centroid_classes.txt").c_str());
60  classifier::Target targets(is);
61  is.close();
62
63  suite.err() << "... done" << std::endl;
64
65  // Generate matrix with weight 0 for missing values and 1 for others.
66  utility::MatrixWeighted xw(data);
67  classifier::MatrixLookupWeighted dataviewweighted(xw);
68 
69  f2.update(dataviewweighted,targets);
70  suite.err() << "\nRandomly ordered features (top 12):\n";
71  std::vector<size_t> features=f2.features().vector();
72  std::copy(features.begin(),features.end(),
73            std::ostream_iterator<size_t>(suite.err()," ")); 
74  suite.err() << std::endl;
75
76  f.update(dataviewweighted,targets);
77  suite.err() << "\nSNR ordered ordered features (top 12):\n";
78  features=f.features().vector();
79  std::copy(features.begin(),features.end(),
80            std::ostream_iterator<size_t>(suite.err()," ")); 
81  suite.err() << std::endl;
82
83  size_t best_feature=features[0];
84  if(best_feature!=69) {
85    suite.err() << "\nERROR: Incorrect best feature found!\n" << std::endl;
86    suite.add(false);
87  }
88
89  classifier::DataLookupWeighted1D row(dataviewweighted,best_feature,true);
90  double score_diff=std::abs(snr.score(targets,row)-1.47804);
91  if(score_diff>0.00001) {
92    suite.err() << "\nERROR: Best score not what expected!\n" << std::endl;
93    suite.add(false);
94  }
95 
96  // Re-rank the best features
97  classifier::MatrixLookupWeighted ranked=f.get(dataviewweighted);
98  f.update(ranked,targets);
99  features=f.features().vector();
100  for(size_t i=0;i<features.size();i++) {
101    if(features[i]!=i) {
102      suite.add(false);
103      suite.err() << "ERROR: Problem with top-ranked feature" << std::endl;
104    }
105  }
106
107  return suite.return_value();
108}
Note: See TracBrowser for help on using the repository browser.