[mlpack-git] master: Center the reconstructed approximation and the kernel matrix. (757886b)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:57:50 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 757886b84808a282146ac4a63df1f223916d1a71
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date: Fri Aug 15 16:52:11 2014 +0000
Center the reconstructed approximation and the kernel matrix.
>---------------------------------------------------------------
757886b84808a282146ac4a63df1f223916d1a71
.../kernel_pca/kernel_rules/nystroem_method.hpp | 24 ++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/src/mlpack/methods/kernel_pca/kernel_rules/nystroem_method.hpp b/src/mlpack/methods/kernel_pca/kernel_rules/nystroem_method.hpp
index 34bcf62..044db4e 100644
--- a/src/mlpack/methods/kernel_pca/kernel_rules/nystroem_method.hpp
+++ b/src/mlpack/methods/kernel_pca/kernel_rules/nystroem_method.hpp
@@ -45,21 +45,29 @@ class NystroemKernelRule
nm.Apply(G);
transformedData = G.t() * G;
+ // Center the reconstructed approximation.
+ math::Center(transformedData, transformedData);
+
// 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.
- arma::rowvec rowMean = arma::sum(transformedData, 0) /
- transformedData.n_cols;
- transformedData.each_col() -= arma::sum(transformedData, 1) /
- transformedData.n_cols;
- transformedData.each_row() -= rowMean;
- transformedData += arma::sum(rowMean) / transformedData.n_cols;
+ arma::colvec colMean = arma::sum(G, 1) / G.n_rows;
+ G.each_row() -= arma::sum(G, 0) / G.n_rows;
+ G.each_col() -= colMean;
+ G += arma::sum(colMean) / G.n_rows;
// Eigendecompose the centered kernel matrix.
- arma::svd(eigvec, eigval, v, transformedData);
- eigval %= eigval / (data.n_cols - 1);
+ arma::eig_sym(eigval, eigvec, transformedData);
+
+ // Swap the eigenvalues since they are ordered backwards (we need largest
+ // to smallest).
+ for (size_t i = 0; i < floor(eigval.n_elem / 2.0); ++i)
+ eigval.swap_rows(i, (eigval.n_elem - 1) - i);
+
+ // Flip the coefficients to produce the same effect.
+ eigvec = arma::fliplr(eigvec);
transformedData = eigvec.t() * G.t();
}
More information about the mlpack-git
mailing list