[mlpack-svn] r10717 - mlpack/trunk/src/mlpack/methods/kpca

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Dec 12 05:45:31 EST 2011


Author: ajinkya
Date: 2011-12-12 05:45:30 -0500 (Mon, 12 Dec 2011)
New Revision: 10717

Added:
   mlpack/trunk/src/mlpack/methods/kpca/kpca_impl.hpp
Modified:
   mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp
   mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp
Log:
got the implementation into hpp file to include it in the class def. This is need to make the templates work. Good article here which talk about why we need this http://www.comeaucomputing.com/techtalk/templates/#whylinkerror

Modified: mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt	2011-12-12 10:33:50 UTC (rev 10716)
+++ mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt	2011-12-12 10:45:30 UTC (rev 10717)
@@ -4,7 +4,7 @@
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
   kpca.hpp
-  kpca.cpp
+  kpca_impl.hpp
 )
 
 # Add directory name to sources.

Modified: mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp	2011-12-12 10:33:50 UTC (rev 10716)
+++ mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp	2011-12-12 10:45:30 UTC (rev 10717)
@@ -5,6 +5,8 @@
  * Defines the KPCA class to perform Kernel Principal Components Analysis on the
  * specified data set.
  */
+#ifndef __MLPACK_METHODS_KPCA_KPCA_HPP
+#define __MLPACK_METHODS_KPCA_KPCA_HPP
 
 #include <mlpack/core.hpp>
 #include <mlpack/core/kernels/linear_kernel.hpp>
@@ -16,7 +18,7 @@
 class KPCA
 {
  public:
-  KPCA(/*const KernelType kernel = KernelType(),*/
+  KPCA(const KernelType kernel = KernelType(),
        const bool centerData = true,
        const bool scaleData = false);
 
@@ -100,4 +102,7 @@
 }; // namespace kpca
 }; // namespace mlpack
 
+// Include implementation.
+#include "kpca_impl.hpp"
 
+#endif // __MLPACK_METHODS_KPCA_HPP

