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

Last change on this file since 736 was 736, checked in by Peter, 15 years ago

Fixes #176

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