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

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

removed dynamic_casts

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1#ifndef _theplu_yat_classifier_subset_generator_
2#define _theplu_yat_classifier_subset_generator_
3
4// $Id: SubsetGenerator.h 1165 2008-02-26 19:06:28Z 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 "FeatureSelector.h"
29#include "KernelLookup.h"
30#include "MatrixLookup.h"
31#include "MatrixLookupWeighted.h"
32#include "Target.h"
33#include "Sampler.h"
34#include "yat/utility/Index.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 utility::Index&
106    training_features(std::vector<size_t>::size_type i) const;
107
108    ///
109    /// @return training index
110    ///
111    const utility::Index&
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 utility::Index&
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<utility::Index > 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_.push_back(utility::Index(data.rows()));
188
189    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
190    utility::yat_assert<std::runtime_error>(training_target_.size()==size());
191    utility::yat_assert<std::runtime_error>(validation_data_.size()==size());
192    utility::yat_assert<std::runtime_error>(validation_target_.size()==size());
193  }
194
195
196  template<typename T>
197  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler, 
198                                   const T& data, 
199                                   FeatureSelector& fs)
200    : f_selector_(&fs), sampler_(sampler)
201  { 
202    utility::yat_assert<std::runtime_error>(target().size()==data.columns());
203    features_.reserve(size());
204    training_data_.reserve(size());
205    validation_data_.reserve(size());
206    build(data);
207    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
208    utility::yat_assert<std::runtime_error>(training_target_.size()==size());
209    utility::yat_assert<std::runtime_error>(validation_data_.size()==size());
210    utility::yat_assert<std::runtime_error>(validation_target_.size()==size());
211  }
212
213
214  template<typename T>
215  SubsetGenerator<T>::~SubsetGenerator()
216  {
217    utility::yat_assert<std::runtime_error>(training_data_.size()==validation_data_.size());
218    for (size_t i=0; i<training_data_.size(); i++) 
219      delete training_data_[i];
220    for (size_t i=0; i<validation_data_.size(); i++) 
221      delete validation_data_[i];
222  }
223
224
225  template<typename T>
226  void SubsetGenerator<T>::build(const MatrixLookup& ml)
227  {
228    for (size_t k=0; k<size(); k++){
229      training_target_.push_back(Target(target(),training_index(k)));
230      validation_target_.push_back(Target(target(),validation_index(k)));
231      // training data with no feature selection
232      const MatrixLookup* train_data_all_feat = 
233        ml.training_data(training_index(k));
234      // use these data to create feature selection
235      utility::yat_assert<std::runtime_error>(train_data_all_feat);
236      f_selector_->update(*train_data_all_feat, training_target(k));
237        // get features
238      features_.push_back(f_selector_->features());
239      utility::yat_assert<std::runtime_error>(train_data_all_feat);
240      delete train_data_all_feat;
241     
242      // Dynamically allocated. Must be deleted in destructor.
243      training_data_.push_back(new MatrixLookup(ml,features_.back(), 
244                                                training_index(k)));
245      validation_data_.push_back(new MatrixLookup(ml,features_.back(), 
246                                                  validation_index(k)));     
247    }
248
249  }
250
251
252  template<typename T>
253  void SubsetGenerator<T>::build(const MatrixLookupWeighted& ml)
254  {
255    for (u_long k=0; k<size(); k++){
256      training_target_.push_back(Target(target(),training_index(k)));
257      validation_target_.push_back(Target(target(),validation_index(k)));
258      // training data with no feature selection
259      const MatrixLookupWeighted* train_data_all_feat = 
260        ml.training_data(training_index(k));
261      // use these data to create feature selection
262      f_selector_->update(*train_data_all_feat, training_target(k));
263      // get features
264      features_.push_back(f_selector_->features());
265      delete train_data_all_feat;
266     
267      // Dynamically allocated. Must be deleted in destructor.
268      training_data_.push_back(new MatrixLookupWeighted(ml, features_.back(), 
269                                                        training_index(k)));
270      validation_data_.push_back(new MatrixLookupWeighted(ml, features_.back(), 
271                                                          validation_index(k)));
272    }
273  }
274
275  template<typename T>
276  void SubsetGenerator<T>::build(const KernelLookup& kernel)
277  {
278    for (u_long k=0; k<size(); k++){
279      training_target_.push_back(Target(target(),training_index(k)));
280      validation_target_.push_back(Target(target(),validation_index(k)));
281
282      if (kernel.weighted()){
283        utility::SmartPtr<const MatrixLookupWeighted> ml=kernel.data_weighted();
284        f_selector_->update(MatrixLookupWeighted(*ml,training_index(k),false), 
285                            training_target(k));
286      }
287      else {
288        utility::SmartPtr<const MatrixLookup> ml=kernel.data();
289        f_selector_->update(MatrixLookup(*ml,training_index(k), false), 
290                            training_target(k));
291      } 
292      utility::Index dummie=f_selector_->features();
293      features_.push_back(dummie);
294      //features_.push_back(f_selector_->features());
295      const KernelLookup* kl = kernel.selected(features_.back());
296     
297      // Dynamically allocated. Must be deleted in destructor.
298      training_data_.push_back(kl->training_data(training_index(k)));
299      validation_data_.push_back(kl->validation_data(training_index(k), 
300                                                     validation_index(k)));
301      utility::yat_assert<std::runtime_error>(kl);
302      delete kl;
303    }
304  }
305
306
307  template<typename T>
308  u_long SubsetGenerator<T>::size(void) const
309  {
310    return sampler_.size();
311  }
312
313
314  template<typename T>
315  const Target& SubsetGenerator<T>::target(void) const
316  {
317    return sampler_.target();
318  }
319
320
321  template<typename T>
322  const T&
323  SubsetGenerator<T>::training_data(size_t i) const 
324  {
325    return *(training_data_[i]);
326  }
327
328
329  template<typename T>
330  const utility::Index&
331  SubsetGenerator<T>::training_features(size_t i) const
332  {
333    return f_selector_ ? features_[i] : features_[0];
334  }
335
336
337  template<typename T>
338  const utility::Index&
339  SubsetGenerator<T>::training_index(size_t i) const
340  {
341    return sampler_.training_index(i);
342  }
343
344
345  template<typename T>
346  const Target&
347  SubsetGenerator<T>::training_target(std::vector<Target>::size_type i) const
348  {
349    return training_target_[i];
350  }
351
352
353  template<typename T>
354  const T&
355  SubsetGenerator<T>::validation_data(size_t i) const
356  {
357    return *(validation_data_[i]);
358  }
359
360
361  template<typename T>
362  const utility::Index&
363  SubsetGenerator<T>::validation_index(std::vector<size_t>::size_type i) const
364  {
365    return sampler_.validation_index(i);
366  }
367
368
369  template<typename T>
370  const Target&
371  SubsetGenerator<T>::validation_target(std::vector<Target>::size_type i) const
372  {
373    return validation_target_[i];
374  }
375
376}}} // of namespace classifier, yat, and theplu
377
378#endif
379
Note: See TracBrowser for help on using the repository browser.