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

Last change on this file since 1090 was 1086, checked in by Peter, 13 years ago

fixes #310

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1#ifndef _theplu_yat_classifier_subset_generator_
2#define _theplu_yat_classifier_subset_generator_
3
4// $Id: SubsetGenerator.h 1086 2008-02-14 05:43:10Z 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    typedef T value_type;
55
56    ///
57    /// @brief Constructor
58    /// 
59    /// @param sampler sampler
60    /// @param data data to split up in validation and training.
61    ///
62    SubsetGenerator(const Sampler& sampler, const T& data);
63
64    ///
65    /// @brief Constructor
66    /// 
67    /// @param sampler taking care of partioning dataset
68    /// @param data data to be split up in validation and training.
69    /// @param fs Object selecting features for each subset
70    ///
71    SubsetGenerator(const Sampler& sampler, const T& data, 
72                    FeatureSelector& fs);
73
74    ///
75    /// Destructor
76    ///
77    ~SubsetGenerator();
78 
79    ///
80    /// @return number of subsets
81    ///
82    u_long size(void) const;
83
84    ///
85    /// @return the target for the total set
86    ///
87    const Target& target(void) const;
88
89    ///
90    /// @return the sampler for the total set
91    ///
92    //    const Sampler& sampler(void) const;
93
94    ///
95    /// @return training data
96    ///
97    const T& training_data(size_t i) const;
98
99    ///
100    /// @return training features
101    ///
102    const std::vector<size_t>&
103    training_features(std::vector<size_t>::size_type i) const;
104
105    ///
106    /// @return training index
107    ///
108    const std::vector<size_t>&
109    training_index(std::vector<size_t>::size_type i) const;
110
111    ///
112    /// @return training target
113    ///
114    const Target& training_target(std::vector<Target>::size_type i) const;
115
116    ///
117    /// @return validation data
118    ///
119    const T& validation_data(size_t i) const;
120
121    ///
122    /// @return validation index
123    ///
124    const std::vector<size_t>&
125    validation_index(std::vector<size_t>::size_type i) const;
126
127    ///
128    /// @return validation target
129    ///
130    const Target& validation_target(std::vector<Target>::size_type i) const;
131
132    ///
133    /// @return true if weighted
134    /// @todo remove this function
135    //bool weighted(void) const;
136
137  private:
138    void build(const MatrixLookup&);
139    void build(const MatrixLookupWeighted&);
140    void build(const KernelLookup&);
141
142    SubsetGenerator(const SubsetGenerator&);
143    const SubsetGenerator& operator=(const SubsetGenerator&) const;
144
145    FeatureSelector* f_selector_;
146    std::vector<std::vector<size_t> > features_;
147    const Sampler& sampler_;
148    std::vector<const T*> training_data_;
149    std::vector<Target> training_target_;
150    std::vector<const T*> validation_data_;
151    std::vector<Target> validation_target_;
152
153  };
154
155
156  // templates
157
158  template<typename T>
159  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler, 
160                                      const T& data)
161    : f_selector_(NULL), sampler_(sampler)
162  { 
163    utility::yat_assert<std::runtime_error>(target().size()==data.columns());
164
165    training_data_.reserve(sampler_.size());
166    validation_data_.reserve(sampler_.size());
167    for (size_t i=0; i<sampler_.size(); ++i){
168      // Dynamically allocated. Must be deleted in destructor.
169      training_data_.push_back(data.training_data(sampler.training_index(i)));
170      validation_data_.push_back(data.validation_data(sampler.training_index(i),
171                                                      sampler.validation_index(i)));
172
173      training_target_.push_back(Target(target(),sampler.training_index(i)));
174      validation_target_.push_back(Target(target(),
175                                          sampler.validation_index(i)));
176      utility::yat_assert<std::runtime_error>(training_data_.size()==i+1);
177      utility::yat_assert<std::runtime_error>(training_target_.size()==i+1);
178      utility::yat_assert<std::runtime_error>(validation_data_.size()==i+1);
179      utility::yat_assert<std::runtime_error>(validation_target_.size()==i+1);
180    }
181
182    // No feature selection, hence features same for all partitions
183    // and can be stored in features_[0]
184    features_.resize(1);
185    features_[0].reserve(data.rows());
186    for (size_t i=0; i<data.rows(); ++i)
187      features_[0].push_back(i);
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      const DataLookup2D* matrix = kernel.data();
282      // dynamically allocated must be deleted
283      const DataLookup2D* training_matrix = 
284        matrix->training_data(training_index(k));
285      if (matrix->weighted()){
286        const MatrixLookupWeighted& ml = 
287          dynamic_cast<const MatrixLookupWeighted&>(*matrix);
288        f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false), 
289                            training_target(k));
290      }
291      else {
292        const MatrixLookup& ml = 
293          dynamic_cast<const MatrixLookup&>(*matrix);
294        f_selector_->update(MatrixLookup(ml,training_index(k), false), 
295                            training_target(k));
296      } 
297      std::vector<size_t> dummie=f_selector_->features();
298      features_.push_back(dummie);
299      //features_.push_back(f_selector_->features());
300      const KernelLookup* kl = kernel.selected(features_.back());
301      utility::yat_assert<std::runtime_error>(training_matrix);
302      delete training_matrix;
303     
304      // Dynamically allocated. Must be deleted in destructor.
305      training_data_.push_back(kl->training_data(training_index(k)));
306      validation_data_.push_back(kl->validation_data(training_index(k), 
307                                                     validation_index(k)));
308      utility::yat_assert<std::runtime_error>(kl);
309      delete kl;
310    }
311  }
312
313
314  template<typename T>
315  u_long SubsetGenerator<T>::size(void) const
316  {
317    return sampler_.size();
318  }
319
320
321  template<typename T>
322  const Target& SubsetGenerator<T>::target(void) const
323  {
324    return sampler_.target();
325  }
326
327
328  template<typename T>
329  const T&
330  SubsetGenerator<T>::training_data(size_t i) const 
331  {
332    return *(training_data_[i]);
333  }
334
335
336  template<typename T>
337  const std::vector<size_t>&
338  SubsetGenerator<T>::training_features(typename std::vector<size_t>::size_type i) const
339  {
340    return f_selector_ ? features_[i] : features_[0];
341  }
342
343
344  template<typename T>
345  const std::vector<size_t>&
346  SubsetGenerator<T>::training_index(std::vector<size_t>::size_type i) const
347  {
348    return sampler_.training_index(i);
349  }
350
351
352  template<typename T>
353  const Target&
354  SubsetGenerator<T>::training_target(std::vector<Target>::size_type i) const
355  {
356    return training_target_[i];
357  }
358
359
360  template<typename T>
361  const T&
362  SubsetGenerator<T>::validation_data(size_t i) const
363  {
364    return *(validation_data_[i]);
365  }
366
367
368  template<typename T>
369  const std::vector<size_t>&
370  SubsetGenerator<T>::validation_index(std::vector<size_t>::size_type i) const
371  {
372    return sampler_.validation_index(i);
373  }
374
375
376  template<typename T>
377  const Target&
378  SubsetGenerator<T>::validation_target(std::vector<Target>::size_type i) const
379  {
380    return validation_target_[i];
381  }
382
383}}} // of namespace classifier, yat, and theplu
384
385#endif
386
Note: See TracBrowser for help on using the repository browser.