[mlpack] 36/207: Update tests for slightly changed (and not final) API.

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:38 UTC 2017


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit b4fb1d924e77fda7f4e0b9dbc0f14c7eb0deb63a
Author: Ryan Curtin <ryan at ratml.org>
Date:   Thu Dec 15 16:34:37 2016 -0500

    Update tests for slightly changed (and not final) API.
---
 src/mlpack/tests/adaboost_test.cpp       |  13 ++-
 src/mlpack/tests/decision_stump_test.cpp | 140 +++++++++++++++++++++++++------
 src/mlpack/tests/serialization_test.cpp  |   8 +-
 3 files changed, 131 insertions(+), 30 deletions(-)

diff --git a/src/mlpack/tests/adaboost_test.cpp b/src/mlpack/tests/adaboost_test.cpp
index 542eb59..ec4b4ca 100644
--- a/src/mlpack/tests/adaboost_test.cpp
+++ b/src/mlpack/tests/adaboost_test.cpp
@@ -875,10 +875,15 @@ BOOST_AUTO_TEST_CASE(DecisionStumpSerializationTest)
                   abText.WeakLearner(i).Split(),
                   abBinary.WeakLearner(i).Split());
 
-    CheckMatrices(ab.WeakLearner(i).BinLabels(),
-                  abXml.WeakLearner(i).BinLabels(),
-                  abText.WeakLearner(i).BinLabels(),
-                  abBinary.WeakLearner(i).BinLabels());
+    for (size_t j = 0; j < ab.WeakLearner(i).Split().n_elem + 1; ++j)
+    {
+      BOOST_REQUIRE_EQUAL(ab.WeakLearner(i).Child(j).Label(),
+                          abXml.WeakLearner(i).Child(j).Label());
+      BOOST_REQUIRE_EQUAL(ab.WeakLearner(i).Child(j).Label(),
+                          abText.WeakLearner(i).Child(j).Label());
+      BOOST_REQUIRE_EQUAL(ab.WeakLearner(i).Child(j).Label(),
+                          abBinary.WeakLearner(i).Child(j).Label());
+    }
   }
 }
 
diff --git a/src/mlpack/tests/decision_stump_test.cpp b/src/mlpack/tests/decision_stump_test.cpp
index af1e5f7..2587e27 100644
--- a/src/mlpack/tests/decision_stump_test.cpp
+++ b/src/mlpack/tests/decision_stump_test.cpp
@@ -307,9 +307,9 @@ BOOST_AUTO_TEST_CASE(DimensionSelectionTest)
   for (size_t i = 0; i < ds.Split().n_elem; ++i)
   {
     if (ds.Split()[i] <= -3.0)
-      BOOST_CHECK_EQUAL(ds.BinLabels()[i], 0);
+      BOOST_CHECK_EQUAL(ds.Child(i).Label(), 0);
     else if (ds.Split()[i] >= 3.0)
-      BOOST_CHECK_EQUAL(ds.BinLabels()[i], 1);
+      BOOST_CHECK_EQUAL(ds.Child(i).Label(), 1);
   }
 }
 
@@ -359,37 +359,129 @@ BOOST_AUTO_TEST_CASE(EmptyConstructorTest)
 }
 
 /**
- * Ensure that a matrix holding ints can be trained.  The bigger issue here is
- * just compilation.
+ * Test the copy constructor for a stump.
  */
