Add tests for using HoeffdingTrees with an empty constructor.

This commit is contained in:
Ryan Curtin
2021-06-01 19:15:45 -04:00
parent aaa1a47259
commit 6461067aac
+48 -1
View File
@@ -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<size_t> 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<double>("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));
}