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 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace 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 | void EnsembleBuilder::predict |
---|
60 | (const DataLookup2D& data, |
---|
61 | std::vector<std::vector<statistics::Averager> >& result) |
---|
62 | { |
---|
63 | result.clear(); |
---|
64 | result.reserve(subset_.target().nof_classes()); |
---|
65 | for(size_t i=0; i<subset_.target().nof_classes();i++) |
---|
66 | result.push_back(std::vector<statistics::Averager>(data.columns())); |
---|
67 | |
---|
68 | utility::matrix prediction; |
---|
69 | try { |
---|
70 | const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data); |
---|
71 | for(u_long k=0;k<subset_.size();k++) { |
---|
72 | KernelLookup kernel_peter(kernel,subset_.training_index(k),true); |
---|
73 | classifier(k).predict(kernel_peter,prediction); |
---|
74 | |
---|
75 | for(size_t i=0; i<prediction.rows();i++) |
---|
76 | for(size_t j=0; j<prediction.columns();j++) |
---|
77 | result[i][j].add(prediction(i,j)); |
---|
78 | } |
---|
79 | } |
---|
80 | catch (std::bad_cast) { |
---|
81 | for(u_long k=0;k<subset_.size();k++) { |
---|
82 | classifier(k).predict(data,prediction); |
---|
83 | for(size_t i=0; i<prediction.rows();i++) |
---|
84 | for(size_t j=0; j<prediction.columns();j++) |
---|
85 | result[i][j].add(prediction(i,j)); |
---|
86 | |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | const std::vector<std::vector<statistics::Averager> >& |
---|
94 | EnsembleBuilder::validate(void) |
---|
95 | { |
---|
96 | validation_result_.clear(); |
---|
97 | |
---|
98 | validation_result_.reserve(subset_.target().nof_classes()); |
---|
99 | for(size_t i=0; i<subset_.target().nof_classes();i++) |
---|
100 | validation_result_.push_back(std::vector<statistics::Averager>(subset_.target().size())); |
---|
101 | |
---|
102 | utility::matrix prediction; |
---|
103 | for(u_long k=0;k<subset_.size();k++) { |
---|
104 | classifier(k).predict(subset_.validation_data(k),prediction); |
---|
105 | |
---|
106 | for(size_t i=0; i<prediction.rows();i++) |
---|
107 | for(size_t j=0; j<prediction.columns();j++) { |
---|
108 | validation_result_[i][subset_.validation_index(k)[j]]. |
---|
109 | add(prediction(i,j)); |
---|
110 | } |
---|
111 | } |
---|
112 | return validation_result_; |
---|
113 | } |
---|
114 | |
---|
115 | }}} // of namespace classifier, yat, and theplu |
---|