[mlpack-svn] r10803 - in mlpack/trunk/src/mlpack/methods: . kernel_pca

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Dec 14 15:08:36 EST 2011


Author: rcurtin
Date: 2011-12-14 15:08:36 -0500 (Wed, 14 Dec 2011)
New Revision: 10803

Added:
   mlpack/trunk/src/mlpack/methods/kernel_pca/
   mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp
   mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp
   mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_main.cpp
Removed:
   mlpack/trunk/src/mlpack/methods/kernel_pca/kpca.hpp
   mlpack/trunk/src/mlpack/methods/kernel_pca/kpca_impl.hpp
   mlpack/trunk/src/mlpack/methods/kernel_pca/kpca_main.cpp
   mlpack/trunk/src/mlpack/methods/kpca/
Modified:
   mlpack/trunk/src/mlpack/methods/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/kernel_pca/CMakeLists.txt
Log:
Move kpca to kernel_pca; clean up the code to match formatting standards (okay,
some of my changes were superfluous), use size_t instead of int where necessary,
and change CMake configuration so kernel_pca is compiled.


Modified: mlpack/trunk/src/mlpack/methods/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/CMakeLists.txt	2011-12-14 19:47:09 UTC (rev 10802)
+++ mlpack/trunk/src/mlpack/methods/CMakeLists.txt	2011-12-14 20:08:36 UTC (rev 10803)
@@ -5,6 +5,7 @@
   emst
   gmm
   hmm
+  kernel_pca
   kmeans
   lars
   linear_regression

Modified: mlpack/trunk/src/mlpack/methods/kernel_pca/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt	2011-12-13 07:23:09 UTC (rev 10737)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/CMakeLists.txt	2011-12-14 20:08:36 UTC (rev 10803)
@@ -3,8 +3,8 @@
 # Define the files we need to compile
 # Anything not in this list will not be compiled into MLPACK.
 set(SOURCES
-  kpca.hpp
-  kpca_impl.hpp
+  kernel_pca.hpp
+  kernel_pca_impl.hpp
 )
 
 # Add directory name to sources.
@@ -16,11 +16,11 @@
 # the parent scope).
 set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
 
