src/mlpack/tests/main_tests/decision_tree_test.cpp
BOOST_AUTO_TEST_CASE(DecisionModelCategoricalReuseTest)
{
  arma::mat inputData;
  DatasetInfo info;
  if (!data::Load("braziltourism.arff", inputData, info))
    BOOST_FAIL("Cannot load train dataset braziltourism.arff!");

  arma::Row<size_t> labels;
  if (!data::Load("braziltourism_labels.txt", labels))
    BOOST_FAIL("Cannot load labels for braziltourism_labels.txt");

  // Initialize an all-ones weight matrix.
  arma::mat weights(1, labels.n_cols, arma::fill::ones);

  arma::mat testData;
  if (!data::Load("braziltourism_test.arff", testData, info))
    BOOST_FAIL("Cannot load test dataset braziltourism_test.arff!");

  size_t testSize = testData.n_cols;

  // Input training data.
  SetInputParam("training", std::make_tuple(info, inputData));
  SetInputParam("labels", std::move(labels));
  SetInputParam("weights", std::move(weights));

  // Input test data.
  SetInputParam("test", std::make_tuple(info, testData));

  mlpackMain();

  arma::Row<size_t> predictions;
  arma::mat probabilities;
  predictions = std::move(CLI::GetParam<arma::Row<size_t>>("predictions"));
  probabilities = std::move(CLI::GetParam<arma::mat>("probabilities"));

  // Reset passed parameters.
  CLI::GetSingleton().Parameters()["training"].wasPassed = false;
  CLI::GetSingleton().Parameters()["labels"].wasPassed = false;
  CLI::GetSingleton().Parameters()["weights"].wasPassed = false;
  CLI::GetSingleton().Parameters()["test"].wasPassed = false;

  // Input trained model.
  SetInputParam("test", std::make_tuple(info, testData));
  SetInputParam("input_model",
      std::move(CLI::GetParam<DecisionTreeModel*>("output_model")));

  mlpackMain();

  // Check that number of output points are equal to number of input points.
  BOOST_REQUIRE_EQUAL(CLI::GetParam<arma::Row<size_t>>("predictions").n_cols,
      testSize);
  BOOST_REQUIRE_EQUAL(CLI::GetParam<arma::mat>("probabilities").n_cols,
      testSize);

  // Check number of output rows equals number of classes in case of
  // probabilities and 1 for predicitions.
  BOOST_REQUIRE_EQUAL(
      CLI::GetParam<arma::Row<size_t>>("predictions").n_rows, 1);
  BOOST_REQUIRE_EQUAL(CLI::GetParam<arma::mat>("probabilities").n_rows, 6);

  // Check that initial predictions and predictions using saved model are same.
  CheckMatrices(predictions, CLI::GetParam<arma::Row<size_t>>("predictions"));
  CheckMatrices(probabilities, CLI::GetParam<arma::mat>("probabilities"));
}
/**
 * Ensure that saved model trained on categorical dataset can be used again.
 */
