source: trunk/c++_tools/classifier/SubsetGenerator.h @ 615

Last change on this file since 615 was 615, checked in by Peter, 15 years ago

ref #60 NOTE: there is most likely a bug around. I have removed the ensemble.build() test in the ensemble_test to get the test go through. I will try to find and remove this bug asap.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1#ifndef _theplu_classifier_subset_generator_
2#define _theplu_classifier_subset_generator_
3
4// $Id: SubsetGenerator.h 615 2006-08-31 05:33:35Z peter $
5
6/*
7  Copyright (C) 2006 Markus Ringnér, Peter Johansson
8
9  This file is part of the thep c++ tools library,
10                                http://lev.thep.lu.se/trac/c++_tools
11
12  The c++ tools library is free software; you can redistribute it
13  and/or modify it under the terms of the GNU General Public License
14  as published by the Free Software Foundation; either version 2 of
15  the License, or (at your option) any later version.
16
17  The c++ tools library is distributed in the hope that it will be
18  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27#include <c++_tools/classifier/Target.h>
28#include <c++_tools/classifier/Sampler.h>
29
30#include <cassert>
31#include <vector>
32
33namespace theplu {
34namespace classifier { 
35  class DataLookup2D;
36  class FeatureSelector;
37  class MatrixLookup;
38
39  ///
40  /// Class splitting a set into training set and validation set using
41  /// a Sampler method.
42  ///   
43  class SubsetGenerator
44  {
45 
46  public:
47    ///
48    /// @brief Constructor
49    /// 
50    /// @parameter sampler sampler
51    /// @parameter data data to split up in validation and training.
52    ///
53    SubsetGenerator(const Sampler& sampler, const DataLookup2D& data);
54
55
56    ///
57    /// @brief Constructor with weights
58    /// 
59    /// @parameter data data to split up in validation and training.
60    /// @parameter weights accompanying data.
61    ///
62    /// @todo This most likely be removed.
63    SubsetGenerator(const Sampler& sampler, const DataLookup2D& data,
64                    const MatrixLookup& weight);
65
66
67    ///
68    /// @brief Constructor
69    /// 
70    /// @parameter Sampler
71    /// @parameter data data to be split up in validation and training.
72    ///
73    SubsetGenerator(const Sampler& sampler, const DataLookup2D& data, 
74                    FeatureSelector& fs);
75
76    ///
77    /// Destructor
78    ///
79    ~SubsetGenerator();
80
81    ///
82    /// @return true if in a valid state
83    ///
84    inline bool more(void) const { return state_<size(); } 
85
86    ///
87    /// Function turning the object to the next state.
88    ///
89    inline void next(void) { state_++; }
90
91    ///
92    /// rewind object to initial state
93    ///
94    inline void reset(void) { state_=0; }
95
96    ///
97    /// @return number of subsets
98    ///
99    inline u_long size(void) const { return sampler_.size(); }
100
101    ///
102    /// @return the target for the total set
103    ///
104    inline const Target& target(void) const { return sampler_.target(); }
105
106
107    ///
108    /// @return the target for the total set
109    ///
110    inline const Sampler& sampler(void) const { return sampler_; }
111
112
113    ///
114    /// @return training data
115    ///
116    inline const DataLookup2D& training_data(void) const 
117    { assert(more()); return *(training_data_[state_]); } 
118
119    ///
120    /// @return training features
121    ///
122    inline const std::vector<size_t>& training_features(void) const
123    { assert(more()); return f_selector_ ? features_[state_] : features_[0]; }
124
125
126    ///
127    /// @return training index
128    ///
129    inline const std::vector<size_t>& training_index(void) const
130    { assert(more()); return sampler_.training_index(state_); }
131
132    ///
133    /// @return training target
134    ///
135    inline const Target& training_target(void) const 
136    { assert(more()); return training_target_[state_]; }
137
138    ///
139    /// @return training data weight
140    /// @todo remove this function
141    inline const MatrixLookup& training_weight(void) const 
142    { assert(more()); return *(training_weight_[state_]); } 
143
144    ///
145    /// @return validation data
146    ///
147    inline const DataLookup2D& validation_data(void) const
148    { assert(more()); return *(validation_data_[state_]); }
149
150    ///
151    /// @return validation index
152    ///
153    inline const std::vector<size_t>& validation_index(void) const
154    { assert(more()); return sampler_.validation_index(state_); }
155
156    ///
157    /// @return validation target
158    ///
159    inline const Target& validation_target(void) const 
160    { assert(more()); return validation_target_[state_]; }
161
162    ///
163    /// @return validation data weights
164    /// @todo remove this function
165    inline const MatrixLookup& validation_weight(void) const 
166    { assert(more()); return *(validation_weight_[state_]); } 
167
168    ///
169    /// @return true if weighted
170    /// @todo remove this function
171    inline bool weighted(void) const { return weighted_; }
172
173  private:
174    SubsetGenerator(const SubsetGenerator&);
175    const SubsetGenerator& operator=(const SubsetGenerator&) const;
176
177    FeatureSelector* f_selector_;
178    std::vector<std::vector<size_t> > features_;
179    const Sampler& sampler_;
180    size_t state_;
181    std::vector<const DataLookup2D*> training_data_;
182    std::vector<Target> training_target_;
183    std::vector<const MatrixLookup*> training_weight_;
184    std::vector<const DataLookup2D*> validation_data_;
185    std::vector<Target> validation_target_;
186    std::vector<const MatrixLookup*> validation_weight_;
187    const bool weighted_;
188
189  };
190
191}} // of namespace classifier and namespace theplu
192
193#endif
194
Note: See TracBrowser for help on using the repository browser.