1 | // $Id: CrossSplitting.cc 460 2005-12-16 12:40:06Z peter $ |
---|
2 | |
---|
3 | |
---|
4 | #include <c++_tools/gslapi/vector.h> |
---|
5 | #include <c++_tools/classifier/CrossSplitting.h> |
---|
6 | #include <c++_tools/random/random.h> |
---|
7 | |
---|
8 | #include <vector> |
---|
9 | |
---|
10 | namespace theplu { |
---|
11 | namespace classifier { |
---|
12 | |
---|
13 | CrossSplitting::CrossSplitting(const theplu::gslapi::vector& target, |
---|
14 | const size_t k) |
---|
15 | :count_(0),index_negative_(std::vector<size_t>()), |
---|
16 | index_positive_(std::vector<size_t>()), k_(k) |
---|
17 | |
---|
18 | { |
---|
19 | for (size_t i=0; i<target.size(); i++){ |
---|
20 | if (target(i)==1) |
---|
21 | index_positive_.push_back(i); |
---|
22 | else |
---|
23 | index_negative_.push_back(i); |
---|
24 | } |
---|
25 | |
---|
26 | random::DiscreteUniform a; |
---|
27 | random_shuffle(index_negative_.begin(), index_negative_.end(), a); |
---|
28 | random_shuffle(index_positive_.begin(), index_positive_.end(), a); |
---|
29 | } |
---|
30 | |
---|
31 | std::vector<size_t> CrossSplitting::next() |
---|
32 | { |
---|
33 | random::DiscreteUniform a; |
---|
34 | if (count_==k_){ |
---|
35 | count_=0; |
---|
36 | random_shuffle(index_negative_.begin(), index_negative_.end(), a); |
---|
37 | random_shuffle(index_positive_.begin(), index_positive_.end(), a); |
---|
38 | } |
---|
39 | |
---|
40 | count_++; |
---|
41 | std::vector<size_t> training_set; |
---|
42 | |
---|
43 | size_t begin = int(index_positive_.size()*(count_-1)/k_); |
---|
44 | size_t end = int(index_positive_.size()*count_/k_); |
---|
45 | for (size_t i=0; i<index_positive_.size(); i++) |
---|
46 | if (i<begin || i>=end) |
---|
47 | training_set.push_back(index_positive_[i]); |
---|
48 | |
---|
49 | begin = int(index_negative_.size()*(count_-1)/k_); |
---|
50 | end = int(index_negative_.size()*count_/k_); |
---|
51 | for (size_t i=0; i<index_negative_.size(); i++) |
---|
52 | if (i<begin || i>=end) |
---|
53 | training_set.push_back(index_negative_[i]); |
---|
54 | |
---|
55 | return training_set ; |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | }} // of namespace classifier and namespace theplu |
---|