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