source: trunk/c++_tools/classifier/ConsensusInputRanker.cc @ 675

Last change on this file since 675 was 675, checked in by Jari Häkkinen, 15 years ago

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.4 KB
Line 
1// $Id$
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/ConsensusInputRanker.h"
25
26#include "yat/classifier/InputRanker.h"
27#include "yat/classifier/IRRetrieve.h"
28#include "yat/classifier/MatrixLookup.h"
29#include "yat/classifier/MatrixLookupWeighted.h"
30#include "yat/classifier/Sampler.h"
31#include "yat/classifier/Target.h"
32#include "yat/statistics/Score.h"
33#include "yat/statistics/utility.h"
34#include "yat/utility/stl_utility.h"
35
36#include <cassert>
37#include <functional>
38#include <iostream>
39#include <utility>
40#include <vector>
41#include <cmath>
42
43namespace theplu {
44namespace classifier { 
45
46  ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever)
47    : retriever_(retriever)
48  {
49  }
50
51
52  ConsensusInputRanker::ConsensusInputRanker(const Sampler& sampler, 
53                                             const MatrixLookup& data, 
54                                             statistics::Score& score,
55                                             const IRRetrieve& retriever)
56    : retriever_(retriever)
57  {
58    assert(sampler.size());
59    id_.resize(data.rows());
60    rank_.resize(data.rows());
61    for (size_t i=0; i<sampler.size(); ++i){
62      input_rankers_.push_back(InputRanker(MatrixLookup(data,sampler.training_index(i), false), 
63                                           sampler.training_target(i), 
64                                           score));       
65    }
66    update();
67  }
68
69  ConsensusInputRanker::ConsensusInputRanker(const Sampler& sampler, 
70                                             const MatrixLookupWeighted& data, 
71                                             statistics::Score& score,
72                                             const IRRetrieve& retriever)
73    : retriever_(retriever)
74  {
75    assert(sampler.size());
76    id_.resize(data.rows());
77    rank_.resize(data.rows());
78 
79    for (size_t i=0; i<sampler.size(); ++i){
80      input_rankers_.push_back(InputRanker(MatrixLookupWeighted(data,sampler.training_index(i), false), 
81                                           sampler.training_target(i), 
82                                           score));       
83    }
84    update();
85  }
86
87  void ConsensusInputRanker::update(void)
88  {
89
90    // Sorting with respect to median info (from retriever_)
91    std::vector<std::pair<double,size_t> > medians(id_.size());
92    for (size_t i=0; i<id_.size(); i++){ 
93      std::vector<double> scores;
94      scores.reserve(input_rankers_.size());
95      for (size_t j=0; j<input_rankers_.size(); j++) {
96        scores.push_back(retriever_(input_rankers_[j],i));
97      }
98      medians[i].first = statistics::median(scores);
99      medians[i].second = i;
100    }
101   
102    //sort medians and assign id_ and rank_
103    sort(medians.begin(), medians.end(),
104         std::greater<std::pair<double, size_t> >()); 
105         
106    for (size_t i=0; i<medians.size(); i++){
107      id_[i]=medians[i].second;
108      rank_[id_[i]]=i;
109    }
110  }
111
112
113}} // of namespace classifier and namespace theplu
Note: See TracBrowser for help on using the repository browser.