yat/classifier/ConsensusInputRanker.cc

Code
Comments
Other
Rev Date Author Line
115 19 Jul 04 peter 1 // $Id$
115 19 Jul 04 peter 2
675 10 Oct 06 jari 3 /*
2119 12 Dec 09 peter 4   Copyright (C) 2004 Jari Häkkinen, Peter Johansson
831 27 Mar 07 peter 5   Copyright (C) 2005 Peter Johansson
2119 12 Dec 09 peter 6   Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
4359 23 Aug 23 peter 7   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 8   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 9   Copyright (C) 2010, 2012 Peter Johansson
115 19 Jul 04 peter 10
1437 25 Aug 08 peter 11   This file is part of the yat library, http://dev.thep.lu.se/yat
295 29 Apr 05 peter 12
675 10 Oct 06 jari 13   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 14   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 15   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 16   License, or (at your option) any later version.
675 10 Oct 06 jari 17
675 10 Oct 06 jari 18   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 19   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 21   General Public License for more details.
675 10 Oct 06 jari 22
675 10 Oct 06 jari 23   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 24   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 25 */
675 10 Oct 06 jari 26
2881 18 Nov 12 peter 27 #include <config.h>
2881 18 Nov 12 peter 28
680 11 Oct 06 jari 29 #include "ConsensusInputRanker.h"
680 11 Oct 06 jari 30 #include "InputRanker.h"
680 11 Oct 06 jari 31 #include "IRRetrieve.h"
680 11 Oct 06 jari 32 #include "MatrixLookup.h"
680 11 Oct 06 jari 33 #include "MatrixLookupWeighted.h"
680 11 Oct 06 jari 34 #include "Sampler.h"
680 11 Oct 06 jari 35 #include "Target.h"
675 10 Oct 06 jari 36 #include "yat/statistics/Score.h"
675 10 Oct 06 jari 37 #include "yat/statistics/utility.h"
828 19 Mar 07 peter 38 #include "yat/statistics/VectorFunction.h"
675 10 Oct 06 jari 39 #include "yat/utility/stl_utility.h"
675 10 Oct 06 jari 40
482 02 Jan 06 peter 41 #include <cassert>
667 06 Oct 06 peter 42 #include <functional>
482 02 Jan 06 peter 43 #include <iostream>
295 29 Apr 05 peter 44 #include <utility>
133 16 Aug 04 peter 45 #include <vector>
560 13 Mar 06 markus 46 #include <cmath>
133 16 Aug 04 peter 47
115 19 Jul 04 peter 48 namespace theplu {
680 11 Oct 06 jari 49 namespace yat {
4200 19 Aug 22 peter 50 namespace classifier {
115 19 Jul 04 peter 51
828 19 Mar 07 peter 52   ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever,
828 19 Mar 07 peter 53                                              const statistics::VectorFunction&
828 19 Mar 07 peter 54                                              vf)
828 19 Mar 07 peter 55     : retriever_(retriever), vec_func_(vf)
666 06 Oct 06 peter 56   {
666 06 Oct 06 peter 57   }
666 06 Oct 06 peter 58
666 06 Oct 06 peter 59
4200 19 Aug 22 peter 60   void ConsensusInputRanker::add(const Sampler& sampler,
4200 19 Aug 22 peter 61                                  const MatrixLookup& data,
938 05 Oct 07 peter 62                                  const statistics::Score& score)
115 19 Jul 04 peter 63   {
482 02 Jan 06 peter 64     assert(sampler.size());
828 19 Mar 07 peter 65     assert(id_.empty() || id_.size()==data.rows());
828 19 Mar 07 peter 66     input_rankers_.reserve(sampler.size()+input_rankers_.size());
615 31 Aug 06 peter 67     id_.resize(data.rows());
615 31 Aug 06 peter 68     rank_.resize(data.rows());
615 31 Aug 06 peter 69     for (size_t i=0; i<sampler.size(); ++i){
4200 19 Aug 22 peter 70       input_rankers_.push_back(InputRanker(MatrixLookup(data,sampler.training_index(i), false),
4200 19 Aug 22 peter 71                                            sampler.training_target(i),
4200 19 Aug 22 peter 72                                            score));
115 19 Jul 04 peter 73     }
522 23 Feb 06 peter 74     update();
522 23 Feb 06 peter 75   }
482 02 Jan 06 peter 76
4200 19 Aug 22 peter 77   void ConsensusInputRanker::add(const Sampler& sampler,
4200 19 Aug 22 peter 78                                  const MatrixLookupWeighted& data,
938 05 Oct 07 peter 79                                  const statistics::Score& score)
615 31 Aug 06 peter 80   {
828 19 Mar 07 peter 81     assert(id_.empty() || id_.size()==data.rows());
615 31 Aug 06 peter 82     id_.resize(data.rows());
615 31 Aug 06 peter 83     rank_.resize(data.rows());
2223 19 Mar 10 peter 84     for (size_t i=0; i<sampler.size(); ++i) {
4200 19 Aug 22 peter 85       MatrixLookupWeighted mlw(data, utility::Index(data.rows()),
2223 19 Mar 10 peter 86                                sampler.training_index(i));
4200 19 Aug 22 peter 87       input_rankers_.push_back(InputRanker(mlw,
4200 19 Aug 22 peter 88                                            sampler.training_target(i),
4200 19 Aug 22 peter 89                                            score));
615 31 Aug 06 peter 90     }
615 31 Aug 06 peter 91     update();
615 31 Aug 06 peter 92   }
615 31 Aug 06 peter 93
720 26 Dec 06 jari 94   void ConsensusInputRanker::add(const InputRanker& ir)
720 26 Dec 06 jari 95   {
828 19 Mar 07 peter 96     assert(id_.empty() || id_.size()==ir.id().size());
720 26 Dec 06 jari 97     input_rankers_.push_back(ir);
720 26 Dec 06 jari 98   }
720 26 Dec 06 jari 99
720 26 Dec 06 jari 100   size_t ConsensusInputRanker::id(size_t i) const
720 26 Dec 06 jari 101   {
828 19 Mar 07 peter 102     assert(i<id_.size());
720 26 Dec 06 jari 103     return id_[i];
720 26 Dec 06 jari 104   }
720 26 Dec 06 jari 105
817 17 Mar 07 peter 106   const InputRanker& ConsensusInputRanker::input_ranker(size_t i) const
817 17 Mar 07 peter 107   {
817 17 Mar 07 peter 108     assert(i<input_rankers_.size());
817 17 Mar 07 peter 109     return input_rankers_[i];
817 17 Mar 07 peter 110   }
817 17 Mar 07 peter 111
720 26 Dec 06 jari 112   size_t ConsensusInputRanker::rank(size_t i) const
720 26 Dec 06 jari 113   {
720 26 Dec 06 jari 114     return rank_[i];
720 26 Dec 06 jari 115   }
720 26 Dec 06 jari 116
828 19 Mar 07 peter 117
828 19 Mar 07 peter 118   void ConsensusInputRanker::reserve(size_t n)
828 19 Mar 07 peter 119   {
828 19 Mar 07 peter 120     input_rankers_.reserve(n);
828 19 Mar 07 peter 121   }
828 19 Mar 07 peter 122
828 19 Mar 07 peter 123
522 23 Feb 06 peter 124   void ConsensusInputRanker::update(void)
522 23 Feb 06 peter 125   {
828 19 Mar 07 peter 126     // Sorting with respect to VectorFunction(info) where info is a
828 19 Mar 07 peter 127     // vector and each element contains infomation retrieved with
828 19 Mar 07 peter 128     // retriever_ from each InputRanker
828 19 Mar 07 peter 129     std::vector<std::pair<double,size_t> > cons_rank;
828 19 Mar 07 peter 130     cons_rank.reserve(id_.size());
4200 19 Aug 22 peter 131     for (size_t i=0; i<id_.size(); i++){
667 06 Oct 06 peter 132       std::vector<double> scores;
667 06 Oct 06 peter 133       scores.reserve(input_rankers_.size());
522 23 Feb 06 peter 134       for (size_t j=0; j<input_rankers_.size(); j++) {
667 06 Oct 06 peter 135         scores.push_back(retriever_(input_rankers_[j],i));
475 22 Dec 05 peter 136       }
828 19 Mar 07 peter 137       cons_rank.push_back(std::make_pair(vec_func_(scores), i));
133 16 Aug 04 peter 138     }
4200 19 Aug 22 peter 139
828 19 Mar 07 peter 140     //sort cons_rank and assign id_ and rank_
828 19 Mar 07 peter 141     sort(cons_rank.begin(), cons_rank.end(),
4200 19 Aug 22 peter 142          std::greater<std::pair<double, size_t> >());
4200 19 Aug 22 peter 143
828 19 Mar 07 peter 144     for (size_t i=0; i<cons_rank.size(); i++){
828 19 Mar 07 peter 145       assert(i<id_.size());
828 19 Mar 07 peter 146       id_[i]=cons_rank[i].second;
828 19 Mar 07 peter 147       assert(id_[i]<rank_.size());
475 22 Dec 05 peter 148       rank_[id_[i]]=i;
133 16 Aug 04 peter 149     }
115 19 Jul 04 peter 150   }
152 10 Sep 04 peter 151
152 10 Sep 04 peter 152
680 11 Oct 06 jari 153 }}} // of namespace classifier, yat, and theplu