lpbst.cpp

来自「C++编写的机器学习算法 Lemga is a C++ package whi」· C++ 代码 · 共 73 行

CPP
73
字号
/* $Id: lpbst.cpp 2538 2006-01-08 10:01:17Z ling $ */#include <iostream>#include <fstream>#include <lemga/stump.h>#include <lemga/adaboost.h>#include <lemga/lpboost.h>int main (unsigned int argc, char* argv[]) {    if (argc < 5) {        std::cerr << "Usage: " << argv[0] << " datafile n_train n_test"                  << " #_boost\n";        return -1;    }    /* open data file */    std::ifstream fd(argv[1]);    if (!fd.is_open()) {        std::cerr << argv[0] << ": data file ("                  << argv[1] << ") open error\n";        return -2;    }    /* load training and test data */    lemga::pDataSet trd = lemga::load_data(fd, atoi(argv[2]));    lemga::pDataSet ted = lemga::load_data(fd, atoi(argv[3]));    std::cout << trd->size() << " training samples and "              << ted->size() << " test samples loaded\n";    fd.close();    /* set base model */    lemga::Stump st;    lemga::AdaBoost ada;    lemga::LPBoost lpb;    ada.set_base_model(st);    lpb.set_base_model(st);    /* train the AdaBoost of stumps */    ada.set_max_models(atoi(argv[4]));    ada.initialize();    ada.set_train_data(trd);    ada.train();    /* train the LPBoost of stumps */    lpb.set_max_models(atoi(argv[4]));    lpb.set_C(10);    lpb.initialize();    lpb.set_train_data(trd);    lpb.train();    std::cout << "Minimal margin: AdaBoost "              << ada.min_margin() / ada.margin_norm()              << ", LPBoost "              << lpb.min_margin() / lpb.margin_norm() << '\n';    /* test the performance */    double ada_tre(0), ada_tee(0), lpb_tre(0), lpb_tee(0);    for (UINT i = 0; i < trd->size(); ++i) {        ada_tre += st.c_error(ada(trd->x(i)), trd->y(i));        lpb_tre += st.c_error(lpb(trd->x(i)), trd->y(i));    }    for (UINT i = 0; i < ted->size(); ++i) {        ada_tee += st.c_error(ada(ted->x(i)), ted->y(i));        lpb_tee += st.c_error(lpb(ted->x(i)), ted->y(i));    }    std::cout << "training error: AdaBoost " << 100*ada_tre/trd->size()              << "%,\tLPBoost " << 100*lpb_tre/trd->size() << "%\n";    std::cout << "    test error: AdaBoost " << 100*ada_tee/ted->size()              << "%,\tLPBoost " << 100*lpb_tee/ted->size() << "%\n";    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?