Changeset 4200 for trunk/yat/classifier/EnsembleBuilder.h
 Timestamp:
 Aug 19, 2022, 9:01:47 AM (14 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/classifier/EnsembleBuilder.h
r2226 r4200 1 #ifndef _theplu_yat_classifier_ensemblebuilder_ 2 #define _theplu_yat_classifier_ensemblebuilder_ 1 #ifndef _theplu_yat_classifier_ensemblebuilder_ 2 #define _theplu_yat_classifier_ensemblebuilder_ 3 3 4 4 // $Id$ … … 39 39 namespace theplu { 40 40 namespace yat { 41 namespace classifier { 41 namespace classifier { 42 42 43 43 /// … … 67 67 /// Constructor. 68 68 /// 69 EnsembleBuilder(const Classifier&, const Data&, const Sampler&, 69 EnsembleBuilder(const Classifier&, const Data&, const Sampler&, 70 70 FeatureSelector&); 71 71 … … 76 76 77 77 /** 78 \brief Generate ensemble. 79 78 \brief Generate ensemble. 79 80 80 Function trains each member of the Ensemble. 81 81 */ … … 86 86 /// 87 87 const Classifier& classifier(size_t i) const; 88 88 89 89 /// 90 90 /// @return Number of classifiers in ensemble. Prior build(void) … … 99 99 /// 100 100 const std::vector<std::vector<statistics::Averager> >& validate(void); 101 101 102 102 /** 103 103 Predict a dataset using the ensemble. 104 104 105 105 If @a data is a KernelLookup each column should correspond to a 106 106 test sample and each row should correspond to a training … … 109 109 kernel corresponds to. 110 110 */ 111 void predict(const Data& data, 111 void predict(const Data& data, 112 112 std::vector<std::vector<statistics::Averager> > &); 113 113 … … 116 116 EnsembleBuilder(const EnsembleBuilder&); 117 117 const EnsembleBuilder& operator=(const EnsembleBuilder&); 118 118 119 119 120 120 const Classifier& mother_; … … 127 127 128 128 }; 129 129 130 130 131 131 // implementation … … 166 166 for(unsigned long i=0; i<subset_>size();++i) { 167 167 Classifier* classifier = mother_.make_classifier(); 168 classifier>train(subset_>training_data(i), 168 classifier>train(subset_>training_data(i), 169 169 subset_>training_target(i)); 170 170 classifier_.push_back(classifier); 171 } 171 } 172 172 } 173 173 } … … 186 186 { 187 187 result = std::vector<std::vector<statistics::Averager> > 188 (subset_>target().nof_classes(), 188 (subset_>target().nof_classes(), 189 189 std::vector<statistics::Averager>(data.columns())); 190 191 utility::Matrix prediction; 192 193 for(unsigned long k=0;k<size();++k) { 190 191 utility::Matrix prediction; 192 193 for(unsigned long k=0;k<size();++k) { 194 194 Data sub_data = test_data(data, k); 195 195 classifier(k).predict(sub_data,prediction); 196 for(size_t i=0; i<prediction.rows();i++) 197 for(size_t j=0; j<prediction.columns();j++) 198 result[i][j].add(prediction(i,j)); 199 } 200 } 201 202 196 for(size_t i=0; i<prediction.rows();i++) 197 for(size_t j=0; j<prediction.columns();j++) 198 result[i][j].add(prediction(i,j)); 199 } 200 } 201 202 203 203 template <class Classifier, class Data> 204 204 unsigned long EnsembleBuilder<Classifier, Data>::size(void) const … … 215 215 return MatrixLookup(data, subset_>training_features(k), true); 216 216 } 217 218 219 template <class Classifier, class Data> 220 MatrixLookupWeighted 217 218 219 template <class Classifier, class Data> 220 MatrixLookupWeighted 221 221 EnsembleBuilder<Classifier, Data>::test_data(const MatrixLookupWeighted& data, 222 222 size_t k) 223 223 { 224 return MatrixLookupWeighted(data, subset_>training_features(k), 224 return MatrixLookupWeighted(data, subset_>training_features(k), 225 225 utility::Index(data.columns())); 226 226 } 227 227 228 228 229 229 template <class Classifier, class Data> … … 246 246 if (kernel.data().rows()==subset_>training_features(k).size()) 247 247 return KernelLookup(kernel, subset_>training_index(k), true); 248 248 249 249 // feature selection 250 250 MatrixLookup ml = test_data(kernel.data(),k); 251 251 return subset_>training_data(k).test_kernel(ml); 252 252 } 253 254 255 template <class Classifier, class Data> 256 const std::vector<std::vector<statistics::Averager> >& 253 254 255 template <class Classifier, class Data> 256 const std::vector<std::vector<statistics::Averager> >& 257 257 EnsembleBuilder<Classifier, Data>::validate(void) 258 258 { … … 262 262 263 263 validation_result_ = std::vector<std::vector<statistics::Averager> > 264 (subset_>target().nof_classes(), 264 (subset_>target().nof_classes(), 265 265 std::vector<statistics::Averager>(subset_>target().size())); 266 266 267 utility::Matrix prediction; 267 utility::Matrix prediction; 268 268 for(unsigned long k=0;k<size();k++) { 269 269 classifier(k).predict(subset_>validation_data(k),prediction); 270 270 271 271 // map results to indices of samples in training + validation data set 272 for(size_t i=0; i<prediction.rows();i++) 272 for(size_t i=0; i<prediction.rows();i++) 273 273 for(size_t j=0; j<prediction.columns();j++) { 274 274 validation_result_[i][subset_>validation_index(k)[j]]. 275 275 add(prediction(i,j)); 276 } 276 } 277 277 } 278 278 return validation_result_;
Note: See TracChangeset
for help on using the changeset viewer.