-BOOST_AUTO_TEST_CASE(IntTest)
+BOOST_AUTO_TEST_CASE(DecisionStumpCopyConstructorTest)
 {
-  // Train on a dataset and make sure something kind of makes sense.
-  imat trainingData;
-  trainingData << -7 << -6 << -5 << -4 << -3 << -2 << -1 << 0 << 1
-               << 2  << 3  << 4  << 5  << 6  << 7  << 8  << 9 << 10;
+  // This dataset comes from Chapter 6 of the book "Data Mining: Concepts,
+  // Models, Methods, and Algorithms" (2nd Edition) by Mehmed Kantardzic.  It is
+  // found on page 176 (and a description of the correct splitting dimension is
+  // given below that).
+  mat trainingData;
+  trainingData << 0  << 0  << 0  << 0  << 0  << 1  << 1  << 1  << 1
+               << 2  << 2  << 2  << 2  << 2  << endr
+               << 70 << 90 << 85 << 95 << 70 << 90 << 78 << 65 << 75
+               << 80 << 70 << 80 << 80 << 96 << endr
+               << 1  << 1  << 0  << 0  << 0  << 1  << 0  << 1  << 0
+               << 1  << 1  << 0  << 0  << 0  << endr;
 
   // No need to normalize labels here.
   Mat<size_t> labelsIn;
-  labelsIn << 0 << 0 << 0 << 0 << 1 << 1 << 0 << 0
-           << 1 << 1 << 1 << 2 << 1 << 2 << 2 << 2 << 2 << 2;
+  labelsIn << 0 << 1 << 1 << 1 << 0 << 0 << 0 << 0
+           << 0 << 1 << 1 << 0 << 0 << 0;
 
-  DecisionStump<arma::imat> ds(trainingData, labelsIn.row(0), 4, 3);
+  DecisionStump<> d(trainingData, labelsIn.row(0), 2);
 
-  imat testingData;
-  testingData << -6 << -6 << -2 << -1 << 3 << 5 << 7 << 9;
+  // Make a copy.
+  DecisionStump<> copy(d);
+  DecisionStump<> copy2 = d;
 
-  arma::Row<size_t> predictedLabels;
-  ds.Classify(testingData, predictedLabels);
+  // Check the objects for similarity.
+  BOOST_REQUIRE_EQUAL(d.Split().n_elem, copy.Split().n_elem);
+  BOOST_REQUIRE_EQUAL(d.Split().n_elem, copy2.Split().n_elem);
+  for (size_t i = 0; i < d.Split().n_elem + 1; ++i)
+  {
+    BOOST_REQUIRE_EQUAL(d.Child(i).Label(), copy.Child(i).Label());
+    CheckMatrices(d.Child(i).Split(), copy.Child(i).Split());
+    BOOST_REQUIRE_EQUAL(d.Child(i).Label(), copy2.Child(i).Label());
+    CheckMatrices(d.Child(i).Split(), copy2.Child(i).Split());
+  }
+}
 
