source: trunk/yat/classifier/EnsembleBuilder.cc @ 876

Last change on this file since 876 was 876, checked in by Jari Häkkinen, 14 years ago

Added missing #include <cassert>

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.8 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2005 Markus Ringnér
5  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
6  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "EnsembleBuilder.h"
27#include "DataLookup2D.h"
28#include "FeatureSelector.h"
29#include "KernelLookup.h"
30#include "MatrixLookup.h"
31#include "MatrixLookupWeighted.h"
32#include "Sampler.h"
33#include "SubsetGenerator.h"
34#include "SupervisedClassifier.h"
35#include "Target.h"
36#include "yat/utility/matrix.h"
37
38#include <cassert>
39
40namespace theplu {
41namespace yat {
42namespace classifier {
43
44  EnsembleBuilder::EnsembleBuilder(const SupervisedClassifier& sc, 
45                                   const Sampler& sampler) 
46    : mother_(sc),subset_(new SubsetGenerator(sampler,sc.data()))
47  {
48  }
49
50  EnsembleBuilder::EnsembleBuilder(const SupervisedClassifier& sc, 
51                                   const Sampler& sampler,
52                                   FeatureSelector& fs) 
53    : mother_(sc),subset_(new SubsetGenerator(sampler,sc.data(),fs))
54  {
55  }
56
57  EnsembleBuilder::~EnsembleBuilder(void) 
58  {
59    for(size_t i=0; i<classifier_.size(); i++)
60      delete classifier_[i];
61    delete subset_;
62  }
63
64  void EnsembleBuilder::build(void) 
65  {
66    for(u_long i=0; i<subset_->size();++i) {
67      SupervisedClassifier* classifier=
68        mother_.make_classifier(subset_->training_data(i), 
69                                subset_->training_target(i));
70      classifier->train();
71      classifier_.push_back(classifier);
72    }   
73  }
74
75
76  const SupervisedClassifier& EnsembleBuilder::classifier(size_t i) const
77  {
78    return *(classifier_[i]);
79  }
80
81
82  u_long EnsembleBuilder::size(void) const
83  {
84    return classifier_.size();
85  }
86
87
88  void EnsembleBuilder::predict
89  (const DataLookup2D& data, 
90   std::vector<std::vector<statistics::Averager> >& result)
91  {
92    result.clear();
93    result.reserve(subset_->target().nof_classes());   
94    for(size_t i=0; i<subset_->target().nof_classes();i++)
95      result.push_back(std::vector<statistics::Averager>(data.columns()));
96   
97    utility::matrix prediction; 
98
99    for(u_long k=0;k<subset_->size();++k) {       
100      const DataLookup2D* sub_data =
101        data.selected(subset_->training_features(k));
102      assert(sub_data);
103      classifier(k).predict(*sub_data,prediction);
104      delete sub_data;
105    }
106
107    for(size_t i=0; i<prediction.rows();i++) 
108      for(size_t j=0; j<prediction.columns();j++) 
109        result[i][j].add(prediction(i,j));   
110  }
111
112 
113  const std::vector<std::vector<statistics::Averager> >& 
114  EnsembleBuilder::validate(void)
115  {
116    validation_result_.clear();
117
118    validation_result_.reserve(subset_->target().nof_classes());   
119    for(size_t i=0; i<subset_->target().nof_classes();i++)
120      validation_result_.push_back(std::vector<statistics::Averager>(subset_->target().size()));
121   
122    utility::matrix prediction; 
123    for(u_long k=0;k<subset_->size();k++) {
124      classifier(k).predict(subset_->validation_data(k),prediction);
125     
126      // map results to indices of samples in training + validation data set
127      for(size_t i=0; i<prediction.rows();i++) 
128        for(size_t j=0; j<prediction.columns();j++) {
129          validation_result_[i][subset_->validation_index(k)[j]].
130            add(prediction(i,j));
131        }           
132    }
133    return validation_result_;
134  }
135
136}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.