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

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

fixes #311

  • 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 1079 2008-02-13 11:44: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 "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
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    ///
66    /// @brief Constructor
67    /// 
68    /// @param sampler taking care of partioning dataset
69    /// @param data data to be split up in validation and training.
70    /// @param fs Object selecting features for each subset
71    ///
72    SubsetGenerator(const Sampler& sampler, const T& data, 
73                    FeatureSelector& fs);
74
75    ///
76    /// Destructor
77    ///
78    ~SubsetGenerator();
79 
80    ///
81    /// @return number of subsets
82    ///
83    u_long size(void) const;
84
85    ///
86    /// @return the target for the total set
87    ///
88    const Target& target(void) const;
89
90    ///
91    /// @return the sampler for the total set
92    ///
93    //    const Sampler& sampler(void) const;
94
95    ///
96    /// @return training data
97    ///
98    const T& training_data(size_t i) const;
99
100    ///
101    /// @return training features
102    ///
103    const std::vector<size_t>&
104    training_features(std::vector<size_t>::size_type i) const;
105
106    ///
107    /// @return training index
108    ///
109    const std::vector<size_t>&
110    training_index(std::vector<size_t>::size_type i) const;
111
112    ///
113    /// @return training target
114    ///
115    const Target& training_target(std::vector<Target>::size_type i) const;
116
117    ///
118    /// @return validation data
119    ///
120    const T& validation_data(size_t i) const;
121
122    ///
123    /// @return validation index
124    ///
125    const std::vector<size_t>&
126    validation_index(std::vector<size_t>::size_type i) const;
127
128    ///
129    /// @return validation target
130    ///
131    const Target& validation_target(std::vector<Target>::size_type i) const;
132
133    ///
134    /// @return true if weighted
135    /// @todo remove this function
136    //bool weighted(void) const;
137
138  private:
139    SubsetGenerator(const SubsetGenerator&);
140    const SubsetGenerator& operator=(const SubsetGenerator&) const;
141
142    FeatureSelector* f_selector_;
143    std::vector<std::vector<size_t> > features_;
144    const Sampler& sampler_;
145    std::vector<const T*> training_data_;
146    std::vector<Target> training_target_;
147    std::vector<const T*> validation_data_;
148    std::vector<Target> validation_target_;
149    const bool weighted_;
150
151  };
152
153
154  // templates
155
156  template<typename T>
157  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler, 
158                                   const T& data)
159    : f_selector_(NULL), sampler_(sampler), weighted_(false)
160  { 
161    assert(target().size()==data.columns());
162
163    training_data_.reserve(sampler_.size());
164    validation_data_.reserve(sampler_.size());
165    for (size_t i=0; i<sampler_.size(); ++i){
166      // Dynamically allocated. Must be deleted in destructor.
167      training_data_.push_back(data.training_data(sampler.training_index(i)));
168      validation_data_.push_back(data.validation_data(sampler.training_index(i),
169                                                      sampler.validation_index(i)));
170
171      training_target_.push_back(Target(target(),sampler.training_index(i)));
172      validation_target_.push_back(Target(target(),
173                                          sampler.validation_index(i)));
174      assert(training_data_.size()==i+1);
175      assert(training_target_.size()==i+1);
176      assert(validation_data_.size()==i+1);
177      assert(validation_target_.size()==i+1);
178    }
179
180    // No feature selection, hence features same for all partitions
181    // and can be stored in features_[0]
182    features_.resize(1);
183    features_[0].reserve(data.rows());
184    for (size_t i=0; i<data.rows(); ++i)
185      features_[0].push_back(i);
186
187    assert(training_data_.size()==size());
188    assert(training_target_.size()==size());
189    assert(validation_data_.size()==size());
190    assert(validation_target_.size()==size());
191  }
192
193
194  template<typename T>
195  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler, 
196                                   const T& data, 
197                                   FeatureSelector& fs)
198    : f_selector_(&fs), sampler_(sampler), weighted_(false)
199  { 
200    assert(target().size()==data.columns());
201
202    features_.reserve(size());
203    training_data_.reserve(size());
204    validation_data_.reserve(size());
205
206    // Taking care of three different case.
207    // We start with the case of MatrixLookup
208    const MatrixLookup* ml = dynamic_cast<const MatrixLookup*>(&data);
209    if (ml){
210      for (size_t k=0; k<size(); k++){
211     
212        training_target_.push_back(Target(target(),training_index(k)));
213        validation_target_.push_back(Target(target(),validation_index(k)));
214        // training data with no feature selection
215        const MatrixLookup* train_data_all_feat = 
216          ml->training_data(training_index(k));
217        // use these data to create feature selection
218        assert(train_data_all_feat);
219        f_selector_->update(*train_data_all_feat, training_target(k));
220        // get features
221        features_.push_back(f_selector_->features());
222        assert(train_data_all_feat);
223        delete train_data_all_feat;
224       
225        // Dynamically allocated. Must be deleted in destructor.
226        training_data_.push_back(new MatrixLookup(*ml,features_.back(), 
227                                                  training_index(k)));
228        validation_data_.push_back(new MatrixLookup(*ml,features_.back(), 
229                                                    validation_index(k)));     
230      }
231    }
232    else {
233      // Second the case of MatrixLookupWeighted
234      const MatrixLookupWeighted* ml = 
235        dynamic_cast<const MatrixLookupWeighted*>(&data);
236      if (ml){       
237        for (u_long k=0; k<size(); k++){
238          training_target_.push_back(Target(target(),training_index(k)));
239          validation_target_.push_back(Target(target(),validation_index(k)));
240          // training data with no feature selection
241          const MatrixLookupWeighted* train_data_all_feat = 
242            ml->training_data(training_index(k));
243          // use these data to create feature selection
244          f_selector_->update(*train_data_all_feat, training_target(k));
245          // get features
246          features_.push_back(f_selector_->features());
247          delete train_data_all_feat;
248         
249          // Dynamically allocated. Must be deleted in destructor.
250          training_data_.push_back(new MatrixLookupWeighted(*ml,
251                                                            features_.back(), 
252                                                            training_index(k)
253                                                            ));
254          validation_data_.push_back(new MatrixLookupWeighted(*ml,
255                                                              features_.back(), 
256                                                              validation_index(k)
257                                                              ));     
258        }
259      }
260      else {
261        // Third the case of MatrixLookupWeighted
262        const KernelLookup* kernel = dynamic_cast<const KernelLookup*>(&data);
263        if (kernel){
264          for (u_long k=0; k<size(); k++){
265            training_target_.push_back(Target(target(),training_index(k)));
266            validation_target_.push_back(Target(target(),validation_index(k)));
267            const T* matrix = kernel->data();
268            // dynamically allocated must be deleted
269            const T* training_matrix = 
270              matrix->training_data(training_index(k));
271            if (matrix->weighted()){
272              const MatrixLookupWeighted& ml = 
273                dynamic_cast<const MatrixLookupWeighted&>(*matrix);
274              f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false), 
275                                  training_target(k));
276            }
277            else {
278              const MatrixLookup& ml = 
279                dynamic_cast<const MatrixLookup&>(*matrix);
280              f_selector_->update(MatrixLookup(ml,training_index(k), false), 
281                                  training_target(k));
282            } 
283            std::vector<size_t> dummie=f_selector_->features();
284            features_.push_back(dummie);
285            //features_.push_back(f_selector_->features());
286            assert(kernel);
287            const KernelLookup* kl = kernel->selected(features_.back());
288            assert(training_matrix);
289            delete training_matrix;
290                     
291            // Dynamically allocated. Must be deleted in destructor.
292            training_data_.push_back(kl->training_data(training_index(k)));
293            validation_data_.push_back(kl->validation_data(training_index(k), 
294                                                           validation_index(k)));
295            assert(kl);
296            delete kl;
297          }
298        }
299        else {
300        std::cerr << "Sorry, your type of T (" 
301                  << typeid(data).name() << ")\nis not supported in " 
302                  << "SubsetGenerator with\nFeatureSelection\n";
303        exit(-1);
304        }
305      }
306    }
307    assert(training_data_.size()==size());
308    assert(training_target_.size()==size());
309    assert(validation_data_.size()==size());
310    assert(validation_target_.size()==size());
311  }
312
313
314  template<typename T>
315  SubsetGenerator<T>::~SubsetGenerator()
316  {
317    assert(training_data_.size()==validation_data_.size());
318    for (size_t i=0; i<training_data_.size(); i++) 
319      delete training_data_[i];
320    for (size_t i=0; i<validation_data_.size(); i++) 
321      delete validation_data_[i];
322  }
323
324
325  template<typename T>
326  u_long SubsetGenerator<T>::size(void) const
327  {
328    return sampler_.size();
329  }
330
331
332  template<typename T>
333  const Target& SubsetGenerator<T>::target(void) const
334  {
335    return sampler_.target();
336  }
337
338
339  template<typename T>
340  const T&
341  SubsetGenerator<T>::training_data(size_t i) const 
342  {
343    return *(training_data_[i]);
344  }
345
346
347  template<typename T>
348  const std::vector<size_t>&
349  SubsetGenerator<T>::training_features(typename std::vector<size_t>::size_type i) const
350  {
351    return f_selector_ ? features_[i] : features_[0];
352  }
353
354
355  template<typename T>
356  const std::vector<size_t>&
357  SubsetGenerator<T>::training_index(std::vector<size_t>::size_type i) const
358  {
359    return sampler_.training_index(i);
360  }
361
362
363  template<typename T>
364  const Target&
365  SubsetGenerator<T>::training_target(std::vector<Target>::size_type i) const
366  {
367    return training_target_[i];
368  }
369
370
371  template<typename T>
372  const T&
373  SubsetGenerator<T>::validation_data(size_t i) const
374  {
375    return *(validation_data_[i]);
376  }
377
378
379  template<typename T>
380  const std::vector<size_t>&
381  SubsetGenerator<T>::validation_index(std::vector<size_t>::size_type i) const
382  {
383    return sampler_.validation_index(i);
384  }
385
386
387  template<typename T>
388  const Target&
389  SubsetGenerator<T>::validation_target(std::vector<Target>::size_type i) const
390  {
391    return validation_target_[i];
392  }
393
394}}} // of namespace classifier, yat, and theplu
395
396#endif
397
Note: See TracBrowser for help on using the repository browser.