[mlpack-svn] r15362 - mlpack/trunk/src/mlpack/tests

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Jun 28 15:53:22 EDT 2013


Author: rcurtin
Date: Fri Jun 28 15:53:21 2013
New Revision: 15362

Log:
Test label normalization.


Modified:
   mlpack/trunk/src/mlpack/tests/load_save_test.cpp

Modified: mlpack/trunk/src/mlpack/tests/load_save_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/load_save_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/load_save_test.cpp	Fri Jun 28 15:53:21 2013
@@ -6,8 +6,7 @@
  */
 #include <sstream>
 
-#include <mlpack/core/data/load.hpp>
-#include <mlpack/core/data/save.hpp>
+#include <mlpack/core.hpp>
 
 #include <boost/test/unit_test.hpp>
 #include "old_boost_test_definitions.hpp"
@@ -508,4 +507,60 @@
 }
 #endif
 
+/**
+ * Test normalization of labels.
+ */
+BOOST_AUTO_TEST_CASE(NormalizeLabelSmallDatasetTest)
+{
+  arma::ivec labels("-1 1 1 -1 -1 -1 1 1");
+  arma::uvec newLabels;
+  arma::ivec mappings;
+
+  data::NormalizeLabels(labels, newLabels, mappings);
+
+  BOOST_REQUIRE_EQUAL(mappings[0], -1);
+  BOOST_REQUIRE_EQUAL(mappings[1], 1);
+
+  BOOST_REQUIRE_EQUAL(newLabels[0], 0);
+  BOOST_REQUIRE_EQUAL(newLabels[1], 1);
+  BOOST_REQUIRE_EQUAL(newLabels[2], 1);
+  BOOST_REQUIRE_EQUAL(newLabels[3], 0);
+  BOOST_REQUIRE_EQUAL(newLabels[4], 0);
+  BOOST_REQUIRE_EQUAL(newLabels[5], 0);
+  BOOST_REQUIRE_EQUAL(newLabels[6], 1);
+  BOOST_REQUIRE_EQUAL(newLabels[7], 1);
+
+  arma::ivec revertedLabels;
+
+  data::RevertLabels(newLabels, mappings, revertedLabels);
+
+  for (size_t i = 0; i < labels.n_elem; ++i)
+    BOOST_REQUIRE_EQUAL(labels[i], revertedLabels[i]);
+}
+
+/**
+ * Harder label normalization test.
+ */
+BOOST_AUTO_TEST_CASE(NormalizeLabelTest)
+{
+  arma::vec randLabels(5000);
+  for (size_t i = 0; i < 5000; ++i)
+    randLabels[i] = math::RandInt(-50, 50);
+  randLabels[0] = 0.65; // Hey, doubles work too!
+
+  arma::uvec newLabels;
+  arma::vec mappings;
+
+  data::NormalizeLabels(randLabels, newLabels, mappings);
+
+  Log::Warn << mappings.t();
+
+  // Now map them back and ensure they are right.
+  arma::vec revertedLabels(5000);
+  data::RevertLabels(newLabels, mappings, revertedLabels);
+
+  for (size_t i = 0; i < 5000; ++i)
+    BOOST_REQUIRE_EQUAL(randLabels[i], revertedLabels[i]);
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-svn mailing list