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

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Sep 11 17:22:19 EDT 2013


Author: rcurtin
Date: Wed Sep 11 17:22:18 2013
New Revision: 15762

Log:
Add test functions for new stuff from Sumedh.


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

Modified: mlpack/trunk/src/mlpack/tests/pca_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/pca_test.cpp	(original)
+++ mlpack/trunk/src/mlpack/tests/pca_test.cpp	Wed Sep 11 17:22:18 2013
@@ -57,12 +57,15 @@
 
   // Now run PCA to reduce the dimensionality.
   PCA p;
-  p.Apply(data, 2); // Reduce to 2 dimensions.
+  const double varRetained = p.Apply(data, 2); // Reduce to 2 dimensions.
 
   // 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);
+
   // If the eigenvectors are pointed opposite directions, they will cancel
   // each other out in this summation.
   for (size_t i = 0; i < data.n_rows; i++)
@@ -77,6 +80,73 @@
   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);
+
+  // Check that the amount of variance retained is right.
+  BOOST_REQUIRE_CLOSE(varRetained, 0.904876047045906, 1e-5);
+}
+
+/**
+ * Test that setting the variance retained parameter to perform dimensionality
+ * reduction works.
+ */
+BOOST_AUTO_TEST_CASE(PCAVarianceRetainedTest)
+{
+  // Fake, simple dataset.
+  mat data("1 0 2 3 9;"
+           "5 2 8 4 8;"
+           "6 7 3 1 8");
+
+  // The normalized eigenvalues:
+  //   0.616237391936100
+  //   0.288638655109805
+  //   0.095123952954094
+  // So if we keep one dimension, the actual variance retained is
+  //   0.616237391936100
+  // and if we keep two, the actual variance retained is
+  //   0.904876047045906
+  // and if we keep three, the actual variance retained is 1.
+  PCA p;
+  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);
+
+  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);
+
+  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);
+
+  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);
+
+  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);
+
+  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);
 }
 
 /**
@@ -107,7 +177,7 @@
   // 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.2); // 20% tolerance.
+  BOOST_REQUIRE_CLOSE(std::abs(eigvec(0, 0)), sqrt(2) / 2, 0.2);
   BOOST_REQUIRE_CLOSE(std::abs(eigvec(1, 0)), sqrt(2) / 2, 0.2);
   BOOST_REQUIRE_SMALL(eigvec(2, 0), 0.08); // Large tolerance for noise.
 



More information about the mlpack-svn mailing list