Copied: mlpack/trunk/src/mlpack/methods/kpca/kpca_impl.hpp (from rev 10709, mlpack/trunk/src/mlpack/methods/kpca/kpca.cpp)
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca_impl.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kpca/kpca_impl.hpp	2011-12-12 10:45:30 UTC (rev 10717)
@@ -0,0 +1,128 @@
+/**
+ * @file kpca.cpp
+ * @author Ajinkya Kale
+ *
+ * Implementation of KPCA class to perform Kernel Principal Components Analysis on the
+ * specified data set.
+ */
+#include "kpca.hpp"
+#include <iostream>
+
+using namespace std;
+namespace mlpack {
+namespace kpca {
+
+template <typename KernelType>
+KPCA<KernelType>::KPCA(const KernelType kernel,
+                       const bool centerData,
+                       const bool scaleData) :
+      kernel_(kernel),
+      centerData_(centerData),
+      scaleData_(scaleData)
+{
+}
+
+/**
+ * Apply Kernel Principal Component Analysis to the provided data set.
+ *
+ * @param data - Data matrix
+ * @param transformedData - Data with KPCA applied
+ * @param eigVal - contains eigen values in a column vector
+ * @param coeff - KPCA Loadings/Coeffs/EigenVectors
+ */
+template <typename KernelType>
+void KPCA<KernelType>::Apply(const arma::mat& data, arma::mat& transformedData,
+           arma::vec& eigVal, arma::mat& coeffs)
+{
+  arma::mat transData = trans(data);
+
+  if(centerData_)
+  {
+    arma::rowvec means = arma::mean(transData, 0);
+    transData = transData - arma::ones<arma::colvec>(transData.n_rows) * means;
+    cout << "centering data" << endl;
+  }
+  transData.print("TRANSDATA");
+  arma::mat centeredData = trans(transData);
+
+  arma::mat kernelMat(centeredData.n_rows, centeredData.n_rows);
+
+  for(int i = 0; i < centeredData.n_rows; i++)
+  {
+    for(int j = 0; j < centeredData.n_rows; j++)
+    {
+          arma::vec v1 = trans(centeredData.row(i));
+          arma::vec v2 = trans(centeredData.row(j));
+         kernelMat(i, j) = kernel_.Evaluate(v1, v2);
+    }
+  }
+
+  kernelMat.print("KERNEL MATRIX : ");
+  arma::mat mat_cov = (cov(centeredData));
+  mat_cov.print("COV MATRIX : ");
+
+  transData = kernelMat; //use the kernel-matrix to do the transformations after this point.
+
+  if(scaleData_)
+  {
+    transData = transData / (arma::ones<arma::colvec>(transData.n_rows) * stddev(transData, 0, 0));
+  }
+
+  arma::mat covMat = cov(transData);
+  arma::eig_sym(eigVal, coeffs, covMat);
+
+  int n_eigVal = eigVal.n_elem;
+  for(int i = 0; i < floor(n_eigVal / 2); i++)
+    eigVal.swap_rows(i, (n_eigVal - 1) - i);
+
+  coeffs = arma::fliplr(coeffs);
+  transformedData = trans(coeffs) * data;
+  arma::colvec transformedDataMean = arma::mean(transformedData, 1);
+  transformedData = transformedData - (transformedDataMean * arma::ones<arma::rowvec>(transformedData.n_cols));
+}
+
+/**
+ * Apply Kernel Principal Component Analysis to the provided data set.
+ *
+ * @param data - Data matrix
+ * @param transformedData - Data with KPCA applied
+ * @param eigVal - contains eigen values in a column vector
+ */
+template <typename KernelType>
+void KPCA<KernelType>::Apply(const arma::mat& data, arma::mat& transformedData,
+           arma::vec& eigVal)
+{
+  arma::mat coeffs;
+  Apply(data, transformedData,
+              eigVal, coeffs);
+}
+
+/**
+ * Apply Dimensionality Reduction using Kernel Principal Component Analysis
+ * to the provided data set.
+ *
+ * @param data - M x N Data matrix
+ * @param newDimension - matrix consisting of N column vectors,
+ * where each vector is the projection of the corresponding data vector
+ * from data matrix onto the basis vectors contained in the columns of
+ * coeff/eigen vector matrix with only newDimension number of columns chosen.
+ */
+template <typename KernelType>
+void KPCA<KernelType>::Apply(arma::mat& data, const int newDimension)
+{
+  arma::mat coeffs;
+  arma::vec eigVal;
+
+  Apply(data, data, eigVal, coeffs);
+
+  if(newDimension < coeffs.n_rows && newDimension > 0)
+    data.shed_rows(newDimension, data.n_rows - 1);
+}
+
+/*template <typename KernelType>
+KPCA<KernelType>::~KPCA()
+{
+}*/
+
+}; // namespace mlpack
+}; // namespace kpca

Modified: mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp	2011-12-12 10:33:50 UTC (rev 10716)
+++ mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp	2011-12-12 10:45:30 UTC (rev 10717)
@@ -14,18 +14,25 @@
 int main(int argc, char** argv)
 {
 
-  mat data("1 0 2 3 9;"
+  /*mat data("1 0 2 3 9;"
             "5 2 8 4 8;"
-            "6 7 3 1 8");
+            "6 7 3 1 8");*/
+  mat data("1 2 3;"
+            "4 5 6;"
+            "7 8 9");
 
+  data.print("DATA : ");
+
    // Now run PCA to reduce the dimensionality.
-   kpca::KPCA<kernel::LinearKernel> p(true, false);
+   kpca::KPCA<kernel::LinearKernel> p;
    //p.CenterData();
-   //p.Apply(data, 2); // Reduce to 2 dimensions.
+   p.Apply(data, 2); // Reduce to 2 dimensions.
 
+   data.print("RESULT : ");
    // 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");
+   correct.print("CORRECT");
 
    // If the eigenvectors are pointed opposite directions, they will cancel
  // each other out in this summation.




More information about the mlpack-svn mailing list