[mlpack-git] master: Handle variance calculation with zero eigenvalues. (91d3bf2)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:00:03 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit 91d3bf23fe9a7848f3a9c355dd1c9bd0d28ffb64
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Thu Sep 11 17:24:46 2014 +0000

    Handle variance calculation with zero eigenvalues.


>---------------------------------------------------------------

91d3bf23fe9a7848f3a9c355dd1c9bd0d28ffb64
 src/mlpack/methods/pca/pca.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/mlpack/methods/pca/pca.cpp b/src/mlpack/methods/pca/pca.cpp
index 4e02737..8aaa53f 100644
--- a/src/mlpack/methods/pca/pca.cpp
+++ b/src/mlpack/methods/pca/pca.cpp
@@ -9,6 +9,7 @@
 #include <mlpack/core.hpp>
 #include <iostream>
 #include <complex>
+#include <algorithm>
 
 using namespace std;
 using namespace mlpack;
@@ -124,8 +125,12 @@ double PCA::Apply(arma::mat& data, const size_t newDimension) const
     // Drop unnecessary rows.
     data.shed_rows(newDimension, data.n_rows - 1);
 
+  // The svd method returns only non-zero eigenvalues so we have to calculate
+  // the right dimension before calculating the amount of variance retained.
+  double eigDim = std::min(newDimension - 1, (size_t) eigVal.n_elem - 1);
+
   // Calculate the total amount of variance retained.
-  return (sum(eigVal.subvec(0, newDimension - 1)) / sum(eigVal));
+  return (sum(eigVal.subvec(0, eigDim)) / sum(eigVal));
 }
 
 /**



More information about the mlpack-git mailing list