source: trunk/yat/classifier/ConsensusInputRanker.cc @ 817

Last change on this file since 817 was 817, checked in by Peter, 15 years ago

Fixes #143

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.6 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 "ConsensusInputRanker.h"
25#include "InputRanker.h"
26#include "IRRetrieve.h"
27#include "MatrixLookup.h"
28#include "MatrixLookupWeighted.h"
29#include "Sampler.h"
30#include "Target.h"
31#include "yat/statistics/Score.h"
32#include "yat/statistics/utility.h"
33#include "yat/utility/stl_utility.h"
34
35#include <cassert>
36#include <functional>
37#include <iostream>
38#include <utility>
39#include <vector>
40#include <cmath>
41
42namespace theplu {
43namespace yat {
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::add(const InputRanker& ir)
88  {
89    input_rankers_.push_back(ir);
90  }
91
92  size_t ConsensusInputRanker::id(size_t i) const
93  {
94    return id_[i];
95  }
96
97  const InputRanker& ConsensusInputRanker::input_ranker(size_t i) const
98  {
99    assert(i<input_rankers_.size());
100    return input_rankers_[i];
101  }
102
103  size_t ConsensusInputRanker::rank(size_t i) const
104  {
105    return rank_[i];
106  }
107
108  void ConsensusInputRanker::update(void)
109  {
110
111    // Sorting with respect to median info (from retriever_)
112    std::vector<std::pair<double,size_t> > medians(id_.size());
113    for (size_t i=0; i<id_.size(); i++){ 
114      std::vector<double> scores;
115      scores.reserve(input_rankers_.size());
116      for (size_t j=0; j<input_rankers_.size(); j++) {
117        scores.push_back(retriever_(input_rankers_[j],i));
118      }
119      medians[i].first = statistics::median(scores);
120      medians[i].second = i;
121    }
122   
123    //sort medians and assign id_ and rank_
124    sort(medians.begin(), medians.end(),
125         std::greater<std::pair<double, size_t> >()); 
126         
127    for (size_t i=0; i<medians.size(); i++){
128      id_[i]=medians[i].second;
129      rank_[id_[i]]=i;
130    }
131  }
132
133
134}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.