bagging.cpp

Go to the documentation of this file.
00001 
00005 #include <assert.h>
00006 #include "bagging.h"
00007 
00008 REGISTER_CREATOR(lemga::Bagging);
00009 
00010 namespace lemga {
00011 
00012 Bagging::Bagging (UINT max) : Aggregating() {
00013     set_max_models(max);
00014 }
00015 
00016 Output Bagging::operator() (const Input& x) const {
00017     assert(n_in_agg > 0 && n_in_agg <= size());
00018 
00019     Output y(_n_out, 0);
00020 
00021     for (UINT i = 0; i < n_in_agg; ++i) {
00022         assert(lm[i] != 0);
00023         Output yi = (*lm[i])(x);
00024         for (UINT j = 0; j < _n_out; ++j)
00025             y[j] += (yi[j] > 0)? 1 : -1;
00026     }
00027     for (UINT j = 0; j < _n_out; ++j)
00028         y[j] /= n_in_agg;
00029 
00030     return y;
00031 }
00032 
00033 REAL Bagging::train () {
00034     assert(n_in_agg == 0 && empty());
00035 
00036     REAL err = 0;
00037     for (UINT i = 0; i < max_n_model; ++i) {
00038 #if VERBOSE_OUTPUT
00039         std::cout << "=== " << id() << " #" << i+1 << " / "
00040                   << max_n_model << " ===\n";
00041 #endif
00042         assert(lm_base != 0);
00043         LearnModel *p = lm_base->clone();
00044         p->set_train_data(ptd->random_sample(*ptw, n_samples));
00045         p->initialize();
00046         err += p->train();
00047         lm.push_back(p);
00048     }
00049     n_in_agg = max_n_model;
00050 
00051     return (err / n_in_agg);
00052 }
00053 
00054 } // namespace lemga

Generated on Mon Jan 9 23:43:23 2006 for LEMGA by  doxygen 1.4.6