[mlpack-svn] r15863 - mlpack/trunk/src/mlpack/methods/kernel_pca

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Sep 30 12:27:17 EDT 2013


Author: rcurtin
Date: Mon Sep 30 12:27:16 2013
New Revision: 15863

Log:
Because we now require Armadillo 3.6.0, we can use each_row() and each_col()
(thanks Marcus for the reminder).


Modified:
   mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp

Modified: mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/kernel_pca/kernel_pca_impl.hpp	Mon Sep 30 12:27:16 2013
@@ -37,35 +37,17 @@
   // Construct the kernel matrix.
   arma::mat kernelMatrix;
   GetKernelMatrix(data, kernelMatrix);
-  
-  // Reminder: Use the each_row() and the each_row functions to center the
-  // kernel matrix. This is faster as the current version but requires
-  // Armadillo version 3.4.
-//  arma::rowvec rowMean = arma::sum(kernelMatrix, 0) / kernelMatrix.n_cols;
-//  kernelMatrix.each_row() -= rowMean;
-//  kernelMatrix.each_col() -= arma::sum(kernelMatrix, 1) / kernelMatrix.n_cols;
-//  kernelMatrix += arma::sum(rowMean) / kernelMatrix.n_cols;
-  
+
   // For PCA the data has to be centered, even if the data is centered.  But it
   // is not guaranteed that the data, when mapped to the kernel space, is also
   // centered. Since we actually never work in the feature space we cannot
   // center the data. So, we perform a "psuedo-centering" using the kernel
   // matrix.
-  // Get the mean of the elements in each row.
   arma::rowvec rowMean = arma::sum(kernelMatrix, 0) / kernelMatrix.n_cols;
-  
-  // Get the mean of the elements in each col.
-  arma::colvec colMean = arma::sum(kernelMatrix, 1) / kernelMatrix.n_cols;
-  
-  // Center the kernel matrix.
-  for (size_t i = 0; i < kernelMatrix.n_rows; ++i)
-    kernelMatrix.row(i) -= rowMean;
-  
-  for (size_t i = 0; i < kernelMatrix.n_cols; ++i)
-    kernelMatrix.col(i) -= colMean;
-  
+  kernelMatrix.each_row() -= rowMean;
+  kernelMatrix.each_col() -= arma::sum(kernelMatrix, 1) / kernelMatrix.n_cols;
   kernelMatrix += arma::sum(rowMean) / kernelMatrix.n_cols;
-  
+
   // Eigendecompose the centered kernel matrix.
   arma::eig_sym(eigval, eigvec, kernelMatrix);
 



More information about the mlpack-svn mailing list