Changeset 1079 for trunk/yat/classifier/EnsembleBuilder.h
 Timestamp:
 Feb 13, 2008, 12:44:28 PM (15 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/classifier/EnsembleBuilder.h
r1072 r1079 7 7 Copyright (C) 2005 Markus Ringnér 8 8 Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson 9 Copyright (C) 2007 Peter Johansson9 Copyright (C) 2007, 2008 Peter Johansson 10 10 11 11 This file is part of the yat library, http://trac.thep.lu.se/yat … … 27 27 */ 28 28 29 #include "DataLookup2D.h" 29 #include "FeatureSelector.h"; 30 #include "Sampler.h" 30 31 #include "SubsetGenerator.h" 31 32 #include "yat/statistics/Averager.h" … … 37 38 namespace classifier { 38 39 39 class FeatureSelector;40 class Sampler;41 class SupervisedClassifier;42 43 40 /// 44 41 /// @brief Class for ensembles of supervised classifiers 45 42 /// 46 43 template <class Classifier, class Data> 47 44 class EnsembleBuilder 48 45 { 49 46 50 47 public: 48 typedef Classifier classifier_type; 49 typedef Data data_type; 50 51 51 /// 52 52 /// Constructor. 53 53 /// 54 EnsembleBuilder(const SupervisedClassifier&, const Sampler&);54 EnsembleBuilder(const Classifier&, const Sampler&); 55 55 56 56 /// 57 57 /// Constructor. 58 58 /// 59 EnsembleBuilder(const SupervisedClassifier&, const Sampler&,59 EnsembleBuilder(const Classifier&, const Sampler&, 60 60 FeatureSelector&); 61 61 … … 73 73 /// @Return classifier 74 74 /// 75 const SupervisedClassifier& classifier(size_t i) const;75 const Classifier& classifier(size_t i) const; 76 76 77 77 /// … … 96 96 kernel corresponds to. 97 97 */ 98 void predict(const Data Lookup2D& data,98 void predict(const Data& data, 99 99 std::vector<std::vector<statistics::Averager> > &); 100 100 101 101 private: 102 // no copying 102 103 EnsembleBuilder(const EnsembleBuilder&); 103 104 const EnsembleBuilder& operator=(const EnsembleBuilder&); 104 105 105 106 106 const SupervisedClassifier& mother_;107 SubsetGenerator<Data Lookup2D>* subset_;108 std::vector< SupervisedClassifier*> classifier_;107 const Classifier& mother_; 108 SubsetGenerator<Data>* subset_; 109 std::vector<Classifier*> classifier_; 109 110 std::vector<std::vector<statistics::Averager> > validation_result_; 110 111 111 112 }; 112 113 114 115 // implementation 116 117 template <class C, class D> 118 EnsembleBuilder<C,D>::EnsembleBuilder(const C& sc, 119 const Sampler& sampler) 120 : mother_(sc),subset_(new SubsetGenerator<D>(sampler,sc.data())) 121 { 122 } 123 124 125 template <class C, class D> 126 EnsembleBuilder<C, D>::EnsembleBuilder(const C& sc, const Sampler& sampler, 127 FeatureSelector& fs) 128 : mother_(sc), 129 subset_(new SubsetGenerator<D>(sampler,sc.data(),fs)) 130 { 131 } 132 133 134 template <class C, class D> 135 EnsembleBuilder<C, D>::~EnsembleBuilder(void) 136 { 137 for(size_t i=0; i<classifier_.size(); i++) 138 delete classifier_[i]; 139 delete subset_; 140 } 141 142 143 template <class C, class D> 144 void EnsembleBuilder<C, D>::build(void) 145 { 146 for(u_long i=0; i<subset_>size();++i) { 147 C* classifier = mother_.make_classifier(subset_>training_data(i), 148 subset_>training_target(i)); 149 classifier>train(); 150 classifier_.push_back(classifier); 151 } 152 } 153 154 155 template <class C, class D> 156 const C& EnsembleBuilder<C, D>::classifier(size_t i) const 157 { 158 return *(classifier_[i]); 159 } 160 161 162 template <class C, class D> 163 u_long EnsembleBuilder<C, D>::size(void) const 164 { 165 return classifier_.size(); 166 } 167 168 169 template <class C, class D> 170 void EnsembleBuilder<C, D>::predict 171 (const D& data, std::vector<std::vector<statistics::Averager> >& result) 172 { 173 result.clear(); 174 result.reserve(subset_>target().nof_classes()); 175 for(size_t i=0; i<subset_>target().nof_classes();i++) 176 result.push_back(std::vector<statistics::Averager>(data.columns())); 177 178 utility::matrix prediction; 179 180 for(u_long k=0;k<subset_>size();++k) { 181 const D* sub_data = 182 data.selected(subset_>training_features(k)); 183 assert(sub_data); 184 classifier(k).predict(*sub_data,prediction); 185 delete sub_data; 186 } 187 188 for(size_t i=0; i<prediction.rows();i++) 189 for(size_t j=0; j<prediction.columns();j++) 190 result[i][j].add(prediction(i,j)); 191 } 192 193 194 template <class C, class D> 195 const std::vector<std::vector<statistics::Averager> >& 196 EnsembleBuilder<C, D>::validate(void) 197 { 198 validation_result_.clear(); 199 200 validation_result_.reserve(subset_>target().nof_classes()); 201 for(size_t i=0; i<subset_>target().nof_classes();i++) 202 validation_result_.push_back(std::vector<statistics::Averager>(subset_>target().size())); 203 204 utility::matrix prediction; 205 for(u_long k=0;k<subset_>size();k++) { 206 classifier(k).predict(subset_>validation_data(k),prediction); 207 208 // map results to indices of samples in training + validation data set 209 for(size_t i=0; i<prediction.rows();i++) 210 for(size_t j=0; j<prediction.columns();j++) { 211 validation_result_[i][subset_>validation_index(k)[j]]. 212 add(prediction(i,j)); 213 } 214 } 215 return validation_result_; 216 } 217 113 218 }}} // of namespace classifier, yat, and theplu 114 219
Note: See TracChangeset
for help on using the changeset viewer.