-  BOOST_CHECK_EQUAL(predictedLabels(0, 0), 0);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 1), 0);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 2), 1);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 3), 1);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 4), 1);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 5), 1);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 6), 2);
-  BOOST_CHECK_EQUAL(predictedLabels(0, 7), 2);
+/**
+ * Test the move constructor for a stump.
+ */
+BOOST_AUTO_TEST_CASE(DecisionStumpMoveConstructorTest)
+{
+  // This dataset comes from Chapter 6 of the book "Data Mining: Concepts,
+  // Models, Methods, and Algorithms" (2nd Edition) by Mehmed Kantardzic.  It is
+  // found on page 176 (and a description of the correct splitting dimension is
+  // given below that).
+  mat trainingData;
+  trainingData << 0  << 0  << 0  << 0  << 0  << 1  << 1  << 1  << 1
+               << 2  << 2  << 2  << 2  << 2  << endr
+               << 70 << 90 << 85 << 95 << 70 << 90 << 78 << 65 << 75
+               << 80 << 70 << 80 << 80 << 96 << endr
+               << 1  << 1  << 0  << 0  << 0  << 1  << 0  << 1  << 0
+               << 1  << 1  << 0  << 0  << 0  << endr;
+
+  // No need to normalize labels here.
+  Mat<size_t> labelsIn;
+  labelsIn << 0 << 1 << 1 << 1 << 0 << 0 << 0 << 0
+           << 0 << 1 << 1 << 0 << 0 << 0;
+
+  DecisionStump<> d(trainingData, labelsIn.row(0), 2);
+  DecisionStump<> copy(d); // A copy to compare against.
+
+  DecisionStump<> move(std::move(d));
+  DecisionStump<> empty; // An empty object to compare against.
+
+  BOOST_REQUIRE_EQUAL(d.Split().n_elem, empty.Split().n_elem);
+  for (size_t i = 0; i < d.Split().n_elem + 1; ++i)
+  {
+    BOOST_REQUIRE_EQUAL(d.Child(i).Label(), empty.Child(i).Label());
+    CheckMatrices(d.Child(i).Split(), empty.Child(i).Split());
+  }
+
+  BOOST_REQUIRE_EQUAL(move.Split().n_elem, copy.Split().n_elem);
+  for (size_t i = 0; i < move.Split().n_elem + 1; ++i)
+  {
+    BOOST_REQUIRE_EQUAL(move.Child(i).Label(), copy.Child(i).Label());
+    CheckMatrices(move.Child(i).Split(), copy.Child(i).Split());
+  }
+}
+
+/**
+ * Test the move operator.
+ */
+BOOST_AUTO_TEST_CASE(DecisionStumpMoveOperatorTest)
+{
+  // This dataset comes from Chapter 6 of the book "Data Mining: Concepts,
+  // Models, Methods, and Algorithms" (2nd Edition) by Mehmed Kantardzic.  It is
+  // found on page 176 (and a description of the correct splitting dimension is
+  // given below that).
+  mat trainingData;
+  trainingData << 0  << 0  << 0  << 0  << 0  << 1  << 1  << 1  << 1
+               << 2  << 2  << 2  << 2  << 2  << endr
+               << 70 << 90 << 85 << 95 << 70 << 90 << 78 << 65 << 75
+               << 80 << 70 << 80 << 80 << 96 << endr
+               << 1  << 1  << 0  << 0  << 0  << 1  << 0  << 1  << 0
+               << 1  << 1  << 0  << 0  << 0  << endr;
+
+  // No need to normalize labels here.
+  Mat<size_t> labelsIn;
+  labelsIn << 0 << 1 << 1 << 1 << 0 << 0 << 0 << 0
+           << 0 << 1 << 1 << 0 << 0 << 0;
+
+  DecisionStump<> d(trainingData, labelsIn.row(0), 2);
+  DecisionStump<> copy(d); // A copy to compare against.
+
+  DecisionStump<> move = std::move(d);
+  DecisionStump<> empty; // An empty object to compare against.
+
+  BOOST_REQUIRE_EQUAL(d.Split().n_elem, empty.Split().n_elem);
+  for (size_t i = 0; i < d.Split().n_elem + 1; ++i)
+  {
+    BOOST_REQUIRE_EQUAL(d.Child(i).Label(), empty.Child(i).Label());
+    CheckMatrices(d.Child(i).Split(), empty.Child(i).Split());
+  }
+
+  BOOST_REQUIRE_EQUAL(move.Split().n_elem, copy.Split().n_elem);
+  for (size_t i = 0; i < move.Split().n_elem + 1; ++i)
+  {
+    BOOST_REQUIRE_EQUAL(move.Child(i).Label(), copy.Child(i).Label());
+    CheckMatrices(move.Child(i).Split(), copy.Child(i).Split());
+  }
 }
 
 BOOST_AUTO_TEST_SUITE_END();
diff --git a/src/mlpack/tests/serialization_test.cpp b/src/mlpack/tests/serialization_test.cpp
index fd9925b..2996f23 100644
--- a/src/mlpack/tests/serialization_test.cpp
+++ b/src/mlpack/tests/serialization_test.cpp
@@ -1274,8 +1274,12 @@ BOOST_AUTO_TEST_CASE(DecisionStumpTest)
   BOOST_REQUIRE_EQUAL(ds.SplitDimension(), binaryDs.SplitDimension());
 
   CheckMatrices(ds.Split(), xmlDs.Split(), textDs.Split(), binaryDs.Split());
-  CheckMatrices(ds.BinLabels(), xmlDs.BinLabels(), textDs.BinLabels(),
-      binaryDs.BinLabels());
+  for (size_t i = 0; i < ds.Split().n_elem; ++i)
+  {
+    BOOST_REQUIRE_EQUAL(ds.Child(i).Label(), xmlDs.Child(i).Label());
+    BOOST_REQUIRE_EQUAL(ds.Child(i).Label(), textDs.Child(i).Label());
+    BOOST_REQUIRE_EQUAL(ds.Child(i).Label(), binaryDs.Child(i).Label());
+  }
 }
 
 // Make sure serialization works for LARS.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list