[mlpack] 01/149: Handle variance calculation with zero eigenvalues.

Barak A. Pearlmutter barak+git at pearlmutter.net
Sat May 2 09:11:03 UTC 2015


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch svn-trunk
in repository mlpack.

commit 68e46ffb8f0f3d4754fc4abd2f72ffe0fa3b723d
Author: marcus <marcus at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date:   Thu Sep 11 17:24:46 2014 +0000

    Handle variance calculation with zero eigenvalues.
    
    git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@17174 9d5b8971-822b-0410-80eb-d18c1038ef23
---
 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));
 }
 
 /**

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list