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

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

Closes #129

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 3.6 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 "KernelLookup.h"
27#include "Sampler.h"
28#include "SubsetGenerator.h"
29#include "SupervisedClassifier.h"
30#include "Target.h"
31#include "yat/utility/matrix.h"
32
33namespace theplu {
34namespace yat {
35namespace classifier {
36
37  EnsembleBuilder::EnsembleBuilder(const SupervisedClassifier& sc, 
38                                   const Sampler& sampler) 
39    : mother_(sc),subset_(new SubsetGenerator(sampler,sc.data()))
40  {
41  }
42
43  EnsembleBuilder::~EnsembleBuilder(void) 
44  {
45    for(size_t i=0; i<classifier_.size(); i++)
46      delete classifier_[i];
47    delete subset_;
48  }
49
50  void EnsembleBuilder::build(void) 
51  {
52    for(u_long i=0; i<subset_->size();++i) {
53      SupervisedClassifier* classifier=
54        mother_.make_classifier(subset_->training_data(i), 
55                                subset_->training_target(i));
56      classifier->train();
57      classifier_.push_back(classifier);
58    }   
59  }
60
61
62  const SupervisedClassifier& EnsembleBuilder::classifier(size_t i) const
63  {
64    return *(classifier_[i]);
65  }
66
67
68  u_long EnsembleBuilder::size(void) const
69  {
70    return classifier_.size();
71  }
72
73
74  void  EnsembleBuilder::predict
75  (const DataLookup2D& data, 
76   std::vector<std::vector<statistics::Averager> >& result)
77  {
78    result.clear();
79    result.reserve(subset_->target().nof_classes());   
80    for(size_t i=0; i<subset_->target().nof_classes();i++)
81      result.push_back(std::vector<statistics::Averager>(data.columns()));
82   
83    utility::matrix prediction; 
84    try {
85      const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data);
86      for(u_long k=0;k<subset_->size();k++) {
87        KernelLookup kernel_peter(kernel,subset_->training_index(k),true);
88        classifier(k).predict(kernel_peter,prediction);
89
90        for(size_t i=0; i<prediction.rows();i++) 
91          for(size_t j=0; j<prediction.columns();j++) 
92            result[i][j].add(prediction(i,j));
93      }
94    }
95    catch (std::bad_cast) {
96      for(u_long k=0;k<subset_->size();k++) {
97        classifier(k).predict(data,prediction);
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    }
104  }
105
106
107  const std::vector<std::vector<statistics::Averager> >& 
108  EnsembleBuilder::validate(void)
109  {
110    validation_result_.clear();
111
112    validation_result_.reserve(subset_->target().nof_classes());   
113    for(size_t i=0; i<subset_->target().nof_classes();i++)
114      validation_result_.push_back(std::vector<statistics::Averager>(subset_->target().size()));
115   
116    utility::matrix prediction; 
117    for(u_long k=0;k<subset_->size();k++) {
118      classifier(k).predict(subset_->validation_data(k),prediction);
119
120      for(size_t i=0; i<prediction.rows();i++) 
121        for(size_t j=0; j<prediction.columns();j++) {
122          validation_result_[i][subset_->validation_index(k)[j]].
123            add(prediction(i,j));
124        }           
125    }
126    return validation_result_;
127  }
128
129}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.