-add_executable(kpca
-  kpca_main.cpp
+add_executable(kernel_pca
+  kernel_pca_main.cpp
 )
 
-target_link_libraries(kpca
+target_link_libraries(kernel_pca
   mlpack
 )
 

Copied: mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp (from rev 10737, mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp)
===================================================================
--- mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca.hpp	2011-12-14 20:08:36 UTC (rev 10803)
@@ -0,0 +1,97 @@
+/**
+ * @file kernel_pca.hpp
+ * @author Ajinkya Kale
+ *
+ * Defines the KernelPCA class to perform Kernel Principal Components Analysis
+ * on the specified data set.
+ */
+#ifndef __MLPACK_METHODS_KERNEL_PCA_KERNEL_PCA_HPP
+#define __MLPACK_METHODS_KERNEL_PCA_KERNEL_PCA_HPP
+
+#include <mlpack/core.hpp>
+#include <mlpack/core/kernels/linear_kernel.hpp>
+
+namespace mlpack {
+namespace kpca {
+
+template <typename KernelType>
+class KernelPCA
+{
+ public:
+  KernelPCA(const KernelType kernel = KernelType(),
+            const bool centerData = true,
+            const bool scaleData = false);
+
+  /**
+   * Apply Kernel Principal Component Analysis to the provided data set.
+   *
+   * @param data - Data matrix
+   * @param transformedData - Data with PCA applied
+   * @param eigVal - contains eigen values in a column vector
+   * @param coeff - PCA Loadings/Coeffs/EigenVectors
+   */
+  void Apply(const arma::mat& data,
+             arma::mat& transformedData,
+             arma::vec& eigVal,
+             arma::mat& coeff);
+
+  /**
+   * Apply Kernel Principal Component Analysis to the provided data set.
+   *
+   * @param data - Data matrix
+   * @param transformedData - Data with PCA applied
+   * @param eigVal - contains eigen values in a column vector
+   */
+  void Apply(const arma::mat& data,
+             arma::mat& transformedData,
+             arma::vec& eigVal);
+
+  /**
+   * 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.
+   */
+  void Apply(arma::mat& data, const size_t newDimension);
+
+  //! Get the kernel.
+  const KernelType& Kernel() const { return kernel; }
+  //! Modify the kernel.
+  KernelType& Kernel() { return kernel; }
+
+  //! Return whether or not this KernelPCA object will center the data when
+  //! kernel PCA is performed.
+  bool CenterData() const { return centerData; }
+  //! Modify whether or not this KernelPCA object will center the data when
+  //! kernel PCA is performed.
+  bool& CenterData() { return centerData; }
+
+  //! Return whether or not this KernelPCA object will scale (by standard
+  //! deviation) the data when kernel PCA is performed.
+  bool ScaleData() const { return scaleData; }
+  //! Modify whether or not this KernelPCA object will scale (by standard
+  //! deviation) the data when kernel PCA is performed.
+  bool& ScaleData() { return scaleData; }
+
+ private:
+  //! The instantiated kernel.
+  KernelType kernel;
+  //! If true, the data will be centered when Apply() is run.
+  bool centerData;
+  //! If true, the data will be scaled (by standard deviation) when Apply() is
+  //! run.
+  bool scaleData;
+
+}; // class KernelPCA
+
+}; // namespace kpca
+}; // namespace mlpack
+
+// Include implementation.
+#include "kernel_pca_impl.hpp"
+
+#endif // __MLPACK_METHODS_KERNEL_PCA_KERNEL_PCA_HPP

Copied: mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp (from rev 10737, mlpack/trunk/src/mlpack/methods/kpca/kpca_impl.hpp)
===================================================================
--- mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp	2011-12-14 20:08:36 UTC (rev 10803)
@@ -0,0 +1,137 @@
+/**
+ * @file kernelpca_impl.hpp
+ * @author Ajinkya Kale
+ *
+ * Implementation of KernelPCA class to perform Kernel Principal Components
+ * Analysis on the specified data set.
+ */
+#ifndef __MLPACK_METHODS_KERNEL_PCA_KERNEL_PCA_IMPL_HPP
+#define __MLPACK_METHODS_KERNEL_PCA_KERNEL_PCA_IMPL_HPP
+
+// In case it hasn't already been included.
+#include "kernel_pca.hpp"
+
+#include <iostream>
+
+using namespace std; // This'll have to go before the release.
+
+namespace mlpack {
+namespace kpca {
+
+template <typename KernelType>
+KernelPCA<KernelType>::KernelPCA(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 KernelPCA applied
+ * @param eigVal - contains eigen values in a column vector
+ * @param coeff - KernelPCA Loadings/Coeffs/EigenVectors
+ */
+template <typename KernelType>
+void KernelPCA<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 KernelPCA applied
+ * @param eigVal - contains eigen values in a column vector
+ */
+template <typename KernelType>
+void KernelPCA<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 KernelPCA<KernelType>::Apply(arma::mat& data, const size_t 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);
+}
+
+}; // namespace mlpack
+}; // namespace kpca
+
+#endif

Copied: mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_main.cpp (from rev 10737, mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp)
===================================================================
--- mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_main.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_main.cpp	2011-12-14 20:08:36 UTC (rev 10803)
@@ -0,0 +1,50 @@
+/**
+ * @file kernel_pca_main.cpp
+ * @author Ajinkya Kale <kaleajinkya at gmail.com>
+ *
+ * Executable for Kernel PCA.
+ */
+#include <mlpack/core.hpp>
+#include <mlpack/core/kernels/linear_kernel.hpp>
+
+#include "kernel_pca.hpp"
+
+using namespace mlpack;
+using namespace mlpack::kpca;
+using namespace std;
+using namespace arma;
+
+int main(int argc, char** argv)
+{
+
+  /*mat data("1 0 2 3 9;"
+            "5 2 8 4 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::KernelPCA<kernel::LinearKernel> p;
+   //p.CenterData();
+   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.
+   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;
+     }
+   }
+}

Deleted: mlpack/trunk/src/mlpack/methods/kernel_pca/kpca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp	2011-12-13 07:23:09 UTC (rev 10737)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kpca.hpp	2011-12-14 20:08:36 UTC (rev 10803)
@@ -1,108 +0,0 @@
-/**
- * @file kpca.hpp
- * @author Ajinkya Kale
- *
- * 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>
-
-namespace mlpack {
-namespace kpca {
-
-template <typename KernelType>
-class KPCA
-{
- public:
-  KPCA(const KernelType kernel = KernelType(),
-       const bool centerData = true,
-       const bool scaleData = false);
-
-  /* Return whether or not this KPCA object will center the data when KPCA
-   *  is performed.
-   */
-  bool CenterData() const
-  {
-    return centerData_;
-  }
-
-  /* Modify whether or not this KPCA object will center the data when KPCA
-   * is performed.
-   */
-  bool& CenterData()
-  {
-    return centerData_;
-  }
-
-  /* Return whether or not this KPCA object will scale(by standard deviation) the data when KPCA
-   *  is performed.
-   */
-  bool ScaleData() const
-  {
-    return scaleData_;
-  }
-
-  /* Modify whether or not this KPCA object will scale(by standard deviation) the data when KPCA
-   * is performed.
-   */
-  bool& ScaleData()
-  {
-    return scaleData_;
-  }
-
-  /**
-   * Apply Kernel Principal Component Analysis to the provided data set.
-   *
-   * @param data - Data matrix
-   * @param transformedData - Data with PCA applied
-   * @param eigVal - contains eigen values in a column vector
-   * @param coeff - PCA Loadings/Coeffs/EigenVectors
-   */
-  void Apply(const arma::mat& data, arma::mat& transformedData, arma::vec&
-             eigVal, arma::mat& coeff);
-
-  /**
-   * Apply Kernel Principal Component Analysis to the provided data set.
-   *
-   * @param data - Data matrix
-   * @param transformedData - Data with PCA applied
-   * @param eigVal - contains eigen values in a column vector
-   */
-  void Apply(const arma::mat& data, arma::mat& transformedData,
-             arma::vec& eigVal);
-
-  /**
-   * 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.
-   */
-  void Apply(arma::mat& data, const int newDimension);
-
-  /*
-   * Delete KPCA object
-   */
-  //~KPCA();
-
- private:
-   bool centerData_;
-   bool scaleData_;
-   KernelType kernel_;
-
-}; // class KPCA
-
-}; // namespace kpca
-}; // namespace mlpack
-
-// Include implementation.
-#include "kpca_impl.hpp"
-
-#endif // __MLPACK_METHODS_KPCA_HPP

Deleted: mlpack/trunk/src/mlpack/methods/kernel_pca/kpca_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca_impl.hpp	2011-12-13 07:23:09 UTC (rev 10737)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kpca_impl.hpp	2011-12-14 20:08:36 UTC (rev 10803)
@@ -1,128 +0,0 @@
-/**
- * @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

Deleted: mlpack/trunk/src/mlpack/methods/kernel_pca/kpca_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp	2011-12-13 07:23:09 UTC (rev 10737)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kpca_main.cpp	2011-12-14 20:08:36 UTC (rev 10803)
@@ -1,47 +0,0 @@
-
-#include <mlpack/core.hpp>
-#include <mlpack/core/kernels/linear_kernel.hpp>
-
-#include "kpca.hpp"
-
-using namespace mlpack;
-using namespace std;
-using namespace arma;
-using namespace kpca;
-
-
-
-int main(int argc, char** argv)
-{
-
-  /*mat data("1 0 2 3 9;"
-            "5 2 8 4 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;
-   //p.CenterData();
-   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.
-   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;
-     }
-   }
-}




More information about the mlpack-svn mailing list