Merge pull request #6 from mlpack/master

Updated with mlpack master
This commit is contained in:
Aakash kaushik
2020-10-01 01:32:26 +05:30
committed by GitHub
9 changed files with 338 additions and 352 deletions
+3 -1
View File
@@ -132,7 +132,9 @@ Copyright:
Copyright 2020, Lakshya Ojha <ojhalakshya@gmail.com>
Copyright 2020, Bisakh Mondal <bisakhmondal00@gmail.com>
Copyright 2020, Benson Muite <benson_muite@emailplus.org>
Copyright 2020, Sarthak Bhardwaj <7sarthakbhardwaj@gmail.com>
Copyright 2020, Sarthak Bhardwaj <7sarthakbhardwaj@gmail.com>
Copyright 2020, Aakash Kaushik <kaushikaakash7539@gmail.com>
Copyright 2020, Anush Kini <anushkini@gmail.com>
License: BSD-3-clause
All rights reserved.
+7 -8
View File
@@ -1,6 +1,5 @@
# mlpack test executable.
add_executable(mlpack_test
arma_extend_test.cpp
async_learning_test.cpp
augmented_rnns_tasks_test.cpp
callback_test.cpp
@@ -8,7 +7,6 @@ add_executable(mlpack_test
cli_binding_test.cpp
io_test.cpp
cosine_tree_test.cpp
dbscan_test.cpp
dcgan_test.cpp
det_test.cpp
distribution_test.cpp
@@ -47,14 +45,12 @@ add_executable(mlpack_test
nmf_test.cpp
nystroem_method_test.cpp
octree_test.cpp
pca_test.cpp
perceptron_test.cpp
prefixedoutstream_test.cpp
python_binding_test.cpp
q_learning_test.cpp
qdafn_test.cpp
radical_test.cpp
random_forest_test.cpp
random_test.cpp
range_search_test.cpp
rectangle_tree_test.cpp
@@ -79,7 +75,6 @@ add_executable(mlpack_test
vantage_point_tree_test.cpp
wgan_test.cpp
main_tests/cf_test.cpp
main_tests/dbscan_test.cpp
main_tests/det_test.cpp
main_tests/emst_test.cpp
main_tests/fastmks_test.cpp
@@ -102,10 +97,8 @@ add_executable(mlpack_test
main_tests/mean_shift_test.cpp
main_tests/nbc_test.cpp
main_tests/nmf_test.cpp
main_tests/pca_test.cpp
main_tests/perceptron_test.cpp
main_tests/radical_test.cpp
main_tests/random_forest_test.cpp
main_tests/range_search_test.cpp
main_tests/test_helper.hpp
)
@@ -121,6 +114,7 @@ add_executable(mlpack_catch_test
ann_test_tools.hpp
ann_visitor_test.cpp
armadillo_svd_test.cpp
arma_extend_test.cpp
bayesian_linear_regression_test.cpp
bias_svd_test.cpp
binarize_test.cpp
@@ -128,6 +122,7 @@ add_executable(mlpack_catch_test
convolutional_network_test.cpp
convolution_test.cpp
cv_test.cpp
dbscan_test.cpp
decision_stump_test.cpp
decision_tree_test.cpp
feedforward_network_test.cpp
@@ -144,7 +139,9 @@ add_executable(mlpack_catch_test
main.cpp
nca_test.cpp
one_hot_encoding_test.cpp
pca_test.cpp
quic_svd_test.cpp
random_forest_test.cpp
randomized_svd_test.cpp
rbm_network_test.cpp
recurrent_network_test.cpp
@@ -163,6 +160,7 @@ add_executable(mlpack_catch_test
main_tests/adaboost_test.cpp
main_tests/approx_kfn_test.cpp
main_tests/bayesian_linear_regression_test.cpp
main_tests/dbscan_test.cpp
main_tests/decision_stump_test.cpp
main_tests/decision_tree_test.cpp
main_tests/image_converter_test.cpp
@@ -172,11 +170,13 @@ add_executable(mlpack_catch_test
main_tests/knn_test.cpp
main_tests/linear_regression_test.cpp
main_tests/nca_test.cpp
main_tests/pca_test.cpp
main_tests/preprocess_binarize_test.cpp
main_tests/preprocess_imputer_test.cpp
main_tests/preprocess_one_hot_encode_test.cpp
main_tests/preprocess_scale_test.cpp
main_tests/preprocess_split_test.cpp
main_tests/random_forest_test.cpp
main_tests/softmax_regression_test.cpp
main_tests/sparse_coding_test.cpp
main_tests/test_helper.hpp
@@ -273,4 +273,3 @@ add_test(NAME "catch_test" COMMAND mlpack_catch_test WORKING_DIRECTORY ${CMAKE_B
# Use RUN_SERIAL for long running parallel tests
set_tests_properties(${parallel_tests} PROPERTIES RUN_SERIAL TRUE)
+43 -46
View File
@@ -11,18 +11,17 @@
*/
#include <mlpack/core.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "test_catch_tools.hpp"
#include "catch.hpp"
using namespace mlpack;
using namespace arma;
BOOST_AUTO_TEST_SUITE(ArmaExtendTest);
/**
* Test const_row_col_iterator for basic functionality.
*/
BOOST_AUTO_TEST_CASE(ConstRowColIteratorTest)
TEST_CASE("ConstRowColIteratorTest", "[ArmaExtendTest]")
{
mat X;
X.zeros(5, 5);
@@ -39,15 +38,15 @@ BOOST_AUTO_TEST_CASE(ConstRowColIteratorTest)
for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
{
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
++count;
}
BOOST_REQUIRE_EQUAL(count, 25);
REQUIRE(count == 25);
it = X.end_row_col();
do
{
@@ -55,20 +54,20 @@ BOOST_AUTO_TEST_CASE(ConstRowColIteratorTest)
--count;
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
} while (it != X.begin_row_col());
BOOST_REQUIRE_EQUAL(count, 0);
REQUIRE(count == 0);
}
/**
* Test row_col_iterator for basic functionality.
*/
BOOST_AUTO_TEST_CASE(RowColIteratorTest)
TEST_CASE("RowColIteratorTest", "[ArmaExtendTest]")
{
mat X;
X.zeros(5, 5);
@@ -85,15 +84,15 @@ BOOST_AUTO_TEST_CASE(RowColIteratorTest)
for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
{
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
++count;
}
BOOST_REQUIRE_EQUAL(count, 25);
REQUIRE(count == 25);
it = X.end_row_col();
do
{
@@ -101,20 +100,20 @@ BOOST_AUTO_TEST_CASE(RowColIteratorTest)
--count;
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
} while (it != X.begin_row_col());
BOOST_REQUIRE_EQUAL(count, 0);
REQUIRE(count == 0);
}
/**
* Operator-- test for mat::row_col_iterator and mat::const_row_col_iterator
*/
BOOST_AUTO_TEST_CASE(MatRowColIteratorDecrementOperatorTest)
TEST_CASE("MatRowColIteratorDecrementOperatorTest", "[ArmaExtendTest]")
{
mat test = ones<mat>(5, 5);
@@ -124,14 +123,14 @@ BOOST_AUTO_TEST_CASE(MatRowColIteratorDecrementOperatorTest)
// Check that postfix-- does not decrement the position when position is
// pointing to the beginning.
auto junk = it2--; (void)(junk);
BOOST_REQUIRE_EQUAL(it1.row(), it2.row());
BOOST_REQUIRE_EQUAL(it1.col(), it2.col());
REQUIRE(it1.row() == it2.row());
REQUIRE(it1.col() == it2.col());
// Check that prefix-- does not decrement the position when position is
// pointing to the beginning.
--it2;
BOOST_REQUIRE_EQUAL(it1.row(), it2.row());
BOOST_REQUIRE_EQUAL(it1.col(), it2.col());
REQUIRE(it1.row() == it2.row());
REQUIRE(it1.col() == it2.col());
}
// These tests don't work when the sparse iterators hold references and not
@@ -140,7 +139,7 @@ BOOST_AUTO_TEST_CASE(MatRowColIteratorDecrementOperatorTest)
/**
* Test sparse const_row_col_iterator for basic functionality.
*/
BOOST_AUTO_TEST_CASE(ConstSpRowColIteratorTest)
TEST_CASE("ConstSpRowColIteratorTest", "[ArmaExtendTest]")
{
sp_mat X(5, 5);
for (size_t i = 0; i < 5; ++i)
@@ -156,15 +155,15 @@ BOOST_AUTO_TEST_CASE(ConstSpRowColIteratorTest)
for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
{
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == (count % 5) * 3 + (count / 5));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
++count;
}
BOOST_REQUIRE_EQUAL(count, 25);
REQUIRE(count == 25);
it = X.end_row_col();
do
{
@@ -172,20 +171,20 @@ BOOST_AUTO_TEST_CASE(ConstSpRowColIteratorTest)
--count;
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
} while (it != X.begin_row_col());
BOOST_REQUIRE_EQUAL(count, 1);
REQUIRE(count == 1);
}
/**
* Test sparse row_col_iterator for basic functionality.
*/
BOOST_AUTO_TEST_CASE(SpRowColIteratorTest)
TEST_CASE("SpRowColIteratorTest", "[ArmaExtendTest]")
{
sp_mat X(5, 5);
for (size_t i = 0; i < 5; ++i)
@@ -201,15 +200,15 @@ BOOST_AUTO_TEST_CASE(SpRowColIteratorTest)
for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
{
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
++count;
}
BOOST_REQUIRE_EQUAL(count, 25);
REQUIRE(count == 25);
it = X.end_row_col();
do
{
@@ -217,14 +216,12 @@ BOOST_AUTO_TEST_CASE(SpRowColIteratorTest)
--count;
// Check iterator value.
BOOST_REQUIRE_EQUAL(*it, (count % 5) * 3 + (count / 5));
REQUIRE(*it == ((count % 5) * 3 + (count / 5)));
// Check iterator position.
BOOST_REQUIRE_EQUAL(it.row(), count % 5);
BOOST_REQUIRE_EQUAL(it.col(), count / 5);
REQUIRE(it.row() == count % 5);
REQUIRE(it.col() == count / 5);
} while (it != X.begin_row_col());
BOOST_REQUIRE_EQUAL(count, 1);
REQUIRE(count == 1);
}
BOOST_AUTO_TEST_SUITE_END();
+64 -68
View File
@@ -13,17 +13,15 @@
#include <mlpack/methods/dbscan/dbscan.hpp>
#include <mlpack/methods/dbscan/random_point_selection.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "test_catch_tools.hpp"
#include "catch.hpp"
using namespace mlpack;
using namespace mlpack::range;
using namespace mlpack::dbscan;
using namespace mlpack::distribution;
BOOST_AUTO_TEST_SUITE(DBSCANTest);
BOOST_AUTO_TEST_CASE(OneClusterTest)
TEST_CASE("OneClusterTest", "[DBSCANTest]")
{
// Make sure that if we have points in the unit box, and if we set epsilon
// large enough, all points end up as in one cluster.
@@ -34,16 +32,16 @@ BOOST_AUTO_TEST_CASE(OneClusterTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_EQUAL(clusters, 1);
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
REQUIRE(clusters == 1);
REQUIRE(assignments.n_elem == points.n_cols);
for (size_t i = 0; i < assignments.n_elem; ++i)
BOOST_REQUIRE_EQUAL(assignments[i], 0);
REQUIRE(assignments[i] == 0);
}
/**
* When epsilon is small enough, every point returned should be noise.
*/
BOOST_AUTO_TEST_CASE(TinyEpsilonTest)
TEST_CASE("TinyEpsilonTest", "[DBSCANTest]")
{
arma::mat points(10, 200, arma::fill::randu);
@@ -52,16 +50,16 @@ BOOST_AUTO_TEST_CASE(TinyEpsilonTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_EQUAL(clusters, 0);
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
REQUIRE(clusters == 0);
REQUIRE(assignments.n_elem == points.n_cols);
for (size_t i = 0; i < assignments.n_elem; ++i)
BOOST_REQUIRE_EQUAL(assignments[i], SIZE_MAX);
REQUIRE(assignments[i] == SIZE_MAX);
}
/**
* Check that outliers are properly labeled as noise.
*/
BOOST_AUTO_TEST_CASE(OutlierTest)
TEST_CASE("OutlierTest", "[DBSCANTest]")
{
arma::mat points(2, 200, arma::fill::randu);
@@ -75,17 +73,17 @@ BOOST_AUTO_TEST_CASE(OutlierTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_GT(clusters, 0);
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
BOOST_REQUIRE_EQUAL(assignments[15], SIZE_MAX);
BOOST_REQUIRE_EQUAL(assignments[45], SIZE_MAX);
BOOST_REQUIRE_EQUAL(assignments[101], SIZE_MAX);
REQUIRE(clusters > 0);
REQUIRE(assignments.n_elem == points.n_cols);
REQUIRE(assignments[15] == SIZE_MAX);
REQUIRE(assignments[45] == SIZE_MAX);
REQUIRE(assignments[101] == SIZE_MAX);
}
/**
* Check that the Gaussian clusters are correctly found.
*/
BOOST_AUTO_TEST_CASE(GaussiansTest)
TEST_CASE("GaussiansTest", "[DBSCANTest]")
{
arma::mat points(3, 300);
@@ -105,7 +103,7 @@ BOOST_AUTO_TEST_CASE(GaussiansTest)
arma::Row<size_t> assignments;
arma::mat centroids;
const size_t clusters = d.Cluster(points, assignments, centroids);
BOOST_REQUIRE_EQUAL(clusters, 3);
REQUIRE(clusters == 3);
// Our centroids should be close to one of our Gaussians.
arma::Row<size_t> matches(3);
@@ -120,35 +118,35 @@ BOOST_AUTO_TEST_CASE(GaussiansTest)
matches(2) = j;
}
BOOST_REQUIRE_NE(matches(0), matches(1));
BOOST_REQUIRE_NE(matches(1), matches(2));
BOOST_REQUIRE_NE(matches(2), matches(0));
REQUIRE(matches(0) != matches(1));
REQUIRE(matches(1) != matches(2));
REQUIRE(matches(2) != matches(0));
BOOST_REQUIRE_NE(matches(0), 3);
BOOST_REQUIRE_NE(matches(1), 3);
BOOST_REQUIRE_NE(matches(2), 3);
REQUIRE(matches(0) != 3);
REQUIRE(matches(1) != 3);
REQUIRE(matches(2) != 3);
for (size_t i = 0; i < 100; ++i)
{
// Each point should either be noise or in cluster matches(0).
BOOST_REQUIRE_NE(assignments(i), matches(1));
BOOST_REQUIRE_NE(assignments(i), matches(2));
REQUIRE(assignments(i) != matches(1));
REQUIRE(assignments(i) != matches(2));
}
for (size_t i = 100; i < 200; ++i)
{
BOOST_REQUIRE_NE(assignments(i), matches(0));
BOOST_REQUIRE_NE(assignments(i), matches(2));
REQUIRE(assignments(i) != matches(0));
REQUIRE(assignments(i) != matches(2));
}
for (size_t i = 200; i < 300; ++i)
{
BOOST_REQUIRE_NE(assignments(i), matches(0));
BOOST_REQUIRE_NE(assignments(i), matches(1));
REQUIRE(assignments(i) != matches(0));
REQUIRE(assignments(i) != matches(1));
}
}
BOOST_AUTO_TEST_CASE(OneClusterSingleModeTest)
TEST_CASE("OneClusterSingleModeTest", "[DBSCANTest]")
{
// Make sure that if we have points in the unit box, and if we set epsilon
// large enough, all points end up as in one cluster.
@@ -159,16 +157,16 @@ BOOST_AUTO_TEST_CASE(OneClusterSingleModeTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_EQUAL(clusters, 1);
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
REQUIRE(clusters == 1);
REQUIRE(assignments.n_elem == points.n_cols);
for (size_t i = 0; i < assignments.n_elem; ++i)
BOOST_REQUIRE_EQUAL(assignments[i], 0);
REQUIRE(assignments[i] == 0);
}
/**
* When epsilon is small enough, every point returned should be noise.
*/
BOOST_AUTO_TEST_CASE(TinyEpsilonSingleModeTest)
TEST_CASE("TinyEpsilonSingleModeTest", "[DBSCANTest]")
{
arma::mat points(10, 200, arma::fill::randu);
@@ -177,16 +175,16 @@ BOOST_AUTO_TEST_CASE(TinyEpsilonSingleModeTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_EQUAL(clusters, 0);
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
REQUIRE(clusters == 0);
REQUIRE(assignments.n_elem == points.n_cols);
for (size_t i = 0; i < assignments.n_elem; ++i)
BOOST_REQUIRE_EQUAL(assignments[i], SIZE_MAX);
REQUIRE(assignments[i] == SIZE_MAX);
}
/**
* Check that outliers are properly labeled as noise.
*/
BOOST_AUTO_TEST_CASE(OutlierSingleModeTest)
TEST_CASE("OutlierSingleModeTest", "[DBSCANTest]")
{
arma::mat points(2, 200, arma::fill::randu);
@@ -200,17 +198,17 @@ BOOST_AUTO_TEST_CASE(OutlierSingleModeTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_GT(clusters, 0);
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
BOOST_REQUIRE_EQUAL(assignments[15], SIZE_MAX);
BOOST_REQUIRE_EQUAL(assignments[45], SIZE_MAX);
BOOST_REQUIRE_EQUAL(assignments[101], SIZE_MAX);
REQUIRE(clusters > 0);
REQUIRE(assignments.n_elem == points.n_cols);
REQUIRE(assignments[15] == SIZE_MAX);
REQUIRE(assignments[45] == SIZE_MAX);
REQUIRE(assignments[101] == SIZE_MAX);
}
/**
* Check that the Gaussian clusters are correctly found.
*/
BOOST_AUTO_TEST_CASE(GaussiansSingleModeTest)
TEST_CASE("GaussiansSingleModeTest", "[DBSCANTest]")
{
arma::mat points(3, 300);
@@ -230,7 +228,7 @@ BOOST_AUTO_TEST_CASE(GaussiansSingleModeTest)
arma::Row<size_t> assignments;
arma::mat centroids;
const size_t clusters = d.Cluster(points, assignments, centroids);
BOOST_REQUIRE_EQUAL(clusters, 3);
REQUIRE(clusters == 3);
// Our centroids should be close to one of our Gaussians.
arma::Row<size_t> matches(3);
@@ -245,38 +243,38 @@ BOOST_AUTO_TEST_CASE(GaussiansSingleModeTest)
matches(2) = j;
}
BOOST_REQUIRE_NE(matches(0), matches(1));
BOOST_REQUIRE_NE(matches(1), matches(2));
BOOST_REQUIRE_NE(matches(2), matches(0));
REQUIRE(matches(0) != matches(1));
REQUIRE(matches(1) != matches(2));
REQUIRE(matches(2) != matches(0));
BOOST_REQUIRE_NE(matches(0), 3);
BOOST_REQUIRE_NE(matches(1), 3);
BOOST_REQUIRE_NE(matches(2), 3);
REQUIRE(matches(0) != 3);
REQUIRE(matches(1) != 3);
REQUIRE(matches(2) != 3);
for (size_t i = 0; i < 100; ++i)
{
// Each point should either be noise or in cluster matches(0).
BOOST_REQUIRE_NE(assignments(i), matches(1));
BOOST_REQUIRE_NE(assignments(i), matches(2));
REQUIRE(assignments(i) != matches(1));
REQUIRE(assignments(i) != matches(2));
}
for (size_t i = 100; i < 200; ++i)
{
BOOST_REQUIRE_NE(assignments(i), matches(0));
BOOST_REQUIRE_NE(assignments(i), matches(2));
REQUIRE(assignments(i) != matches(0));
REQUIRE(assignments(i) != matches(2));
}
for (size_t i = 200; i < 300; ++i)
{
BOOST_REQUIRE_NE(assignments(i), matches(0));
BOOST_REQUIRE_NE(assignments(i), matches(1));
REQUIRE(assignments(i) != matches(0));
REQUIRE(assignments(i) != matches(1));
}
}
/**
* Check that OrderedPointSelection works correctly.
*/
BOOST_AUTO_TEST_CASE(OrderedPointSelectionTest)
TEST_CASE("OrderedPointSelectionTest", "[DBSCANTest]")
{
arma::mat points(10, 200, arma::fill::randu);
@@ -285,16 +283,16 @@ BOOST_AUTO_TEST_CASE(OrderedPointSelectionTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_EQUAL(clusters, 1);
REQUIRE(clusters == 1);
// The number of assignments returned should be the same as points.
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
REQUIRE(assignments.n_elem == points.n_cols);
}
/**
* Check that RandomPointSelection works correctly.
*/
BOOST_AUTO_TEST_CASE(RandomPointSelectionTest)
TEST_CASE("RandomPointSelectionTest", "[DBSCANTest]")
{
arma::mat points(10, 200, arma::fill::randu);
@@ -303,10 +301,8 @@ BOOST_AUTO_TEST_CASE(RandomPointSelectionTest)
arma::Row<size_t> assignments;
const size_t clusters = d.Cluster(points, assignments);
BOOST_REQUIRE_EQUAL(clusters, 1);
REQUIRE(clusters == 1);
// The number of assignments returned should be the same as points.
BOOST_REQUIRE_EQUAL(assignments.n_elem, points.n_cols);
REQUIRE(assignments.n_elem == points.n_cols);
}
BOOST_AUTO_TEST_SUITE_END();
+46 -41
View File
@@ -19,8 +19,8 @@ static const std::string testName = "DBSCAN";
#include "test_helper.hpp"
#include <mlpack/methods/dbscan/dbscan_main.cpp>
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../catch.hpp"
#include "../test_catch_tools.hpp"
using namespace mlpack;
@@ -41,17 +41,16 @@ struct DBSCANTestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(DBSCANMainTest, DBSCANTestFixture);
/**
* Check that number of output labels and number of input
* points are equal.
*/
BOOST_AUTO_TEST_CASE(DBSCANOutputDimensionTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANOutputDimensionTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
size_t inputSize = inputData.n_cols;
@@ -60,45 +59,45 @@ BOOST_AUTO_TEST_CASE(DBSCANOutputDimensionTest)
mlpackMain();
// Check that number of predicted labels is equal to the input test points.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Row<size_t>>("assignments").n_cols,
inputSize);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Row<size_t>>("assignments").n_rows,
1);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("centroids").n_rows, 4);
BOOST_REQUIRE_GE(IO::GetParam<arma::mat>("centroids").n_cols, 1);
REQUIRE(IO::GetParam<arma::Row<size_t>>("assignments").n_cols == inputSize);
REQUIRE(IO::GetParam<arma::Row<size_t>>("assignments").n_rows == 1);
REQUIRE(IO::GetParam<arma::mat>("centroids").n_rows == 4);
REQUIRE(IO::GetParam<arma::mat>("centroids").n_cols >= 1);
}
/**
* Check that radius of search(epsilon) is always non-negative.
*/
BOOST_AUTO_TEST_CASE(DBSCANEpsilonTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANEpsilonTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
SetInputParam("epsilon", (double) -0.5);
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Check that minimum size of cluster is always non-negative.
*/
BOOST_AUTO_TEST_CASE(DBSCANMinSizeTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANMinSizeTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
SetInputParam("min_size", (int) -1);
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
@@ -106,11 +105,12 @@ BOOST_AUTO_TEST_CASE(DBSCANMinSizeTest)
* Check that no point is labelled as noise point
* when min_size is equal to 1.
*/
BOOST_AUTO_TEST_CASE(DBSCANClusterNumberTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANClusterNumberTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
SetInputParam("min_size", (int) 1);
@@ -122,18 +122,19 @@ BOOST_AUTO_TEST_CASE(DBSCANClusterNumberTest)
output = std::move(IO::GetParam<arma::Row<size_t>>("assignments"));
for (size_t i = 0; i < output.n_elem; ++i)
BOOST_REQUIRE_LT(output[i], inputData.n_cols);
REQUIRE(output[i] < inputData.n_cols);
}
/**
* Check that the cluster assignment is different for different
* values of epsilon.
*/
BOOST_AUTO_TEST_CASE(DBSCANDiffEpsilonTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANDiffEpsilonTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
SetInputParam("epsilon", (double) 1.0);
@@ -156,18 +157,19 @@ BOOST_AUTO_TEST_CASE(DBSCANDiffEpsilonTest)
arma::Row<size_t> output2;
output2 = std::move(IO::GetParam<arma::Row<size_t>>("assignments"));
BOOST_REQUIRE_GT(arma::accu(output1 != output2), 1);
REQUIRE(arma::accu(output1 != output2) > 1);
}
/**
* Check that the cluster assignment is different for different
* values of Min Size.
*/
BOOST_AUTO_TEST_CASE(DBSCANDiffMinSizeTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANDiffMinSizeTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
SetInputParam("epsilon", (double) 0.4);
@@ -193,7 +195,7 @@ BOOST_AUTO_TEST_CASE(DBSCANDiffMinSizeTest)
arma::Row<size_t> output2;
output2 = std::move(IO::GetParam<arma::Row<size_t>>("assignments"));
BOOST_REQUIRE_GT(arma::accu(output1 != output2), 1);
REQUIRE(arma::accu(output1 != output2) > 1);
}
/**
@@ -201,17 +203,18 @@ BOOST_AUTO_TEST_CASE(DBSCANDiffMinSizeTest)
* tree types. kd, r, r-star, x, hilbert-r, r-plus,
* r-plus-plus, cover, ball.
*/
BOOST_AUTO_TEST_CASE(DBSCANTreeTypeTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANTreeTypeTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", std::move(inputData));
SetInputParam("tree_type", std::string("binary"));
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
@@ -219,11 +222,12 @@ BOOST_AUTO_TEST_CASE(DBSCANTreeTypeTest)
* Check that the assignment of cluster is same if
* different tree type is used for search.
*/
BOOST_AUTO_TEST_CASE(DBSCANDiffTreeTypeTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANDiffTreeTypeTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
// Tree type = kd tree.
@@ -369,11 +373,12 @@ BOOST_AUTO_TEST_CASE(DBSCANDiffTreeTypeTest)
* Check that the assignment of cluster is same if
* single tree is used for search.
*/
BOOST_AUTO_TEST_CASE(DBSCANSingleTreeTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANSingleTreeTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
@@ -401,11 +406,12 @@ BOOST_AUTO_TEST_CASE(DBSCANSingleTreeTest)
* Check that the assignment of cluster is same if
* single tree is used for search.
*/
BOOST_AUTO_TEST_CASE(DBSCANNaiveSearchTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANNaiveSearchTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
@@ -433,11 +439,12 @@ BOOST_AUTO_TEST_CASE(DBSCANNaiveSearchTest)
* Check that the assignment of cluster is different if
* point selection policies are different.
*/
BOOST_AUTO_TEST_CASE(DBSCANRandomSelectionFlagTest)
TEST_CASE_METHOD(DBSCANTestFixture, "DBSCANRandomSelectionFlagTest",
"[DBSCANMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("iris.csv", inputData))
BOOST_FAIL("Unable to load dataset iris.csv!");
FAIL("Unable to load dataset iris.csv!");
SetInputParam("input", inputData);
SetInputParam("epsilon", (double) 0.358);
@@ -466,7 +473,5 @@ BOOST_AUTO_TEST_CASE(DBSCANRandomSelectionFlagTest)
arma::Row<size_t> randomOutput;
randomOutput = std::move(IO::GetParam<arma::Row<size_t>>("assignments"));
BOOST_REQUIRE_GT(arma::accu(orderedOutput != randomOutput), 0);
REQUIRE(arma::accu(orderedOutput != randomOutput) > 0);
}
BOOST_AUTO_TEST_SUITE_END();
+16 -17
View File
@@ -19,8 +19,7 @@ static const std::string testName = "PrincipalComponentAnalysis";
#include "test_helper.hpp"
#include <mlpack/methods/pca/pca_main.cpp>
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../catch.hpp"
using namespace mlpack;
@@ -41,12 +40,11 @@ struct PCATestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(PCAMainTest, PCATestFixture);
/**
* Make sure that if we ask for a dataset in 3 dimensions back, we get it.
*/
BOOST_AUTO_TEST_CASE(PCADimensionTest)
TEST_CASE_METHOD(PCATestFixture, "PCADimensionTest",
"[PCAMainTest][BindingTests]")
{
arma::mat x = arma::randu<arma::mat>(5, 5);
@@ -57,15 +55,16 @@ BOOST_AUTO_TEST_CASE(PCADimensionTest)
mlpackMain();
// Now check that the output has 3 dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows, 3);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols, 5);
REQUIRE(IO::GetParam<arma::mat>("output").n_rows == 3);
REQUIRE(IO::GetParam<arma::mat>("output").n_cols == 5);
}
/**
* Ensure that if we retain all variance, we get back a matrix with the same
* dimensionality.
*/
BOOST_AUTO_TEST_CASE(PCAVarRetainTest)
TEST_CASE_METHOD(PCATestFixture, "PCAVarRetainTest",
"[PCAMainTest][BindingTests]")
{
arma::mat x = arma::randu<arma::mat>(4, 5);
@@ -77,14 +76,15 @@ BOOST_AUTO_TEST_CASE(PCAVarRetainTest)
mlpackMain();
// Check that the output has 5 dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows, 4);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols, 5);
REQUIRE(IO::GetParam<arma::mat>("output").n_rows == 4);
REQUIRE(IO::GetParam<arma::mat>("output").n_cols == 5);
}
/**
* Ensure that if we retain no variance, we get back no dimensions.
*/
BOOST_AUTO_TEST_CASE(PCANoVarRetainTest)
TEST_CASE_METHOD(PCATestFixture, "PCANoVarRetainTest",
"[PCAMainTest][BindingTests]")
{
arma::mat x = arma::randu<arma::mat>(5, 5);
@@ -96,14 +96,15 @@ BOOST_AUTO_TEST_CASE(PCANoVarRetainTest)
mlpackMain();
// Check that the output has 1 dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows, 1);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols, 5);
REQUIRE(IO::GetParam<arma::mat>("output").n_rows == 1);
REQUIRE(IO::GetParam<arma::mat>("output").n_cols == 5);
}
/**
* Check that we can't specify an invalid new dimensionality.
*/
BOOST_AUTO_TEST_CASE(PCATooHighNewDimensionalityTest)
TEST_CASE_METHOD(PCATestFixture, "PCATooHighNewDimensionalityTest",
"[PCAMainTest][BindingTests]")
{
arma::mat x = arma::randu<arma::mat>(5, 5);
@@ -111,8 +112,6 @@ BOOST_AUTO_TEST_CASE(PCATooHighNewDimensionalityTest)
SetInputParam("new_dimensionality", (int) 7); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
BOOST_AUTO_TEST_SUITE_END();
@@ -18,8 +18,8 @@ static const std::string testName = "RandomForest";
#include <mlpack/methods/random_forest/random_forest_main.cpp>
#include "test_helper.hpp"
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../catch.hpp"
#include "../test_catch_tools.hpp"
using namespace mlpack;
@@ -40,25 +40,24 @@ struct RandomForestTestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(RandomForestMainTest, RandomForestTestFixture);
/**
* Check that number of output points and number of input
* points are equal and have appropriate number of classes.
*/
BOOST_AUTO_TEST_CASE(RandomForestOutputDimensionTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestOutputDimensionTest",
"[RandomForestMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
arma::mat testData;
if (!data::Load("vc2_test.csv", testData))
BOOST_FAIL("Cannot load test dataset vc2.csv!");
FAIL("Cannot load test dataset vc2.csv!");
size_t testSize = testData.n_cols;
@@ -72,34 +71,32 @@ BOOST_AUTO_TEST_CASE(RandomForestOutputDimensionTest)
mlpackMain();
// Check that number of output points are equal to number of input points.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Row<size_t>>("predictions").n_cols,
testSize);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("probabilities").n_cols,
testSize);
REQUIRE(IO::GetParam<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(IO::GetParam<arma::mat>("probabilities").n_cols == testSize);
// Check number of output rows equals number of classes in case of
// probabilities and 1 for predictions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Row<size_t>>("predictions").n_rows,
1);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("probabilities").n_rows, 3);
REQUIRE(IO::GetParam<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(IO::GetParam<arma::mat>("probabilities").n_rows == 3);
}
/**
* Ensure that saved model can be used again.
*/
BOOST_AUTO_TEST_CASE(RandomForestModelReuseTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestModelReuseTest",
"[RandomForestMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
arma::mat testData;
if (!data::Load("vc2_test.csv", testData))
BOOST_FAIL("Cannot load test dataset vc2.csv!");
FAIL("Cannot load test dataset vc2.csv!");
size_t testSize = testData.n_cols;
@@ -130,16 +127,13 @@ BOOST_AUTO_TEST_CASE(RandomForestModelReuseTest)
mlpackMain();
// Check that number of output points are equal to number of input points.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Row<size_t>>("predictions").n_cols,
testSize);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("probabilities").n_cols,
testSize);
REQUIRE(IO::GetParam<arma::Row<size_t>>("predictions").n_cols == testSize);
REQUIRE(IO::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(IO::GetParam<arma::Row<size_t>>("predictions").n_rows,
1);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("probabilities").n_rows, 3);
REQUIRE(IO::GetParam<arma::Row<size_t>>("predictions").n_rows == 1);
REQUIRE(IO::GetParam<arma::mat>("probabilities").n_rows == 3);
// Check that initial predictions and predictions using saved model are same.
CheckMatrices(predictions, IO::GetParam<arma::Row<size_t>>("predictions"));
@@ -149,75 +143,79 @@ BOOST_AUTO_TEST_CASE(RandomForestModelReuseTest)
/**
* Make sure number of trees specified is always a positive number.
*/
BOOST_AUTO_TEST_CASE(RandomForestNumOfTreesTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestNumOfTreesTest",
"[RandomForestMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
SetInputParam("num_trees", (int) 0); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Make sure minimum leaf size specified is always a positive number.
*/
BOOST_AUTO_TEST_CASE(RandomForestMinimumLeafSizeTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestMinimumLeafSizeTest",
"[RandomForestMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
SetInputParam("minimum_leaf_size", (int) 0); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Make sure maximum depth specified is always a positive number.
*/
BOOST_AUTO_TEST_CASE(RandomForestMaximumDepthTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestMaximumDepthTest",
"[RandomForestMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
SetInputParam("maximum_depth", (int) -1); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Make sure only one of training data or pre-trained model is passed.
*/
BOOST_AUTO_TEST_CASE(RandomForestTrainingVerTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestTrainingVerTest",
"[RandomForestMainTest][BindingTests]")
{
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
// Input training data.
SetInputParam("training", std::move(inputData));
@@ -230,7 +228,7 @@ BOOST_AUTO_TEST_CASE(RandomForestTrainingVerTest)
IO::GetParam<RandomForestModel*>("output_model"));
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
@@ -254,16 +252,17 @@ inline bool CheckDifferentTrees(const TreeType& nodeA, const TreeType& nodeB)
* Ensure that the trees have different structure as the minimum leaf size is
* changed.
*/
BOOST_AUTO_TEST_CASE(RandomForestDiffMinLeafSizeTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestDiffMinLeafSizeTest",
"[RandomForestMainTest][BindingTests]")
{
// Train for minimum leaf size 20.
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
// Input training data.
SetInputParam("training", inputData);
@@ -310,8 +309,8 @@ BOOST_AUTO_TEST_CASE(RandomForestDiffMinLeafSizeTest)
// Check that each tree is different.
for (size_t i = 0; i < rf1->rf.NumTrees(); ++i)
{
BOOST_REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf2->rf.Tree(i)));
BOOST_REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf3->rf.Tree(i)));
REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf2->rf.Tree(i)));
REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf3->rf.Tree(i)));
}
delete rf1;
@@ -323,24 +322,25 @@ BOOST_AUTO_TEST_CASE(RandomForestDiffMinLeafSizeTest)
* Ensure that the number of trees are different when num_trees is specified
* differently.
*/
BOOST_AUTO_TEST_CASE(RandomForestDiffNumTreeTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestDiffNumTreeTest",
"[RandomForestMainTest][BindingTests]")
{
// Train for num_trees 1.
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
arma::mat testData;
if (!data::Load("vc2_test.csv", testData))
BOOST_FAIL("Cannot load test dataset vc2_test.csv!");
FAIL("Cannot load test dataset vc2_test.csv!");
arma::Row<size_t> testLabels;
if (!data::Load("vc2_test_labels.txt", testLabels))
BOOST_FAIL("Cannot load labels for vc2__test_labels.txt");
FAIL("Cannot load labels for vc2__test_labels.txt");
// Input training data.
SetInputParam("training", inputData);
@@ -383,23 +383,24 @@ BOOST_AUTO_TEST_CASE(RandomForestDiffNumTreeTest)
const size_t numTrees3 =
IO::GetParam<RandomForestModel*>("output_model")->rf.NumTrees();
BOOST_REQUIRE_NE(numTrees1, numTrees2);
BOOST_REQUIRE_NE(numTrees2, numTrees3);
REQUIRE(numTrees1 != numTrees2);
REQUIRE(numTrees2 != numTrees3);
}
/**
* Ensure that the maximum_depth parameter makes a difference.
*/
BOOST_AUTO_TEST_CASE(RandomForestDiffMaxDepthTest)
TEST_CASE_METHOD(RandomForestTestFixture, "RandomForestDiffMaxDepthTest",
"[RandomForestMainTest][BindingTests]")
{
// Train for minimum leaf size 20.
arma::mat inputData;
if (!data::Load("vc2.csv", inputData))
BOOST_FAIL("Cannot load train dataset vc2.csv!");
FAIL("Cannot load train dataset vc2.csv!");
arma::Row<size_t> labels;
if (!data::Load("vc2_labels.txt", labels))
BOOST_FAIL("Cannot load labels for vc2_labels.txt");
FAIL("Cannot load labels for vc2_labels.txt");
// Input training data.
SetInputParam("training", inputData);
@@ -444,13 +445,11 @@ BOOST_AUTO_TEST_CASE(RandomForestDiffMaxDepthTest)
// Check that each tree is different.
for (size_t i = 0; i < rf1->rf.NumTrees(); ++i)
{
BOOST_REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf2->rf.Tree(i)));
BOOST_REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf3->rf.Tree(i)));
REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf2->rf.Tree(i)));
REQUIRE(CheckDifferentTrees(rf1->rf.Tree(i), rf3->rf.Tree(i)));
}
delete rf1;
delete rf2;
delete rf3;
}
BOOST_AUTO_TEST_SUITE_END();
+49 -55
View File
@@ -17,10 +17,7 @@
#include <mlpack/methods/pca/decomposition_policies/randomized_svd_method.hpp>
#include <mlpack/methods/pca/decomposition_policies/randomized_block_krylov_method.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
BOOST_AUTO_TEST_SUITE(PCATest);
#include "catch.hpp"
using namespace arma;
using namespace mlpack;
@@ -50,9 +47,9 @@ void ArmaComparisonPCA(
for (size_t i = 0; i < eigVal.n_elem; ++i)
{
if (eigVal[i] == 0.0)
BOOST_REQUIRE_SMALL(eigVal1[i], 1e-15);
REQUIRE(eigVal1[i] == Approx(0.0).margin(1e-15));
else
BOOST_REQUIRE_CLOSE(eigVal[i], eigVal1[i], 0.0001);
REQUIRE(eigVal[i] == Approx(eigVal1[i]).epsilon(1e-6));
}
}
@@ -88,14 +85,14 @@ void PCADimensionalityReduction(
++trial;
}
BOOST_REQUIRE_EQUAL(success, true);
REQUIRE(success == true);
// Compare with correct results.
mat correct("-1.53781086 -3.51358020 -0.16139887 -1.87706634 7.08985628;"
" 1.29937798 3.45762685 -2.69910005 -3.15620704 1.09830225");
BOOST_REQUIRE_EQUAL(data.n_rows, correct.n_rows);
BOOST_REQUIRE_EQUAL(data.n_cols, correct.n_cols);
REQUIRE(data.n_rows == correct.n_rows);
REQUIRE(data.n_cols == correct.n_cols);
// If the eigenvectors are pointed opposite directions, they will cancel
// each other out in this summation.
@@ -110,10 +107,10 @@ void PCADimensionalityReduction(
for (size_t row = 0; row < 2; row++)
for (size_t col = 0; col < 5; col++)
BOOST_REQUIRE_CLOSE(data(row, col), correct(row, col), 1e-3);
REQUIRE(data(row, col) == Approx(correct(row, col)).epsilon(1e-5));
// Check that the amount of variance retained is right.
BOOST_REQUIRE_CLOSE(varRetained, 0.904876047045906, 1e-5);
REQUIRE(varRetained == Approx(0.904876047045906).epsilon(1e-7));
}
/**
@@ -141,50 +138,50 @@ void PCAVarianceRetained()
arma::mat origData = data;
double varRetained = p.Apply(data, 0.1);
BOOST_REQUIRE_EQUAL(data.n_rows, 1);
BOOST_REQUIRE_EQUAL(data.n_cols, 5);
BOOST_REQUIRE_CLOSE(varRetained, 0.616237391936100, 1e-5);
REQUIRE(data.n_rows == 1);
REQUIRE(data.n_cols == 5);
REQUIRE(varRetained == Approx(0.616237391936100).epsilon(1e-7));
data = origData;
varRetained = p.Apply(data, 0.5);
BOOST_REQUIRE_EQUAL(data.n_rows, 1);
BOOST_REQUIRE_EQUAL(data.n_cols, 5);
BOOST_REQUIRE_CLOSE(varRetained, 0.616237391936100, 1e-5);
REQUIRE(data.n_rows == 1);
REQUIRE(data.n_cols == 5);
REQUIRE(varRetained == Approx(0.616237391936100).epsilon(1e-7));
data = origData;
varRetained = p.Apply(data, 0.7);
BOOST_REQUIRE_EQUAL(data.n_rows, 2);
BOOST_REQUIRE_EQUAL(data.n_cols, 5);
BOOST_REQUIRE_CLOSE(varRetained, 0.904876047045906, 1e-5);
REQUIRE(data.n_rows == 2);
REQUIRE(data.n_cols == 5);
REQUIRE(varRetained == Approx(0.904876047045906).epsilon(1e-7));
data = origData;
varRetained = p.Apply(data, 0.904);
BOOST_REQUIRE_EQUAL(data.n_rows, 2);
BOOST_REQUIRE_EQUAL(data.n_cols, 5);
BOOST_REQUIRE_CLOSE(varRetained, 0.904876047045906, 1e-5);
REQUIRE(data.n_rows == 2);
REQUIRE(data.n_cols == 5);
REQUIRE(varRetained == Approx(0.904876047045906).epsilon(1e-7));
data = origData;
varRetained = p.Apply(data, 0.905);
BOOST_REQUIRE_EQUAL(data.n_rows, 3);
BOOST_REQUIRE_EQUAL(data.n_cols, 5);
BOOST_REQUIRE_CLOSE(varRetained, 1.0, 1e-5);
REQUIRE(data.n_rows == 3);
REQUIRE(data.n_cols == 5);
REQUIRE(varRetained == Approx(1.0).epsilon(1e-7));
data = origData;
varRetained = p.Apply(data, 1.0);
BOOST_REQUIRE_EQUAL(data.n_rows, 3);
BOOST_REQUIRE_EQUAL(data.n_cols, 5);
BOOST_REQUIRE_CLOSE(varRetained, 1.0, 1e-5);
REQUIRE(data.n_rows == 3);
REQUIRE(data.n_cols == 5);
REQUIRE(varRetained == Approx(1.0).epsilon(1e-7));
}
/**
* Compare the output of our exact PCA implementation with Armadillo's.
*/
BOOST_AUTO_TEST_CASE(ArmaComparisonExactPCATest)
TEST_CASE("ArmaComparisonExactPCATest", "[PCATest]")
{
ArmaComparisonPCA<ExactSVDPolicy>();
}
@@ -193,7 +190,7 @@ BOOST_AUTO_TEST_CASE(ArmaComparisonExactPCATest)
* Compare the output of our randomized block krylov PCA implementation with
* Armadillo's.
*/
BOOST_AUTO_TEST_CASE(ArmaComparisonRandomizedBlockKrylovPCATest)
TEST_CASE("ArmaComparisonRandomizedBlockKrylovPCATest", "[PCATest]")
{
RandomizedBlockKrylovSVDPolicy decomposition(5);
ArmaComparisonPCA<RandomizedBlockKrylovSVDPolicy>(false, decomposition);
@@ -202,7 +199,7 @@ BOOST_AUTO_TEST_CASE(ArmaComparisonRandomizedBlockKrylovPCATest)
/**
* Compare the output of our randomized-SVD PCA implementation with Armadillo's.
*/
BOOST_AUTO_TEST_CASE(ArmaComparisonRandomizedPCATest)
TEST_CASE("ArmaComparisonRandomizedPCATest", "[PCATest]")
{
ArmaComparisonPCA<RandomizedSVDPolicy>();
}
@@ -211,7 +208,7 @@ BOOST_AUTO_TEST_CASE(ArmaComparisonRandomizedPCATest)
* Test that dimensionality reduction with exact-svd PCA works the same way
* MATLAB does (which should be correct!).
*/
BOOST_AUTO_TEST_CASE(ExactPCADimensionalityReductionTest)
TEST_CASE("ExactPCADimensionalityReductionTest", "[PCATest]")
{
PCADimensionalityReduction<ExactSVDPolicy>();
}
@@ -220,7 +217,7 @@ BOOST_AUTO_TEST_CASE(ExactPCADimensionalityReductionTest)
* Test that dimensionality reduction with randomized block krylov PCA works the
* same way MATLAB does (which should be correct!).
*/
BOOST_AUTO_TEST_CASE(RandomizedBlockKrylovPCADimensionalityReductionTest)
TEST_CASE("RandomizedBlockKrylovPCADimensionalityReductionTest", "[PCATest]")
{
RandomizedBlockKrylovSVDPolicy decomposition(5);
PCADimensionalityReduction<RandomizedBlockKrylovSVDPolicy>(false,
@@ -231,7 +228,7 @@ BOOST_AUTO_TEST_CASE(RandomizedBlockKrylovPCADimensionalityReductionTest)
* Test that dimensionality reduction with randomized-svd PCA works the same way
* MATLAB does (which should be correct!).
*/
BOOST_AUTO_TEST_CASE(RandomizedPCADimensionalityReductionTest)
TEST_CASE("RandomizedPCADimensionalityReductionTest", "[PCATest]")
{
PCADimensionalityReduction<RandomizedSVDPolicy>();
}
@@ -240,7 +237,7 @@ BOOST_AUTO_TEST_CASE(RandomizedPCADimensionalityReductionTest)
* Test that dimensionality reduction with QUIC-SVD PCA works the same way
* as the Exact-SVD PCA method.
*/
BOOST_AUTO_TEST_CASE(QUICPCADimensionalityReductionTest)
TEST_CASE("QUICPCADimensionalityReductionTest", "[PCATest]")
{
arma::mat data, data1;
data::Load("test_data_3_1000.csv", data);
@@ -275,16 +272,16 @@ BOOST_AUTO_TEST_CASE(QUICPCADimensionalityReductionTest)
}
}
BOOST_REQUIRE_GE(successes, 1);
BOOST_REQUIRE_EQUAL(data.n_rows, data1.n_rows);
BOOST_REQUIRE_EQUAL(data.n_cols, data1.n_cols);
REQUIRE(successes >= 1);
REQUIRE(data.n_rows == data1.n_rows);
REQUIRE(data.n_cols == data1.n_cols);
}
/**
* Test that setting the variance retained parameter to perform dimensionality
* reduction works using the exact svd PCA method.
*/
BOOST_AUTO_TEST_CASE(ExactPCAVarianceRetainedTest)
TEST_CASE("ExactPCAVarianceRetainedTest", "[PCATest]")
{
PCAVarianceRetained<ExactSVDPolicy>();
}
@@ -292,7 +289,7 @@ BOOST_AUTO_TEST_CASE(ExactPCAVarianceRetainedTest)
/**
* Test that scaling PCA works.
*/
BOOST_AUTO_TEST_CASE(PCAScalingTest)
TEST_CASE("PCAScalingTest", "[PCATest]")
{
// Generate an artificial dataset in 3 dimensions.
arma::mat data(3, 5000);
@@ -317,25 +314,22 @@ BOOST_AUTO_TEST_CASE(PCAScalingTest)
// The first two components of the eigenvector with largest eigenvalue should
// be somewhere near sqrt(2) / 2. The third component should be close to
// zero. There is noise, of course...
BOOST_REQUIRE_CLOSE(std::abs(eigvec(0, 0)), sqrt(2) / 2, 0.35);
BOOST_REQUIRE_CLOSE(std::abs(eigvec(1, 0)), sqrt(2) / 2, 0.35);
BOOST_REQUIRE_SMALL(eigvec(2, 0), 0.1); // Large tolerance for noise.
REQUIRE(std::abs(eigvec(0, 0)) == Approx(sqrt(2) / 2).epsilon(0.0035));
REQUIRE(std::abs(eigvec(1, 0)) == Approx(sqrt(2) / 2).epsilon(0.0035));
REQUIRE(eigvec(2, 0) == Approx(0.0).margin(0.1)); // Large tolerance for noise.
// The second component should be focused almost entirely in the third
// dimension.
BOOST_REQUIRE_SMALL(eigvec(0, 1), 0.1);
BOOST_REQUIRE_SMALL(eigvec(1, 1), 0.1);
BOOST_REQUIRE_CLOSE(std::abs(eigvec(2, 1)), 1.0, 0.35);
REQUIRE(eigvec(0, 1) == Approx(0.0).margin(0.1));
REQUIRE(eigvec(1, 1) == Approx(0.0).margin(0.1));
REQUIRE(std::abs(eigvec(2, 1)) == Approx(1.0).epsilon(0.0035));
// The third component should have the same absolute value characteristics as
// the first (plus 20% tolerance).
BOOST_REQUIRE_CLOSE(std::abs(eigvec(0, 0)), sqrt(2) / 2, 0.35);
BOOST_REQUIRE_CLOSE(std::abs(eigvec(1, 0)), sqrt(2) / 2, 0.35);
BOOST_REQUIRE_SMALL(eigvec(2, 0), 0.1); // Large tolerance for noise.
// the first (plus tolerance).
REQUIRE(std::abs(eigvec(0, 0)) == Approx(sqrt(2) / 2).epsilon(0.0035));
REQUIRE(std::abs(eigvec(1, 0)) == Approx(sqrt(2) / 2).epsilon(0.0035));
REQUIRE(eigvec(2, 0) == Approx(0.0).margin(0.1)); // Large tolerance for noise.
// The eigenvalues should sum to three.
BOOST_REQUIRE_CLOSE(accu(eigval), 3.0, 0.1); // 10% tolerance.
REQUIRE(accu(eigval) == Approx(3.0).epsilon(0.001));
}
BOOST_AUTO_TEST_SUITE_END();
+50 -55
View File
@@ -13,20 +13,17 @@
#include <mlpack/methods/random_forest/random_forest.hpp>
#include <mlpack/methods/decision_tree/random_dimension_select.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "catch.hpp"
#include "serialization.hpp"
#include "mock_categorical_data.hpp"
using namespace mlpack;
using namespace mlpack::tree;
BOOST_AUTO_TEST_SUITE(RandomForestTest);
/**
* Make sure bootstrap sampling produces numbers in the dataset.
*/
BOOST_AUTO_TEST_CASE(BootstrapNoWeightsTest)
TEST_CASE("BootstrapNoWeightsTest", "[RandomForestTest]")
{
arma::mat dataset(1, 1000);
dataset.row(0) = arma::linspace<arma::rowvec>(1000, 1999, 1000);
@@ -44,16 +41,16 @@ BOOST_AUTO_TEST_CASE(BootstrapNoWeightsTest)
Bootstrap<false>(dataset, labels, weights, bootstrapDataset,
bootstrapLabels, bootstrapWeights);
BOOST_REQUIRE_EQUAL(bootstrapDataset.n_cols, 1000);
BOOST_REQUIRE_EQUAL(bootstrapDataset.n_rows, 1);
BOOST_REQUIRE_EQUAL(bootstrapLabels.n_elem, 1000);
REQUIRE(bootstrapDataset.n_cols == 1000);
REQUIRE(bootstrapDataset.n_rows == 1);
REQUIRE(bootstrapLabels.n_elem == 1000);
// Check each dataset element.
for (size_t i = 0; i < dataset.n_cols; ++i)
{
BOOST_REQUIRE_GE(bootstrapDataset(0, i), 1000);
BOOST_REQUIRE_LE(bootstrapDataset(0, i), 1999);
BOOST_REQUIRE_EQUAL(bootstrapLabels[i], 1);
REQUIRE(bootstrapDataset(0, i) >= 1000);
REQUIRE(bootstrapDataset(0, i) <= 1999);
REQUIRE(bootstrapLabels[i] == 1);
}
}
}
@@ -61,7 +58,7 @@ BOOST_AUTO_TEST_CASE(BootstrapNoWeightsTest)
/**
* Make sure bootstrap sampling produces numbers in the dataset.
*/
BOOST_AUTO_TEST_CASE(BootstrapWeightsTest)
TEST_CASE("BootstrapWeightsTest", "[RandomForestTest]")
{
arma::mat dataset(1, 1000);
dataset.row(0) = arma::linspace<arma::rowvec>(1000, 1999, 1000);
@@ -79,19 +76,19 @@ BOOST_AUTO_TEST_CASE(BootstrapWeightsTest)
Bootstrap<true>(dataset, labels, weights, bootstrapDataset,
bootstrapLabels, bootstrapWeights);
BOOST_REQUIRE_EQUAL(bootstrapDataset.n_cols, 1000);
BOOST_REQUIRE_EQUAL(bootstrapDataset.n_rows, 1);
BOOST_REQUIRE_EQUAL(bootstrapLabels.n_elem, 1000);
BOOST_REQUIRE_EQUAL(bootstrapWeights.n_elem, 1000);
REQUIRE(bootstrapDataset.n_cols == 1000);
REQUIRE(bootstrapDataset.n_rows == 1);
REQUIRE(bootstrapLabels.n_elem == 1000);
REQUIRE(bootstrapWeights.n_elem == 1000);
// Check each dataset element.
for (size_t i = 0; i < dataset.n_cols; ++i)
{
BOOST_REQUIRE_GE(bootstrapDataset(0, i), 1000);
BOOST_REQUIRE_LE(bootstrapDataset(0, i), 1999);
BOOST_REQUIRE_EQUAL(bootstrapLabels[i], 1);
BOOST_REQUIRE_GE(bootstrapWeights[i], 0.0);
BOOST_REQUIRE_LE(bootstrapWeights[i], 1.0);
REQUIRE(bootstrapDataset(0, i) >= 1000);
REQUIRE(bootstrapDataset(0, i) <= 1999);
REQUIRE(bootstrapLabels[i] == 1);
REQUIRE(bootstrapWeights[i] >= 0.0);
REQUIRE(bootstrapWeights[i] <= 1.0);
}
}
}
@@ -99,7 +96,7 @@ BOOST_AUTO_TEST_CASE(BootstrapWeightsTest)
/**
* Make sure an empty forest cannot predict.
*/
BOOST_AUTO_TEST_CASE(EmptyClassifyTest)
TEST_CASE("EmptyClassifyTest", "[RandomForestTest]")
{
RandomForest<> rf; // No training.
@@ -108,11 +105,11 @@ BOOST_AUTO_TEST_CASE(EmptyClassifyTest)
arma::mat probabilities;
size_t prediction;
arma::vec pointProbabilities;
BOOST_REQUIRE_THROW(rf.Classify(points, predictions), std::invalid_argument);
BOOST_REQUIRE_THROW(rf.Classify(points.col(0)), std::invalid_argument);
BOOST_REQUIRE_THROW(rf.Classify(points, predictions, probabilities),
REQUIRE_THROWS_AS(rf.Classify(points, predictions), std::invalid_argument);
REQUIRE_THROWS_AS(rf.Classify(points.col(0)), std::invalid_argument);
REQUIRE_THROWS_AS(rf.Classify(points, predictions, probabilities),
std::invalid_argument);
BOOST_REQUIRE_THROW(rf.Classify(points.col(0), prediction,
REQUIRE_THROWS_AS(rf.Classify(points.col(0), prediction,
pointProbabilities), std::invalid_argument);
}
@@ -120,7 +117,7 @@ BOOST_AUTO_TEST_CASE(EmptyClassifyTest)
* Test unweighted numeric learning, making sure that we get better performance
* than a single decision tree.
*/
BOOST_AUTO_TEST_CASE(UnweightedNumericLearningTest)
TEST_CASE("UnweightedNumericLearningTest", "[RandomForestTest]")
{
// Load the vc2 dataset.
arma::mat dataset;
@@ -148,15 +145,15 @@ BOOST_AUTO_TEST_CASE(UnweightedNumericLearningTest)
size_t rfCorrect = arma::accu(rfPredictions == testLabels);
size_t dtCorrect = arma::accu(dtPredictions == testLabels);
BOOST_REQUIRE_GE(rfCorrect, dtCorrect * 0.9);
BOOST_REQUIRE_GE(rfCorrect, size_t(0.7 * testDataset.n_cols));
REQUIRE(rfCorrect >= dtCorrect * 0.9);
REQUIRE(rfCorrect >= size_t(0.7 * testDataset.n_cols));
}
/**
* Test weighted numeric learning, making sure that we get better performance
* than a single decision tree.
*/
BOOST_AUTO_TEST_CASE(WeightedNumericLearningTest)
TEST_CASE("WeightedNumericLearningTest", "[RandomForestTest]")
{
arma::mat dataset;
arma::Row<size_t> labels;
@@ -200,15 +197,15 @@ BOOST_AUTO_TEST_CASE(WeightedNumericLearningTest)
size_t rfCorrect = arma::accu(rfPredictions == testLabels);
size_t dtCorrect = arma::accu(dtPredictions == testLabels);
BOOST_REQUIRE_GE(rfCorrect, dtCorrect * 0.9);
BOOST_REQUIRE_GE(rfCorrect, size_t(0.7 * testDataset.n_cols));
REQUIRE(rfCorrect >= dtCorrect * 0.9);
REQUIRE(rfCorrect >= size_t(0.7 * testDataset.n_cols));
}
/**
* Test unweighted categorical learning. Ensure that we get better performance
* with a random forest.
*/
BOOST_AUTO_TEST_CASE(UnweightedCategoricalLearningTest)
TEST_CASE("UnweightedCategoricalLearningTest", "[RandomForestTest]")
{
arma::mat d;
arma::Row<size_t> l;
@@ -237,14 +234,14 @@ BOOST_AUTO_TEST_CASE(UnweightedCategoricalLearningTest)
size_t rfCorrect = arma::accu(rfPredictions == testLabels);
size_t dtCorrect = arma::accu(dtPredictions == testLabels);
BOOST_REQUIRE_GE(rfCorrect, dtCorrect - 25);
BOOST_REQUIRE_GE(rfCorrect, size_t(0.7 * testData.n_cols));
REQUIRE(rfCorrect >= dtCorrect - 25);
REQUIRE(rfCorrect >= size_t(0.7 * testData.n_cols));
}
/**
* Test weighted categorical learning.
*/
BOOST_AUTO_TEST_CASE(WeightedCategoricalLearningTest)
TEST_CASE("WeightedCategoricalLearningTest", "[RandomForestTest]")
{
arma::mat d;
arma::Row<size_t> l;
@@ -295,14 +292,14 @@ BOOST_AUTO_TEST_CASE(WeightedCategoricalLearningTest)
size_t rfCorrect = arma::accu(rfPredictions == testLabels);
size_t dtCorrect = arma::accu(dtPredictions == testLabels);
BOOST_REQUIRE_GE(rfCorrect, dtCorrect - 25);
BOOST_REQUIRE_GE(rfCorrect, size_t(0.7 * testData.n_cols));
REQUIRE(rfCorrect >= dtCorrect - 25);
REQUIRE(rfCorrect >= size_t(0.7 * testData.n_cols));
}
/**
* Test that a leaf size equal to the dataset size learns nothing.
*/
BOOST_AUTO_TEST_CASE(LeafSizeDatasetTest)
TEST_CASE("LeafSizeDatasetTest", "[RandomForestTest]")
{
// Load the vc2 dataset.
arma::mat dataset;
@@ -324,19 +321,19 @@ BOOST_AUTO_TEST_CASE(LeafSizeDatasetTest)
size_t majorityClass = predictions[0];
arma::vec majorityProbs = probabilities.col(0);
BOOST_REQUIRE_EQUAL(probabilities.n_rows, 3);
BOOST_REQUIRE_EQUAL(probabilities.n_cols, dataset.n_cols);
BOOST_REQUIRE_EQUAL(predictions.n_elem, dataset.n_cols);
REQUIRE(probabilities.n_rows == 3);
REQUIRE(probabilities.n_cols == dataset.n_cols);
REQUIRE(predictions.n_elem == dataset.n_cols);
for (size_t i = 1; i < predictions.n_cols; ++i)
{
BOOST_REQUIRE_EQUAL(predictions[i], majorityClass);
REQUIRE(predictions[i] == majorityClass);
for (size_t j = 0; j < probabilities.n_rows; ++j)
BOOST_REQUIRE_CLOSE(probabilities(j, i), majorityProbs[j], 1e-5);
REQUIRE(probabilities(j, i) == Approx(majorityProbs[j]).epsilon(1e-7));
}
}
// Make sure we can serialize a random forest.
BOOST_AUTO_TEST_CASE(SerializationTest)
TEST_CASE("RandomForestSerializationTest", "[RandomForestTest]")
{
// Load the vc2 dataset.
arma::mat dataset;
@@ -372,7 +369,7 @@ BOOST_AUTO_TEST_CASE(SerializationTest)
* Test that RandomForest::Train() returns finite average entropy on numeric
* dataset.
*/
BOOST_AUTO_TEST_CASE(RandomForestNumericTrainReturnEntropy)
TEST_CASE("RandomForestNumericTrainReturnEntropy", "[RandomForestTest]")
{
arma::mat dataset;
arma::Row<size_t> labels;
@@ -400,20 +397,20 @@ BOOST_AUTO_TEST_CASE(RandomForestNumericTrainReturnEntropy)
RandomForest<GiniGain, RandomDimensionSelect> rf;
double entropy = rf.Train(dataset, labels, 3, 10, 1);
BOOST_REQUIRE_EQUAL(std::isfinite(entropy), true);
REQUIRE(std::isfinite(entropy) == true);
// Test random forest on weighted numeric dataset.
RandomForest<GiniGain, RandomDimensionSelect> wrf;
entropy = wrf.Train(dataset, labels, 3, weights, 10, 1);
BOOST_REQUIRE_EQUAL(std::isfinite(entropy), true);
REQUIRE(std::isfinite(entropy) == true);
}
/**
* Test that RandomForest::Train() returns finite average entropy on categorical
* dataset.
*/
BOOST_AUTO_TEST_CASE(RandomForestCategoricalTrainReturnEntropy)
TEST_CASE("RandomForestCategoricalTrainReturnEntropy", "[RandomForestTest]")
{
arma::mat d;
arma::Row<size_t> l;
@@ -447,20 +444,20 @@ BOOST_AUTO_TEST_CASE(RandomForestCategoricalTrainReturnEntropy)
double entropy = rf.Train(fullData, di, fullLabels, 5, 15 /* 15 trees */, 1,
1e-7, 0, MultipleRandomDimensionSelect(3));
BOOST_REQUIRE_EQUAL(std::isfinite(entropy), true);
REQUIRE(std::isfinite(entropy) == true);
// Test random forest on weighted categorical dataset.
RandomForest<> wrf;
entropy = wrf.Train(fullData, di, fullLabels, 5, weights, 15 /* 15 trees */,
1, 1e-7, 0, MultipleRandomDimensionSelect(3));
BOOST_REQUIRE_EQUAL(std::isfinite(entropy), true);
REQUIRE(std::isfinite(entropy) == true);
}
/**
* Test that different trees get generated.
*/
BOOST_AUTO_TEST_CASE(DifferentTreesTest)
TEST_CASE("DifferentTreesTest", "[RandomForestTest]")
{
arma::mat d(10, 100, arma::fill::randu);
arma::Row<size_t> l(100);
@@ -484,7 +481,5 @@ BOOST_AUTO_TEST_CASE(DifferentTreesTest)
++trial;
}
BOOST_REQUIRE_EQUAL(success, true);
REQUIRE(success == true);
}
BOOST_AUTO_TEST_SUITE_END();