source: trunk/yat/classifier/SubsetGenerator.h @ 1125

Last change on this file since 1125 was 1125, checked in by Peter, 14 years ago

fixing Doxygen parsing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
1#ifndef _theplu_yat_classifier_subset_generator_
2#define _theplu_yat_classifier_subset_generator_
3
4// $Id: SubsetGenerator.h 1125 2008-02-22 21:31:22Z peter $
5
6/*
7  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
8  Copyright (C) 2007 Peter Johansson
9
10  This file is part of the yat library, http://trac.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 2 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  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
28#include "DataLookup2D.h"
29#include "FeatureSelector.h"
30#include "KernelLookup.h"
31#include "MatrixLookup.h"
32#include "MatrixLookupWeighted.h"
33#include "Target.h"
34#include "Sampler.h"
35#include "yat/utility/yat_assert.h"
36
37#include <algorithm>
38#include <cassert>
39#include <utility>
40#include <typeinfo>
41#include <vector>
42
43namespace theplu {
44namespace yat {
45namespace classifier { 
46
47  ///
48  /// @brief Class splitting a set into training set and validation set.
49  ///
50  template <typename T> 
51  class SubsetGenerator
52  {
53  public:
54    /**
55       type of data that is stored in SubsetGenerator
56     */
57    typedef T value_type;
58
59    ///
60    /// @brief Constructor
61    /// 
62    /// @param sampler sampler
63    /// @param data data to split up in validation and training.
64    ///
65    SubsetGenerator(const Sampler& sampler, const T& data);
66
67    ///
68    /// @brief Constructor
69    /// 
70    /// @param sampler taking care of partioning dataset
71    /// @param data data to be split up in validation and training.
72    /// @param fs Object selecting features for each subset
73    ///
74    SubsetGenerator(const Sampler& sampler, const T& data, 
75                    FeatureSelector& fs);
76
77    ///
78    /// Destructor
79    ///
80    ~SubsetGenerator();
81 
82    ///
83    /// @return number of subsets
84    ///
85    u_long size(void) const;
86
87    ///
88    /// @return the target for the total set
89    ///
90    const Target& target(void) const;
91
92    ///
93    /// @return the sampler for the total set
94    ///
95    //    const Sampler& sampler(void) const;
96
97    ///
98    /// @return training data
99    ///
100    const T& training_data(size_t i) const;
101
102    ///
103    /// @return training features
104    ///
105    const std::vector<size_t>&
106    training_features(std::vector<size_t>::size_type i) const;
107
108    ///
109    /// @return training index
110    ///
111    const std::vector<size_t>&
112    training_index(std::vector<size_t>::size_type i) const;
113
114    ///
115    /// @return training target
116    ///
117    const Target& training_target(std::vector<Target>::size_type i) const;
118
119    ///
120    /// @return validation data
121    ///
122    const T& validation_data(size_t i) const;
123
124    ///
125    /// @return validation index
126    ///
127    const std::vector<size_t>&
128    validation_index(std::vector<size_t>::size_type i) const;
129
130    ///
131    /// @return validation target
132    ///
133    const Target& validation_target(std::vector<Target>::size_type i) const;
134
135    ///
136    /// @return true if weighted
137    /// @todo remove this function
138    //bool weighted(void) const;
139
140  private:
141    void build(const MatrixLookup&);
142    void build(const MatrixLookupWeighted&);
143    void build(const KernelLookup&);
144
145    SubsetGenerator(const SubsetGenerator&);
146    const SubsetGenerator& operator=(const SubsetGenerator&) const;
147
148    FeatureSelector* f_selector_;
149    std::vector<std::vector<size_t> > features_;
150    const Sampler& sampler_;
151    std::vector<const T*> training_data_;
152    std::vector<Target> training_target_;
153    std::vector<const T*> validation_data_;
154    std::vector<Target> validation_target_;
155
156  };
157
158
159  // templates
160
161  template<typename T>
162  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler, 
163                                      const T& data)
164    : f_selector_(NULL), sampler_(sampler)
165  { 
166    utility::yat_assert<std::runtime_error>(target().size()==data.columns());
167
168    training_data_.reserve(sampler_.size());
169    validation_data_.reserve(sampler_.size());
170    for (size_t i=0; i<sampler_.size(); ++i){
171      // Dynamically allocated. Must be deleted in destructor.
172      training_data_.push_back(data.training_data(sampler.training_index(i)));
173      validation_data_.push_back(data.validation_data(sampler.training_index(i),
174                                                      sampler.validation_index(i)));
175
176      training_target_.push_back(Target(target(),sampler.training_index(i)));
177      validation_target_.push_back(Target(target(),
178                                          sampler.validation_index(i)));
179      utility::yat_assert<std::runtime_error>(training_data_.size()==i+1);
180      utility::yat_assert<std::runtime_error>(training_target_.size()==i+1);
181      utility::yat_assert<std::runtime_error>(validation_data_.size()==i+1);
182      utility::yat_assert<std::runtime_error>(validation_target_.size()==i+1);
183    }
184
185    // No feature selection, hence features same for all partitions
186    // and can be stored in features_[0]
187    features_.resize(1);
188    features_[0].reserve(data.rows());
189    for (size_t i=0; i<data.rows(); ++i)
190      features_[0].push_back(i);
191
192    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
193    utility::yat_assert<std::runtime_error>(training_target_.size()==size());
194    utility::yat_assert<std::runtime_error>(validation_data_.size()==size());
195    utility::yat_assert<std::runtime_error>(validation_target_.size()==size());
196  }
197
198
199  template<typename T>
200  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler, 
201                                   const T& data, 
202                                   FeatureSelector& fs)
203    : f_selector_(&fs), sampler_(sampler)
204  { 
205    utility::yat_assert<std::runtime_error>(target().size()==data.columns());
206    features_.reserve(size());
207    training_data_.reserve(size());
208    validation_data_.reserve(size());
209    build(data);
210    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
211    utility::yat_assert<std::runtime_error>(training_target_.size()==size());
212    utility::yat_assert<std::runtime_error>(validation_data_.size()==size());
213    utility::yat_assert<std::runtime_error>(validation_target_.size()==size());
214  }
215
216
217  template<typename T>
218  SubsetGenerator<T>::~SubsetGenerator()
219  {
220    utility::yat_assert<std::runtime_error>(training_data_.size()==validation_data_.size());
221    for (size_t i=0; i<training_data_.size(); i++) 
222      delete training_data_[i];
223    for (size_t i=0; i<validation_data_.size(); i++) 
224      delete validation_data_[i];
225  }
226
227
228  template<typename T>
229  void SubsetGenerator<T>::build(const MatrixLookup& ml)
230  {
231    for (size_t k=0; k<size(); k++){
232      training_target_.push_back(Target(target(),training_index(k)));
233      validation_target_.push_back(Target(target(),validation_index(k)));
234      // training data with no feature selection
235      const MatrixLookup* train_data_all_feat = 
236        ml.training_data(training_index(k));
237      // use these data to create feature selection
238      utility::yat_assert<std::runtime_error>(train_data_all_feat);
239      f_selector_->update(*train_data_all_feat, training_target(k));
240        // get features
241      features_.push_back(f_selector_->features());
242      utility::yat_assert<std::runtime_error>(train_data_all_feat);
243      delete train_data_all_feat;
244     
245      // Dynamically allocated. Must be deleted in destructor.
246      training_data_.push_back(new MatrixLookup(ml,features_.back(), 
247                                                training_index(k)));
248      validation_data_.push_back(new MatrixLookup(ml,features_.back(), 
249                                                  validation_index(k)));     
250    }
251
252  }
253
254
255  template<typename T>
256  void SubsetGenerator<T>::build(const MatrixLookupWeighted& ml)
257  {
258    for (u_long k=0; k<size(); k++){
259      training_target_.push_back(Target(target(),training_index(k)));
260      validation_target_.push_back(Target(target(),validation_index(k)));
261      // training data with no feature selection
262      const MatrixLookupWeighted* train_data_all_feat = 
263        ml.training_data(training_index(k));
264      // use these data to create feature selection
265      f_selector_->update(*train_data_all_feat, training_target(k));
266      // get features
267      features_.push_back(f_selector_->features());
268      delete train_data_all_feat;
269     
270      // Dynamically allocated. Must be deleted in destructor.
271      training_data_.push_back(new MatrixLookupWeighted(ml, features_.back(), 
272                                                        training_index(k)));
273      validation_data_.push_back(new MatrixLookupWeighted(ml, features_.back(), 
274                                                          validation_index(k)));
275    }
276  }
277
278  template<typename T>
279  void SubsetGenerator<T>::build(const KernelLookup& kernel)
280  {
281    for (u_long k=0; k<size(); k++){
282      training_target_.push_back(Target(target(),training_index(k)));
283      validation_target_.push_back(Target(target(),validation_index(k)));
284      const DataLookup2D* matrix = kernel.data();
285      // dynamically allocated must be deleted
286      const DataLookup2D* training_matrix = 
287        matrix->training_data(training_index(k));
288      if (matrix->weighted()){
289        const MatrixLookupWeighted& ml = 
290          dynamic_cast<const MatrixLookupWeighted&>(*matrix);
291        f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false), 
292                            training_target(k));
293      }
294      else {
295        const MatrixLookup& ml = 
296          dynamic_cast<const MatrixLookup&>(*matrix);
297        f_selector_->update(MatrixLookup(ml,training_index(k), false), 
298                            training_target(k));
299      } 
300      std::vector<size_t> dummie=f_selector_->features();
301      features_.push_back(dummie);
302      //features_.push_back(f_selector_->features());
303      const KernelLookup* kl = kernel.selected(features_.back());
304      utility::yat_assert<std::runtime_error>(training_matrix);
305      delete training_matrix;
306     
307      // Dynamically allocated. Must be deleted in destructor.
308      training_data_.push_back(kl->training_data(training_index(k)));
309      validation_data_.push_back(kl->validation_data(training_index(k), 
310                                                     validation_index(k)));
311      utility::yat_assert<std::runtime_error>(kl);
312      delete kl;
313    }
314  }
315
316
317  template<typename T>
318  u_long SubsetGenerator<T>::size(void) const
319  {
320    return sampler_.size();
321  }
322
323
324  template<typename T>
325  const Target& SubsetGenerator<T>::target(void) const
326  {
327    return sampler_.target();
328  }
329
330
331  template<typename T>
332  const T&
333  SubsetGenerator<T>::training_data(size_t i) const 
334  {
335    return *(training_data_[i]);
336  }
337
338
339  template<typename T>
340  const std::vector<size_t>&
341  SubsetGenerator<T>::training_features(typename std::vector<size_t>::size_type i) const
342  {
343    return f_selector_ ? features_[i] : features_[0];
344  }
345
346
347  template<typename T>
348  const std::vector<size_t>&
349  SubsetGenerator<T>::training_index(std::vector<size_t>::size_type i) const
350  {
351    return sampler_.training_index(i);
352  }
353
354
355  template<typename T>
356  const Target&
357  SubsetGenerator<T>::training_target(std::vector<Target>::size_type i) const
358  {
359    return training_target_[i];
360  }
361
362
363  template<typename T>
364  const T&
365  SubsetGenerator<T>::validation_data(size_t i) const
366  {
367    return *(validation_data_[i]);
368  }
369
370
371  template<typename T>
372  const std::vector<size_t>&
373  SubsetGenerator<T>::validation_index(std::vector<size_t>::size_type i) const
374  {
375    return sampler_.validation_index(i);
376  }
377
378
379  template<typename T>
380  const Target&
381  SubsetGenerator<T>::validation_target(std::vector<Target>::size_type i) const
382  {
383    return validation_target_[i];
384  }
385
386}}} // of namespace classifier, yat, and theplu
387
388#endif
389
Note: See TracBrowser for help on using the repository browser.