src/mlpack/tests/adaboost_test.cpp
/**
 * This test case runs the AdaBoost.mh algorithm on a non-linearly separable
 * dataset.  It checks if the error returned by running a single instance of the
 * weak learner is worse than running the boosted weak learner using adaboost.
 * This is for the weak learner: decision stumps.
 */
This test case runs the AdaBoost.mh algorithm on a non-linearly separable
dataset.  It checks if the error returned by running a single instance of the
weak learner is worse than running the boosted weak learner using adaboost.
This is for the weak learner: decision stumps.
This test case runs the AdaBoost . mh algorithm on a non - linearly separable dataset . It checks if the error returned by running a single instance of the weak learner is worse than running the boosted weak learner using adaboost . This is for the weak learner : decision stumps .
BOOST_AUTO_TEST_CASE(WeakLearnerErrorIris_DS)
{
  arma::mat inputData;
  if (!data::Load("iris.csv", inputData))
    BOOST_FAIL("Cannot load test dataset iris.csv!");
  arma::Mat<size_t> labels;
  if (!data::Load("iris_labels.txt", labels))
    BOOST_FAIL("Cannot load labels for iris_labels.txt");
  const size_t numClasses = 3;
  const size_t inpBucketSize = 6;
  arma::Row<size_t> dsPrediction(labels.n_cols);
  DecisionStump<> ds(inputData, labels.row(0), numClasses, inpBucketSize);
  ds.Classify(inputData, dsPrediction);
  size_t countWeakLearnerError = 0;
  for (size_t i = 0; i < labels.n_cols; i++)
    if (labels(i) != dsPrediction(i))
      countWeakLearnerError++;
  double weakLearnerErrorRate = (double) countWeakLearnerError / labels.n_cols;
  size_t iterations = 50;
  double tolerance = 1e-10;
  AdaBoost<DecisionStump<>> a(inputData, labels.row(0), numClasses, ds,
      iterations, tolerance);
  arma::Row<size_t> predictedLabels;
  a.Classify(inputData, predictedLabels);
  size_t countError = 0;
  for (size_t i = 0; i < labels.n_cols; i++)
    if (labels(i) != predictedLabels(i))
      countError++;
  double error = (double) countError / labels.n_cols;
  BOOST_REQUIRE_LE(error, weakLearnerErrorRate);
}
BOOST_AUTO_TEST_CASE ( WeakLearnerErrorIris_DS ) { arma :: mat inputData ; if ( ! data :: Load ( " " , inputData ) ) BOOST_FAIL ( " " ) ; arma :: Mat < size_t > labels ; if ( ! data :: Load ( " " , labels ) ) BOOST_FAIL ( " " ) ; const size_t numClasses = 3 ; const size_t inpBucketSize = 6 ; arma :: Row < size_t > dsPrediction ( labels . n_cols ) ; DecisionStump < > ds ( inputData , labels . row ( 0 ) , numClasses , inpBucketSize ) ; ds . Classify ( inputData , dsPrediction ) ; size_t countWeakLearnerError = 0 ; for ( size_t i = 0 ; i < labels . n_cols ; i ++ ) if ( labels ( i ) != dsPrediction ( i ) ) countWeakLearnerError ++ ; double weakLearnerErrorRate = ( double ) countWeakLearnerError / labels . n_cols ; size_t iterations = 50 ; double tolerance = 1e-10 ; AdaBoost < DecisionStump < > > a ( inputData , labels . row ( 0 ) , numClasses , ds , iterations , tolerance ) ; arma :: Row < size_t > predictedLabels ; a . Classify ( inputData , predictedLabels ) ; size_t countError = 0 ; for ( size_t i = 0 ; i < labels . n_cols ; i ++ ) if ( labels ( i ) != predictedLabels ( i ) ) countError ++ ; double error = ( double ) countError / labels . n_cols ; BOOST_REQUIRE_LE ( error , weakLearnerErrorRate ) ; }
This test case runs the AdaBoost.mh algorithm on a non-linearly separable
 dataset.
This test case runs the AdaBoost . mh algorithm on a non - linearly separable dataset .
// no need to map the labels here // Define your own weak learner, decision stumps in this case. // Define parameters for AdaBoost.