source: trunk/yat/classifier/EnsembleBuilder.h @ 1157

Last change on this file since 1157 was 1157, checked in by Markus Ringnér, 14 years ago

Refs #318

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 5.9 KB
Line 
1#ifndef _theplu_yat_classifier_ensemblebuilder_
2#define _theplu_yat_classifier_ensemblebuilder_
3
4// $Id$
5
6/*
7  Copyright (C) 2005 Markus Ringnér
8  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
9  Copyright (C) 2007, 2008 Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
28
29#include "FeatureSelector.h"
30#include "Sampler.h"
31#include "SubsetGenerator.h"
32#include "yat/statistics/Averager.h"
33#include "yat/utility/Matrix.h"
34
35#include <vector>
36
37namespace theplu {
38namespace yat {
39namespace classifier { 
40
41  ///
42  /// @brief Class for ensembles of supervised classifiers
43  ///
44  template <class Classifier, class Data>
45  class EnsembleBuilder
46  {
47  public:
48    /**
49       \brief Type of classifier that ensemble is built on.
50     */
51    typedef Classifier classifier_type;
52
53    /**
54       Type of container used for storing data e.g. MatrixLookup or KernelLookup
55     */
56    typedef Data data_type;
57
58    ///
59    /// Constructor.
60    ///
61    EnsembleBuilder(const Classifier&, const Data&, const Sampler&);
62
63    ///
64    /// Constructor.
65    ///
66    EnsembleBuilder(const Classifier&, const Data&, const Sampler&, 
67                    FeatureSelector&);
68
69    ///
70    /// Destructor.
71    ///
72    virtual ~EnsembleBuilder(void);
73
74    ///
75    /// Generate ensemble. Function trains each member of the Ensemble.
76    ///
77    void build(void);
78
79    ///
80    /// @return classifier
81    ///
82    const Classifier& classifier(size_t i) const;
83     
84    ///
85    /// @return Number of classifiers in ensemble
86    ///
87    u_long size(void) const;
88
89    ///
90    /// @brief Generate validation data for ensemble
91    ///
92    /// validate()[i][j] return averager for class @a i for sample @a j
93    ///
94    const std::vector<std::vector<statistics::Averager> >& validate(void);
95   
96    /**
97       Predict a dataset using the ensemble.
98       
99       If @a data is a KernelLookup each column should correspond to a
100       test sample and each row should correspond to a training
101       sample. More exactly row \f$ i \f$ in @a data should correspond
102       to the same sample as row/column \f$ i \f$ in the training
103       kernel corresponds to.
104    */
105    void predict(const Data& data, 
106                 std::vector<std::vector<statistics::Averager> > &);
107
108  private:
109    // no copying
110    EnsembleBuilder(const EnsembleBuilder&);
111    const EnsembleBuilder& operator=(const EnsembleBuilder&);
112   
113
114    const Classifier& mother_;
115    SubsetGenerator<Data>* subset_;
116    std::vector<Classifier*> classifier_;
117    std::vector<std::vector<statistics::Averager> > validation_result_;
118
119  };
120 
121
122  // implementation
123
124  template <class C, class D> 
125  EnsembleBuilder<C,D>::EnsembleBuilder(const C& sc, const D& data,
126                                        const Sampler& sampler) 
127    : mother_(sc),subset_(new SubsetGenerator<D>(sampler,data))
128  {
129  }
130
131
132  template <class C, class D> 
133  EnsembleBuilder<C, D>::EnsembleBuilder(const C& sc, const D& data, 
134                                         const Sampler& sampler,
135                                         FeatureSelector& fs) 
136    : mother_(sc),
137      subset_(new SubsetGenerator<D>(sampler,data,fs))
138  {
139  }
140
141
142  template <class C, class D> 
143  EnsembleBuilder<C, D>::~EnsembleBuilder(void) 
144  {
145    for(size_t i=0; i<classifier_.size(); i++)
146      delete classifier_[i];
147    delete subset_;
148  }
149
150
151  template <class C, class D> 
152  void EnsembleBuilder<C, D>::build(void) 
153  {
154    for(u_long i=0; i<subset_->size();++i) {
155      C* classifier = mother_.make_classifier();
156      classifier->train(subset_->training_data(i), 
157                        subset_->training_target(i));
158      classifier_.push_back(classifier);
159    }   
160  }
161
162
163  template <class C, class D> 
164  const C& EnsembleBuilder<C, D>::classifier(size_t i) const
165  {
166    return *(classifier_[i]);
167  }
168
169
170  template <class C, class D> 
171  u_long EnsembleBuilder<C, D>::size(void) const
172  {
173    return classifier_.size();
174  }
175
176
177  template <class C, class D> 
178  void EnsembleBuilder<C, D>::predict
179  (const D& data, std::vector<std::vector<statistics::Averager> >& result)
180  {
181    result.clear();
182    result.reserve(subset_->target().nof_classes());   
183    for(size_t i=0; i<subset_->target().nof_classes();i++)
184      result.push_back(std::vector<statistics::Averager>(data.columns()));
185   
186    utility::Matrix prediction; 
187
188    for(u_long k=0;k<subset_->size();++k) {       
189      const D* sub_data =
190        data.selected(subset_->training_features(k));
191      assert(sub_data);
192      classifier(k).predict(*sub_data,prediction);
193      delete sub_data;
194    }
195
196    for(size_t i=0; i<prediction.rows();i++) 
197      for(size_t j=0; j<prediction.columns();j++) 
198        result[i][j].add(prediction(i,j));   
199  }
200
201 
202  template <class C, class D> 
203  const std::vector<std::vector<statistics::Averager> >& 
204  EnsembleBuilder<C, D>::validate(void)
205  {
206    validation_result_.clear();
207
208    validation_result_.reserve(subset_->target().nof_classes());   
209    for(size_t i=0; i<subset_->target().nof_classes();i++)
210      validation_result_.push_back(std::vector<statistics::Averager>(subset_->target().size()));
211   
212    utility::Matrix prediction; 
213    for(u_long k=0;k<subset_->size();k++) {
214      classifier(k).predict(subset_->validation_data(k),prediction);
215     
216      // map results to indices of samples in training + validation data set
217      for(size_t i=0; i<prediction.rows();i++) 
218        for(size_t j=0; j<prediction.columns();j++) {
219          validation_result_[i][subset_->validation_index(k)[j]].
220            add(prediction(i,j));
221        }           
222    }
223    return validation_result_;
224  }
225
226}}} // of namespace classifier, yat, and theplu
227
228#endif
Note: See TracBrowser for help on using the repository browser.