1 #ifndef _theplu_yat_classifier_ensemblebuilder_
2 #define _theplu_yat_classifier_ensemblebuilder_
30 #include "FeatureSelector.h"
32 #include "SubsetGenerator.h"
33 #include "yat/statistics/Averager.h"
34 #include "yat/utility/Matrix.h"
35 #include "yat/utility/yat_assert.h"
41 namespace classifier {
46 template <
class Classifier,
class Data>
93 unsigned long size(
void)
const;
100 const std::vector<std::vector<statistics::Averager> >&
validate(
void);
112 std::vector<std::vector<statistics::Averager> > &);
120 const Classifier& mother_;
122 std::vector<Classifier*> classifier_;
126 std::vector<std::vector<statistics::Averager> > validation_result_;
133 template <
class Classifier,
class Data>
142 template <
class Classifier,
class Data>
153 template <
class Classifier,
class Data>
156 for(
size_t i=0; i<classifier_.size(); i++)
157 delete classifier_[i];
162 template <
class Classifier,
class Data>
165 if (classifier_.empty()){
166 for(
unsigned long i=0; i<subset_->size();++i) {
167 Classifier* classifier = mother_.make_classifier();
168 classifier->train(subset_->training_data(i),
169 subset_->training_target(i));
170 classifier_.push_back(classifier);
176 template <
class Classifier,
class Data>
179 return *(classifier_[i]);
183 template <
class Classifier,
class Data>
185 (
const Data& data, std::vector<std::vector<statistics::Averager> >& result)
187 result = std::vector<std::vector<statistics::Averager> >
188 (subset_->target().nof_classes(),
189 std::vector<statistics::Averager>(data.columns()));
193 for(
unsigned long k=0;k<size();++k) {
194 Data sub_data = test_data(data, k);
195 classifier(k).predict(sub_data,prediction);
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));
203 template <
class Classifier,
class Data>
206 return classifier_.size();
210 template <
class Classifier,
class Data>
215 return MatrixLookup(data, subset_->training_features(k),
true);
219 template <
class Classifier,
class Data>
221 EnsembleBuilder<Classifier, Data>::test_data(
const MatrixLookupWeighted& data,
224 return MatrixLookupWeighted(data, subset_->training_features(k),
229 template <
class Classifier,
class Data>
231 EnsembleBuilder<Classifier, Data>::test_data(
const KernelLookup& kernel,
235 if (kernel.weighted()){
237 if (kernel.data_weighted().rows()==subset_->training_features(k).size())
238 return KernelLookup(kernel, subset_->training_index(k),
true);
239 MatrixLookupWeighted mlw = test_data(kernel.data_weighted(), k);
240 return subset_->training_data(k).test_kernel(mlw);
246 if (kernel.data().rows()==subset_->training_features(k).size())
247 return KernelLookup(kernel, subset_->training_index(k),
true);
250 MatrixLookup ml = test_data(kernel.data(),k);
251 return subset_->training_data(k).test_kernel(ml);
255 template <
class Classifier,
class Data>
256 const std::vector<std::vector<statistics::Averager> >&
260 if (!validation_result_.empty())
261 return validation_result_;
263 validation_result_ = std::vector<std::vector<statistics::Averager> >
264 (subset_->target().nof_classes(),
265 std::vector<statistics::Averager>(subset_->target().size()));
268 for(
unsigned long k=0;k<size();k++) {
269 classifier(k).predict(subset_->validation_data(k),prediction);
272 for(
size_t i=0; i<prediction.
rows();i++)
273 for(
size_t j=0; j<prediction.
columns();j++) {
274 validation_result_[i][subset_->validation_index(k)[j]].
275 add(prediction(i,j));
278 return validation_result_;
General view into utility::Matrix.
Definition: MatrixLookup.h:70
Classifier classifier_type
Type of classifier that ensemble is built on.
Definition: EnsembleBuilder.h:53
unsigned long size(void) const
Definition: EnsembleBuilder.h:204
Interface class for FeatureSelection.
Definition: FeatureSelector.h:39
void build(void)
Generate ensemble.
Definition: EnsembleBuilder.h:163
Class for ensembles of supervised classifiers.
Definition: EnsembleBuilder.h:47
Class splitting Data into training and validation set.
Definition: SubsetGenerator.h:57
Class for storing indices of, e.g., a MatrixLookup.
Definition: Index.h:41
General view into utility::MatrixWeighted.
Definition: MatrixLookupWeighted.h:63
const Classifier & classifier(size_t i) const
Definition: EnsembleBuilder.h:177
Lookup into Kernel.
Definition: KernelLookup.h:67
EnsembleBuilder(const Classifier &, const Data &, const Sampler &)
Definition: EnsembleBuilder.h:134
void predict(const Data &data, std::vector< std::vector< statistics::Averager > > &)
Definition: EnsembleBuilder.h:185
Interface class for dividing samples into training and validation.
Definition: Sampler.h:39
Interface to GSL matrix.
Definition: Matrix.h:63
const std::vector< std::vector< statistics::Averager > > & validate(void)
Generate validation data for ensemble.
Definition: EnsembleBuilder.h:257
Data data_type
Definition: EnsembleBuilder.h:59
virtual ~EnsembleBuilder(void)
Definition: EnsembleBuilder.h:154
size_t columns(void) const