diff --git a/src/mlpack/tests/hoeffding_tree_test.cpp b/src/mlpack/tests/hoeffding_tree_test.cpp index de7db90443..3b3a7c902f 100644 --- a/src/mlpack/tests/hoeffding_tree_test.cpp +++ b/src/mlpack/tests/hoeffding_tree_test.cpp @@ -1044,7 +1044,7 @@ TEST_CASE("BatchTrainingTest", "[HoeffdingTreeTest]") // able to have enough samples to build to the same leaves. HoeffdingTree<> batchTree(trainingData, info, trainingLabels, 5, true, 0.99999999); - HoeffdingTree<> streamTree(trainingLabels, info, trainingLabels, 5, false, + HoeffdingTree<> streamTree(trainingData, info, trainingLabels, 5, false, 0.99999999); // Ensure that the performance of the batch tree is better. @@ -1475,3 +1475,50 @@ TEST_CASE("HoeffdingTreeModelSerializationTest", "[HoeffdingTreeTest]") } } } + +TEST_CASE("HoeffdingTreeEmptyConstructorTrainTest", "[HoeffdingTreeTest]") +{ + // Generate data. + arma::mat data(5, 1000, arma::fill::randu); + // Generate labels. + arma::Row labels(1000); + for (size_t i = 0; i < 500; ++i) + labels[i] = 0; + for (size_t i = 500; i < 1000; ++i) + labels[i] = 1; + + // Create an empty tree. + HoeffdingTree<> ht; + + // Just ensure that we can train without throwing an exception. + REQUIRE_NOTHROW(ht.Train(data, labels)); + + // Now, create a categorical dataset and retrain. + data = arma::mat(4, 3000); + labels.set_size(3000); + data::DatasetInfo info(4); // All features are numeric, except the fourth. + info.MapString("0", 3); + for (size_t i = 0; i < 3000; i += 3) + { + data(0, i) = mlpack::math::Random(); + data(1, i) = mlpack::math::Random(); + data(2, i) = mlpack::math::Random(); + data(3, i) = 0.0; + labels[i] = 0; + + data(0, i + 1) = mlpack::math::Random(); + data(1, i + 1) = mlpack::math::Random() - 1.0; + data(2, i + 1) = mlpack::math::Random() + 0.5; + data(3, i + 1) = 0.0; + labels[i + 1] = 2; + + data(0, i + 2) = mlpack::math::Random(); + data(1, i + 2) = mlpack::math::Random() + 1.0; + data(2, i + 2) = mlpack::math::Random() + 0.8; + data(3, i + 2) = 0.0; + labels[i + 2] = 1; + } + + // Ensure we can train without throwing an exception. + REQUIRE_NOTHROW(ht.Train(data, info, labels)); +}