[mlpack-svn] r10709 - in 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 04:00:29 EST 2011


Author: ajinkya
Date: 2011-12-12 04:00:28 -0500 (Mon, 12 Dec 2011)
New Revision: 10709

Added:
   mlpack/trunk/src/mlpack/methods/kpca/
   mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/kpca/kpca.cpp
   mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp
   mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp
Log:
and i present you <Kernel PCA>

Added: mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kpca/CMakeLists.txt	2011-12-12 09:00:28 UTC (rev 10709)
@@ -0,0 +1,26 @@
+cmake_minimum_required(VERSION 2.8)
+
+# Define the files we need to compile
+# Anything not in this list will not be compiled into MLPACK.
+set(SOURCES
+  kpca.hpp
+  kpca.cpp
+)
+
+# Add directory name to sources.
+set(DIR_SRCS)
+foreach(file ${SOURCES})
+  set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+endforeach()
+# Append sources (with directory name) to list of all MLPACK sources (used at
+# the parent scope).
+set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
+
+add_executable(kpca
+  kpca_main.cpp
+)
+
+target_link_libraries(kpca
+  mlpack
+)
+

Added: mlpack/trunk/src/mlpack/methods/kpca/kpca.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kpca/kpca.cpp	2011-12-12 09:00:28 UTC (rev 10709)
@@ -0,0 +1,120 @@
+/**
+ * @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 <mlpack/core.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;
+  }
+
+  arma::mat kernelMat;
+
+  for(int i = 0; i < transData.n_rows; i++)
+  {
+    for(int j = 0; j < transData.n_cols; j++)
+    {
+      kernelMat(i, j) = kernel_.Evaluate(transData.row(i), transData.col(i));
+    }
+  }
+
+  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

Added: mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kpca/kpca.hpp	2011-12-12 09:00:28 UTC (rev 10709)
@@ -0,0 +1,103 @@
+/**
+ * @file kpca.hpp
+ * @author Ajinkya Kale
+ *
+ * Defines the KPCA class to perform Kernel Principal Components Analysis on the
+ * specified data set.
+ */
+
+#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
+
+

Added: mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/methods/kpca/kpca_main.cpp	2011-12-12 09:00:28 UTC (rev 10709)
@@ -0,0 +1,40 @@
+
+#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");
+
+   // Now run PCA to reduce the dimensionality.
+   kpca::KPCA<kernel::LinearKernel> p(true, false);
+   //p.CenterData();
+   //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;
+     }
+   }
+}




More information about the mlpack-svn mailing list