Changeset 828
 Timestamp:
 Mar 19, 2007, 11:15:43 PM (16 years ago)
 Location:
 trunk
 Files:

 4 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/consensus_inputranker_test.cc
r820 r828 28 28 #include "yat/classifier/CrossValidationSampler.h" 29 29 #include "yat/classifier/IRRank.h" 30 #include "yat/statistics/VectorFunction.h" 30 31 31 32 #include <cstdlib> … … 63 64 *error << "Building Consensus_Inputranker" << std::endl; 64 65 theplu::yat::classifier::IRRank retrieve; 65 theplu::yat::classifier::ConsensusInputRanker cir(sampler,data,roc,retrieve); 66 *error << "Done" << std::endl; 66 theplu::yat::statistics::Median median; 67 theplu::yat::classifier::ConsensusInputRanker cir(retrieve,median); 68 cir.add(sampler,data,roc); 67 69 70 *error << "test ids... "; 68 71 if (cir.id(0)!=2  cir.id(1)!=0  cir.id(2)!=1){ 69 *error << "incorrect id" << endl; 70 ok = false; 71 } 72 73 if (cir.rank(0)!=1  cir.rank(1)!=2  cir.rank(2)!=0){ 74 *error << "incorrect rank" << endl; 72 *error << "\nincorrect id for weighted" << endl; 75 73 ok=false; 76 74 } 75 else 76 *error << "ok." << std::endl; 77 78 *error << "test ranks... "; 79 if (cir.rank(0)!=1  cir.rank(1)!=2  cir.rank(2)!=0){ 80 *error << "\nincorrect rank for weighted" << endl; 81 ok=false; 82 } 83 else 84 *error << "ok." << std::endl; 77 85 78 86 theplu::yat::utility::matrix flag(data.rows(),data.columns(),1); 79 87 // Peter, fix weighted version instead 80 theplu::yat::classifier::ConsensusInputRanker cir2(sampler,data,roc,retrieve); 88 theplu::yat::classifier::ConsensusInputRanker cir2(retrieve,median); 89 cir2.add(sampler,data,roc); 81 90 91 *error << "test ids... "; 82 92 if (cir2.id(0)!=2  cir2.id(1)!=0  cir2.id(2)!=1){ 83 *error << " incorrect id for weighted" << endl;93 *error << "\nincorrect id for weighted" << endl; 84 94 ok=false; 85 95 } 96 else 97 *error << "ok." << std::endl; 86 98 99 *error << "test ranks... "; 87 100 if (cir2.rank(0)!=1  cir2.rank(1)!=2  cir2.rank(2)!=0){ 88 *error << " incorrect rank for weighted" << endl;101 *error << "\nincorrect rank for weighted" << endl; 89 102 ok=false; 90 103 } 104 else 105 *error << "ok." << std::endl; 91 106 92 107 if (error!=&std::cerr) 
trunk/yat/classifier/ConsensusInputRanker.cc
r817 r828 31 31 #include "yat/statistics/Score.h" 32 32 #include "yat/statistics/utility.h" 33 #include "yat/statistics/VectorFunction.h" 33 34 #include "yat/utility/stl_utility.h" 34 35 … … 44 45 namespace classifier { 45 46 46 ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever) 47 : retriever_(retriever) 47 ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever, 48 const statistics::VectorFunction& 49 vf) 50 : retriever_(retriever), vec_func_(vf) 48 51 { 49 52 } 50 53 51 54 52 ConsensusInputRanker::ConsensusInputRanker(const Sampler& sampler, 53 const MatrixLookup& data, 54 statistics::Score& score, 55 const IRRetrieve& retriever) 56 : retriever_(retriever) 55 void ConsensusInputRanker::add(const Sampler& sampler, 56 const MatrixLookup& data, 57 statistics::Score& score) 57 58 { 58 59 assert(sampler.size()); 60 assert(id_.empty()  id_.size()==data.rows()); 61 input_rankers_.reserve(sampler.size()+input_rankers_.size()); 59 62 id_.resize(data.rows()); 60 63 rank_.resize(data.rows()); … … 67 70 } 68 71 69 ConsensusInputRanker::ConsensusInputRanker(const Sampler& sampler, 70 const MatrixLookupWeighted& data, 71 statistics::Score& score, 72 const IRRetrieve& retriever) 73 : retriever_(retriever) 72 void ConsensusInputRanker::add(const Sampler& sampler, 73 const MatrixLookupWeighted& data, 74 statistics::Score& score) 74 75 { 75 assert( sampler.size());76 assert(id_.empty()  id_.size()==data.rows()); 76 77 id_.resize(data.rows()); 77 78 rank_.resize(data.rows()); 78 79 79 for (size_t i=0; i<sampler.size(); ++i){ 80 80 input_rankers_.push_back(InputRanker(MatrixLookupWeighted(data,sampler.training_index(i), false), … … 87 87 void ConsensusInputRanker::add(const InputRanker& ir) 88 88 { 89 assert(id_.empty()  id_.size()==ir.id().size()); 89 90 input_rankers_.push_back(ir); 90 91 } … … 92 93 size_t ConsensusInputRanker::id(size_t i) const 93 94 { 95 assert(i<id_.size()); 94 96 return id_[i]; 95 97 } … … 106 108 } 107 109 110 111 void ConsensusInputRanker::reserve(size_t n) 112 { 113 input_rankers_.reserve(n); 114 } 115 116 108 117 void ConsensusInputRanker::update(void) 109 118 { 110 111 // Sorting with respect to median info (from retriever_) 112 std::vector<std::pair<double,size_t> > medians(id_.size()); 119 // Sorting with respect to VectorFunction(info) where info is a 120 // vector and each element contains infomation retrieved with 121 // retriever_ from each InputRanker 122 std::vector<std::pair<double,size_t> > cons_rank; 123 cons_rank.reserve(id_.size()); 113 124 for (size_t i=0; i<id_.size(); i++){ 114 125 std::vector<double> scores; … … 117 128 scores.push_back(retriever_(input_rankers_[j],i)); 118 129 } 119 medians[i].first = statistics::median(scores); 120 medians[i].second = i; 130 cons_rank.push_back(std::make_pair(vec_func_(scores), i)); 121 131 } 122 132 123 //sort mediansand assign id_ and rank_124 sort( medians.begin(), medians.end(),133 //sort cons_rank and assign id_ and rank_ 134 sort(cons_rank.begin(), cons_rank.end(), 125 135 std::greater<std::pair<double, size_t> >()); 126 136 127 for (size_t i=0; i<medians.size(); i++){ 128 id_[i]=medians[i].second; 137 for (size_t i=0; i<cons_rank.size(); i++){ 138 assert(i<id_.size()); 139 id_[i]=cons_rank[i].second; 140 assert(id_[i]<rank_.size()); 129 141 rank_[id_[i]]=i; 130 142 } 
trunk/yat/classifier/ConsensusInputRanker.h
r817 r828 27 27 #include "InputRanker.h" 28 28 29 #include <vector> 30 29 31 namespace theplu { 30 32 namespace yat { 31 32 class statistics::Score; 33 33 namespace statistics { 34 class Score; 35 class VectorFunction; 36 } 34 37 namespace classifier { 35 38 … … 46 49 /// could be different because they are based upon different 47 50 /// subsets of the data, or the different lists could be different 48 /// because they have aregenerated using different criteria. Having51 /// because they have been generated using different criteria. Having 49 52 /// \f$ N \f$ lists means each row in the data matrix has \f$ N \f$ 50 /// ranks (each corresponding to one list) and a consensus ranked 51 /// list is created by sorting the data rows with respect to their 52 /// median rank. 53 /// ranks (each corresponding to one list). A 54 /// statistics::VectorFunction is used to boil down these ranks to 55 /// one consensus rank, and a ranked list is created by sorting the 56 /// data rows with respect to this consensus rank. 53 57 /// 54 58 /// For the time being there are two ways to build a 55 59 /// ConsensusInputRanker. 1) Sending a Sampler and a MatrixLookup to 56 /// the constructorwill create one ranked list for each of the60 /// the add function will create one ranked list for each of the 57 61 /// partitions defined in the Sampler. 2) You can generate 58 62 /// your ranked list outside, using your favourite method, and … … 71 75 /// Truly does nothing but creates a few empty member vectors. 72 76 /// 73 ConsensusInputRanker(const IRRetrieve& );77 ConsensusInputRanker(const IRRetrieve&, const statistics::VectorFunction&); 74 78 75 79 /// … … 79 83 /// the median rank (i.e. update() is called). 80 84 /// 81 ConsensusInputRanker(const Sampler& sampler, const MatrixLookup&, 82 statistics::Score& s, const IRRetrieve&); 85 void add(const Sampler& sampler, const MatrixLookup&, statistics::Score& s); 83 86 84 87 /// 88 /// @brief Add a set of InputRankers 89 /// 85 90 /// Iterating through @a sampler creating subsets of @a data, and 86 91 /// for each subset is an InputRanker is created using the @a … … 88 93 /// the median rank (i.e. update() is called). 89 94 /// 90 ConsensusInputRanker(const Sampler& sampler, 91 const MatrixLookupWeighted& data, 92 statistics::Score& score, const IRRetrieve&); 95 void add(const Sampler& sampler, const MatrixLookupWeighted& data, 96 statistics::Score& score); 93 97 94 98 /// 95 /// @brief add an InputRanker99 /// @brief Add an InputRanker 96 100 /// 97 101 /// @note update() must be called to make the added InputRanker to … … 119 123 size_t rank(size_t i) const; 120 124 125 /** 126 \brief \brief reserve memory for internal vector of InputRankers 127 128 This function is recommended before adding using add(const 129 InputRanker&) to avoid reallocations. 130 */ 131 void reserve(size_t n); 132 133 121 134 /// 122 135 /// update ids and ranks … … 131 144 std::vector<size_t> rank_; 132 145 const IRRetrieve& retriever_; 133 146 const statistics::VectorFunction& vec_func_; 134 147 }; 135 148 
trunk/yat/statistics/VectorFunction.h
r827 r828 41 41 42 42 43 /// 44 /// @brief Larget element 45 /// 43 46 struct Max : public VectorFunction 44 47 { … … 50 53 51 54 55 /// 56 /// @brief Median element 57 /// 52 58 struct Median : public VectorFunction 53 59 { 60 /// 61 /// \see statistics::median(std::vector<double>, bool) 54 62 /// 55 63 /// \return Median … … 58 66 }; 59 67 60 68 /// 69 /// \brief Mean element 70 /// 61 71 struct Mean : public VectorFunction 62 72 { … … 68 78 69 79 80 /// 81 /// \brief Smallest element 82 /// 70 83 struct Min : public VectorFunction 71 84 {
Note: See TracChangeset
for help on using the changeset viewer.