1 | // $Id: CrossSplitter.h 509 2006-02-18 13:47:32Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_classifier_cross_splitter_ |
---|
4 | #define _theplu_classifier_cross_splitter_ |
---|
5 | |
---|
6 | #include <c++_tools/classifier/Target.h> |
---|
7 | |
---|
8 | |
---|
9 | #include <cassert> |
---|
10 | #include <vector> |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace classifier { |
---|
14 | class DataLookup2D; |
---|
15 | |
---|
16 | |
---|
17 | /// |
---|
18 | /// Class splitting a set into training set and validation set in a |
---|
19 | /// crossvalidation manner. This is done in a balanced way, meaning |
---|
20 | /// the proportions between the classes in the trainingset is close |
---|
21 | /// to the proportions in the whole dataset. In the first \a k |
---|
22 | /// rounds each sample is returned k-1 times, for next round the |
---|
23 | /// samples are shuffled and... |
---|
24 | /// |
---|
25 | |
---|
26 | class CrossSplitter |
---|
27 | { |
---|
28 | |
---|
29 | public: |
---|
30 | /// |
---|
31 | /// Constructor taking \a target and \a k for k-fold cross |
---|
32 | /// validation |
---|
33 | /// |
---|
34 | |
---|
35 | CrossSplitter(const Target& target, const DataLookup2D&, |
---|
36 | const size_t N, const size_t k); |
---|
37 | |
---|
38 | /// |
---|
39 | /// Destructor |
---|
40 | /// |
---|
41 | ~CrossSplitter(); |
---|
42 | |
---|
43 | /// |
---|
44 | /// @return true if in a valid state |
---|
45 | /// |
---|
46 | inline bool more(void) const { return state_<size(); } |
---|
47 | |
---|
48 | /// |
---|
49 | /// Function turning the object to the next state. |
---|
50 | /// |
---|
51 | inline void next(void) { state_++; } |
---|
52 | |
---|
53 | /// |
---|
54 | /// rewind the sampler to initial state |
---|
55 | /// |
---|
56 | inline void reset(void) { state_=0; } |
---|
57 | |
---|
58 | |
---|
59 | /// |
---|
60 | /// @return number of partitions |
---|
61 | /// |
---|
62 | inline u_long size(void) const { return training_data_.size(); } |
---|
63 | |
---|
64 | /// |
---|
65 | /// @return the target for the total set |
---|
66 | /// |
---|
67 | inline const Target& target(void) const { return target_; } |
---|
68 | |
---|
69 | |
---|
70 | /// |
---|
71 | /// @return training data |
---|
72 | /// |
---|
73 | /// @note if state is invalid the result is undefined |
---|
74 | /// |
---|
75 | inline const DataLookup2D& training_data(void) const |
---|
76 | { assert(more()); return *(training_data_[state_]); } |
---|
77 | |
---|
78 | /// @return training index |
---|
79 | /// |
---|
80 | /// @note if state is invalid the result is undefined |
---|
81 | /// |
---|
82 | inline const std::vector<size_t>& training_index(void) const |
---|
83 | { assert(more()); return training_index_[state_]; } |
---|
84 | |
---|
85 | |
---|
86 | /// |
---|
87 | /// @return training target |
---|
88 | /// |
---|
89 | /// @note if state is invalid the result is undefined |
---|
90 | /// |
---|
91 | inline const Target& training_target(void) const |
---|
92 | { assert(more()); return training_target_[state_]; } |
---|
93 | |
---|
94 | /// |
---|
95 | /// @return validation data |
---|
96 | /// |
---|
97 | /// @note if state is invalid the result is undefined |
---|
98 | /// |
---|
99 | inline const DataLookup2D& validation_data(void) const |
---|
100 | { assert(more()); return *(validation_data_[state_]); } |
---|
101 | |
---|
102 | |
---|
103 | /// @return validation index |
---|
104 | /// |
---|
105 | /// @note if state is invalid the result is undefined |
---|
106 | /// |
---|
107 | inline const std::vector<size_t>& validation_index(void) const |
---|
108 | { assert(more()); return validation_index_[state_]; } |
---|
109 | |
---|
110 | /// |
---|
111 | /// @return validation target |
---|
112 | /// |
---|
113 | /// @note if state is invalid the result is undefined |
---|
114 | /// |
---|
115 | inline const Target& validation_target(void) const |
---|
116 | { assert(more()); return validation_target_[state_]; } |
---|
117 | |
---|
118 | private: |
---|
119 | const size_t k_; |
---|
120 | u_long state_; |
---|
121 | Target target_; |
---|
122 | |
---|
123 | std::vector<const DataLookup2D*> training_data_; |
---|
124 | std::vector<Target> training_target_; |
---|
125 | std::vector<std::vector<size_t> > training_index_; |
---|
126 | |
---|
127 | std::vector<const DataLookup2D*> validation_data_; |
---|
128 | std::vector<Target> validation_target_; |
---|
129 | std::vector<std::vector<size_t> > validation_index_; |
---|
130 | }; |
---|
131 | |
---|
132 | }} // of namespace classifier and namespace theplu |
---|
133 | |
---|
134 | #endif |
---|
135 | |
---|