Ensure that saved model trained on categorical dataset can be used again.
Ensure that saved model trained on categorical dataset can be used again .
BOOST_AUTO_TEST_CASE(DecisionModelCategoricalReuseTest)
{
  arma::mat inputData;
  DatasetInfo info;
  if (!data::Load("braziltourism.arff", inputData, info))
    BOOST_FAIL("Cannot load train dataset braziltourism.arff!");
  arma::Row<size_t> labels;
  if (!data::Load("braziltourism_labels.txt", labels))
    BOOST_FAIL("Cannot load labels for braziltourism_labels.txt");
  arma::mat weights(1, labels.n_cols, arma::fill::ones);
  arma::mat testData;
  if (!data::Load("braziltourism_test.arff", testData, info))
    BOOST_FAIL("Cannot load test dataset braziltourism_test.arff!");
  size_t testSize = testData.n_cols;
  SetInputParam("training", std::make_tuple(info, inputData));
  SetInputParam("labels", std::move(labels));
  SetInputParam("weights", std::move(weights));
  SetInputParam("test", std::make_tuple(info, testData));
  mlpackMain();
  arma::Row<size_t> predictions;
  arma::mat probabilities;
  predictions = std::move(CLI::GetParam<arma::Row<size_t>>("predictions"));
  probabilities = std::move(CLI::GetParam<arma::mat>("probabilities"));
  CLI::GetSingleton().Parameters()["training"].wasPassed = false;
  CLI::GetSingleton().Parameters()["labels"].wasPassed = false;
  CLI::GetSingleton().Parameters()["weights"].wasPassed = false;
  CLI::GetSingleton().Parameters()["test"].wasPassed = false;
  SetInputParam("test", std::make_tuple(info, testData));
  SetInputParam("input_model",
      std::move(CLI::GetParam<DecisionTreeModel*>("output_model")));
  mlpackMain();
  BOOST_REQUIRE_EQUAL(CLI::GetParam<arma::Row<size_t>>("predictions").n_cols,
      testSize);
  BOOST_REQUIRE_EQUAL(CLI::GetParam<arma::mat>("probabilities").n_cols,
      testSize);
  BOOST_REQUIRE_EQUAL(
      CLI::GetParam<arma::Row<size_t>>("predictions").n_rows, 1);
  BOOST_REQUIRE_EQUAL(CLI::GetParam<arma::mat>("probabilities").n_rows, 6);
  CheckMatrices(predictions, CLI::GetParam<arma::Row<size_t>>("predictions"));
  CheckMatrices(probabilities, CLI::GetParam<arma::mat>("probabilities"));
}
BOOST_AUTO_TEST_CASE ( DecisionModelCategoricalReuseTest ) { arma :: mat inputData ; DatasetInfo info ; if ( ! data :: Load ( " " , inputData , info ) ) BOOST_FAIL ( " " ) ; arma :: Row < size_t > labels ; if ( ! data :: Load ( " " , labels ) ) BOOST_FAIL ( " " ) ; arma :: mat weights ( 1 , labels . n_cols , arma :: fill :: ones ) ; arma :: mat testData ; if ( ! data :: Load ( " " , testData , info ) ) BOOST_FAIL ( " " ) ; size_t testSize = testData . n_cols ; SetInputParam ( " " , std :: make_tuple ( info , inputData ) ) ; SetInputParam ( " " , std :: move ( labels ) ) ; SetInputParam ( " " , std :: move ( weights ) ) ; SetInputParam ( " " , std :: make_tuple ( info , testData ) ) ; mlpackMain ( ) ; arma :: Row < size_t > predictions ; arma :: mat probabilities ; predictions = std :: move ( CLI :: GetParam < arma :: Row < size_t > > ( " " ) ) ; probabilities = std :: move ( CLI :: GetParam < arma :: mat > ( " " ) ) ; CLI :: GetSingleton ( ) . Parameters ( ) [ " " ] . wasPassed = false ; CLI :: GetSingleton ( ) . Parameters ( ) [ " " ] . wasPassed = false ; CLI :: GetSingleton ( ) . Parameters ( ) [ " " ] . wasPassed = false ; CLI :: GetSingleton ( ) . Parameters ( ) [ " " ] . wasPassed = false ; SetInputParam ( " " , std :: make_tuple ( info , testData ) ) ; SetInputParam ( " " , std :: move ( CLI :: GetParam < DecisionTreeModel * > ( " " ) ) ) ; mlpackMain ( ) ; BOOST_REQUIRE_EQUAL ( CLI :: GetParam < arma :: Row < size_t > > ( " " ) . n_cols , testSize ) ; BOOST_REQUIRE_EQUAL ( CLI :: GetParam < arma :: mat > ( " " ) . n_cols , testSize ) ; BOOST_REQUIRE_EQUAL ( CLI :: GetParam < arma :: Row < size_t > > ( " " ) . n_rows , 1 ) ; BOOST_REQUIRE_EQUAL ( CLI :: GetParam < arma :: mat > ( " " ) . n_rows , 6 ) ; CheckMatrices ( predictions , CLI :: GetParam < arma :: Row < size_t > > ( " " ) ) ; CheckMatrices ( probabilities , CLI :: GetParam < arma :: mat > ( " " ) ) ; }
Ensure that saved model trained on categorical dataset can be used again.
Ensure that saved model trained on categorical dataset can be used again .
// Initialize an all-ones weight matrix. // Input training data. // Input test data. // Reset passed parameters. // Input trained model. // Check that number of output points are equal to number of input points. // Check number of output rows equals number of classes in case of // probabilities and 1 for predicitions. // Check that initial predictions and predictions using saved model are same.