1 | // $Id$ |
---|
2 | |
---|
3 | #include <c++_tools/classifier/ConsensusInputRanker.h> |
---|
4 | |
---|
5 | #include <c++_tools/classifier/InputRanker.h> |
---|
6 | #include <c++_tools/classifier/IRRetrieve.h> |
---|
7 | #include <c++_tools/classifier/MatrixLookup.h> |
---|
8 | #include <c++_tools/classifier/MatrixLookupWeighted.h> |
---|
9 | #include <c++_tools/classifier/Sampler.h> |
---|
10 | #include <c++_tools/classifier/Target.h> |
---|
11 | #include <c++_tools/statistics/Score.h> |
---|
12 | #include <c++_tools/statistics/utility.h> |
---|
13 | #include <c++_tools/utility/stl_utility.h> |
---|
14 | |
---|
15 | #include <cassert> |
---|
16 | #include <functional> |
---|
17 | #include <iostream> |
---|
18 | #include <utility> |
---|
19 | #include <vector> |
---|
20 | #include <cmath> |
---|
21 | |
---|
22 | namespace theplu { |
---|
23 | namespace classifier { |
---|
24 | |
---|
25 | ConsensusInputRanker::ConsensusInputRanker(const IRRetrieve& retriever) |
---|
26 | : retriever_(retriever) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | ConsensusInputRanker::ConsensusInputRanker(const Sampler& sampler, |
---|
32 | const MatrixLookup& data, |
---|
33 | statistics::Score& score, |
---|
34 | const IRRetrieve& retriever) |
---|
35 | : retriever_(retriever) |
---|
36 | { |
---|
37 | assert(sampler.size()); |
---|
38 | id_.resize(data.rows()); |
---|
39 | rank_.resize(data.rows()); |
---|
40 | for (size_t i=0; i<sampler.size(); ++i){ |
---|
41 | input_rankers_.push_back(InputRanker(MatrixLookup(data,sampler.training_index(i), false), |
---|
42 | sampler.training_target(i), |
---|
43 | score)); |
---|
44 | } |
---|
45 | update(); |
---|
46 | } |
---|
47 | |
---|
48 | ConsensusInputRanker::ConsensusInputRanker(const Sampler& sampler, |
---|
49 | const MatrixLookupWeighted& data, |
---|
50 | statistics::Score& score, |
---|
51 | const IRRetrieve& retriever) |
---|
52 | : retriever_(retriever) |
---|
53 | { |
---|
54 | assert(sampler.size()); |
---|
55 | id_.resize(data.rows()); |
---|
56 | rank_.resize(data.rows()); |
---|
57 | |
---|
58 | for (size_t i=0; i<sampler.size(); ++i){ |
---|
59 | input_rankers_.push_back(InputRanker(MatrixLookupWeighted(data,sampler.training_index(i), false), |
---|
60 | sampler.training_target(i), |
---|
61 | score)); |
---|
62 | } |
---|
63 | update(); |
---|
64 | } |
---|
65 | |
---|
66 | void ConsensusInputRanker::update(void) |
---|
67 | { |
---|
68 | |
---|
69 | // Sorting with respect to median info (from retriever_) |
---|
70 | std::vector<std::pair<double,size_t> > medians(id_.size()); |
---|
71 | for (size_t i=0; i<id_.size(); i++){ |
---|
72 | std::vector<double> scores; |
---|
73 | scores.reserve(input_rankers_.size()); |
---|
74 | for (size_t j=0; j<input_rankers_.size(); j++) { |
---|
75 | scores.push_back(retriever_(input_rankers_[j],i)); |
---|
76 | } |
---|
77 | medians[i].first = statistics::median(scores); |
---|
78 | medians[i].second = i; |
---|
79 | } |
---|
80 | |
---|
81 | //sort medians and assign id_ and rank_ |
---|
82 | sort(medians.begin(), medians.end(), |
---|
83 | std::greater<std::pair<double, size_t> >()); |
---|
84 | |
---|
85 | for (size_t i=0; i<medians.size(); i++){ |
---|
86 | id_[i]=medians[i].second; |
---|
87 | rank_[id_[i]]=i; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | }} // of namespace classifier and namespace theplu |
---|