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

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

Fixes #170. Almost all inlines removed, some classes have no cc file.

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