exact_svd_method.hpp
Go to the documentation of this file.
1 
16 #ifndef MLPACK_METHODS_PCA_DECOMPOSITION_POLICIES_EXACT_SVD_METHOD_HPP
17 #define MLPACK_METHODS_PCA_DECOMPOSITION_POLICIES_EXACT_SVD_METHOD_HPP
18 
19 #include <mlpack/prereqs.hpp>
20 
21 namespace mlpack {
22 namespace pca {
23 
28 {
29  public:
41  void Apply(const arma::mat& data,
42  const arma::mat& centeredData,
43  arma::mat& transformedData,
44  arma::vec& eigVal,
45  arma::mat& eigvec,
46  const size_t /* rank */)
47  {
48  // This matrix will store the right singular values; we do not need them.
49  arma::mat v;
50 
51  // Do singular value decomposition. Use the economical singular value
52  // decomposition if the columns are much larger than the rows.
53  if (data.n_rows < data.n_cols)
54  {
55  // Do economical singular value decomposition and compute only the left
56  // singular vectors.
57  arma::svd_econ(eigvec, eigVal, v, centeredData, 'l');
58  }
59  else
60  {
61  arma::svd(eigvec, eigVal, v, centeredData);
62  }
63 
64  // Now we must square the singular values to get the eigenvalues.
65  // In addition we must divide by the number of points, because the
66  // covariance matrix is X * X' / (N - 1).
67  eigVal %= eigVal / (data.n_cols - 1);
68 
69  // Project the samples to the principals.
70  transformedData = arma::trans(eigvec) * centeredData;
71  }
72 };
73 
74 } // namespace pca
75 } // namespace mlpack
76 
77 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
Implementation of the exact SVD policy.
void Apply(const arma::mat &data, const arma::mat &centeredData, arma::mat &transformedData, arma::vec &eigVal, arma::mat &eigvec, const size_t)
Apply Principal Component Analysis to the provided data set using the exact SVD method.