[mlpack-svn] r10921 - mlpack/trunk/src/mlpack/tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Dec 21 12:16:54 EST 2011
Author: ajinkya
Date: 2011-12-21 12:16:54 -0500 (Wed, 21 Dec 2011)
New Revision: 10921
Added:
mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp
Modified:
mlpack/trunk/src/mlpack/tests/CMakeLists.txt
Log:
kernel pca test file
Modified: mlpack/trunk/src/mlpack/tests/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/tests/CMakeLists.txt 2011-12-21 16:38:06 UTC (rev 10920)
+++ mlpack/trunk/src/mlpack/tests/CMakeLists.txt 2011-12-21 17:16:54 UTC (rev 10921)
@@ -11,6 +11,7 @@
gmm_test.cpp
hmm_test.cpp
kernel_test.cpp
+ kernel_pca_test.cpp
kmeans_test.cpp
lars_test.cpp
lin_alg_test.cpp
Added: mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp (rev 0)
+++ mlpack/trunk/src/mlpack/tests/kernel_pca_test.cpp 2011-12-21 17:16:54 UTC (rev 10921)
@@ -0,0 +1,50 @@
+/**
+ * @file kernel_pca_test.cpp
+ * @author Ajinkya Kale <kaleajinkya at gmail.com>
+ *
+ * Test file for Kernel PCA.
+ */
+#include <mlpack/core.hpp>
+#include <mlpack/core/kernels/linear_kernel.hpp>
+#include <mlpack/methods/kernel_pca/kernel_pca.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_SUITE(KernelPCATest);
+
+using namespace mlpack;
+using namespace mlpack::kpca;
+using namespace std;
+using namespace arma;
+
+BOOST_AUTO_TEST_CASE(linear_kernel)
+{
+
+ mat data("1 0 2 3 9;"
+ "5 2 8 4 8;"
+ "6 7 3 1 8");
+
+ kpca::KernelPCA<kernel::LinearKernel> p;
+ 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");
+
+ // 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++)
+ {
+ if (fabs(correct(i, 1) + data(i,1)) < 0.001 /* arbitrary */)
+ {
+ // Flip Armadillo coefficients for this column.
+ data.row(i) *= -1;
+ }
+ }
+
+ 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);
+}
+
+BOOST_AUTO_TEST_SUITE_END();
More information about the mlpack-svn
mailing list