mlpack 3.4.2
randomized_svd_method.hpp
Go to the documentation of this file.
1
14#ifndef MLPACK_METHODS_PCA_DECOMPOSITION_POLICIES_RANDOMIZED_SVD_METHOD_HPP
15#define MLPACK_METHODS_PCA_DECOMPOSITION_POLICIES_RANDOMIZED_SVD_METHOD_HPP
16
17#include <mlpack/prereqs.hpp>
19
20namespace mlpack {
21namespace pca {
22
27{
28 public:
38 RandomizedSVDPolicy(const size_t iteratedPower = 0,
39 const size_t maxIterations = 2) :
40 iteratedPower(iteratedPower),
41 maxIterations(maxIterations)
42 {
43 /* Nothing to do here */
44 }
45
57 void Apply(const arma::mat& data,
58 const arma::mat& centeredData,
59 arma::mat& transformedData,
60 arma::vec& eigVal,
61 arma::mat& eigvec,
62 const size_t rank)
63 {
64 // This matrix will store the right singular values; we do not need them.
65 arma::mat v;
66
67 // Do singular value decomposition using the randomized SVD algorithm.
68 svd::RandomizedSVD rsvd(iteratedPower, maxIterations);
69 rsvd.Apply(data, eigvec, eigVal, v, rank);
70
71 // Now we must square the singular values to get the eigenvalues.
72 // In addition we must divide by the number of points, because the
73 // covariance matrix is X * X' / (N - 1).
74 eigVal %= eigVal / (data.n_cols - 1);
75
76 // Project the samples to the principals.
77 transformedData = arma::trans(eigvec) * centeredData;
78 }
79
81 size_t IteratedPower() const { return iteratedPower; }
83 size_t& IteratedPower() { return iteratedPower; }
84
86 size_t MaxIterations() const { return maxIterations; }
88 size_t& MaxIterations() { return maxIterations; }
89
90 private:
92 size_t iteratedPower;
93
95 size_t maxIterations;
96};
97
98} // namespace pca
99} // namespace mlpack
100
101#endif
Implementation of the randomized SVD policy.
size_t MaxIterations() const
Get the number of iterations for the power method.
void Apply(const arma::mat &data, const arma::mat &centeredData, arma::mat &transformedData, arma::vec &eigVal, arma::mat &eigvec, const size_t rank)
Apply Principal Component Analysis to the provided data set using the randomized SVD.
size_t & MaxIterations()
Modify the number of iterations for the power method.
RandomizedSVDPolicy(const size_t iteratedPower=0, const size_t maxIterations=2)
Use randomized SVD method to perform the principal components analysis (PCA).
size_t IteratedPower() const
Get the size of the normalized power iterations.
size_t & IteratedPower()
Modify the size of the normalized power iterations.
Randomized SVD is a matrix factorization that is based on randomized matrix approximation techniques,...
void Apply(const arma::sp_mat &data, arma::mat &u, arma::vec &s, arma::mat &v, const size_t rank)
Center the data to apply Principal Component Analysis on given sparse matrix dataset using randomized...
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.