Perform PCA with IRLBA.
More...
#include <cmath>
#include <utility>
#include <stdexcept>
#include "compute.hpp"
#include "Matrix/simple.hpp"
#include "Matrix/centered.hpp"
#include "Matrix/scaled.hpp"
#include "Eigen/Dense"
Go to the source code of this file.
|
| template<class InputEigenMatrix_ , class OutputEigenMatrix_ , class EigenVector_ > |
| std::pair< bool, int > | irlba::pca (const InputEigenMatrix_ &matrix, bool center, bool scale, Eigen::Index number, OutputEigenMatrix_ &scores, OutputEigenMatrix_ &rotation, EigenVector_ &variances, const Options< EigenVector_ > &options) |
| |
| template<class OutputEigenMatrix_ = Eigen::MatrixXd, class EigenVector_ = Eigen::VectorXd, class InputEigenMatrix_ > |
| PcaResults< OutputEigenMatrix_, EigenVector_ > | irlba::pca (const InputEigenMatrix_ &matrix, bool center, bool scale, Eigen::Index number, const Options< EigenVector_ > &options) |
| |
◆ pca() [1/2]
template<class OutputEigenMatrix_ = Eigen::MatrixXd, class EigenVector_ = Eigen::VectorXd, class InputEigenMatrix_ >
| PcaResults< OutputEigenMatrix_, EigenVector_ > irlba::pca |
( |
const InputEigenMatrix_ & | matrix, |
|
|
bool | center, |
|
|
bool | scale, |
|
|
Eigen::Index | number, |
|
|
const Options< EigenVector_ > & | options ) |
Convenient overload of pca() that allocates memory for the output matrices of the PCA.
- Template Parameters
-
| InputEigenMatrix_ | An Eigen matrix class containing the input data. |
| OutputEigenMatrix_ | A dense floating-point Eigen::Matrix class for the output. |
| EigenVector_ | A floating-point Eigen::Vector class for the output, typically of the same scalar type as EigenMatrix_. |
- Parameters
-
| [in] | matrix | Input matrix where rows are observations and columns are features. |
| center | Should the matrix be centered by column? This should be set to true if the matrix is not already centered. |
| scale | Should the matrix be scaled to unit variance for each column? |
| number | Number of singular triplets to obtain. |
| options | Further options. |
- Returns
- A
Results object containing the singular vectors and values, as well as some statistics on convergence.
◆ pca() [2/2]
template<class InputEigenMatrix_ , class OutputEigenMatrix_ , class EigenVector_ >
| std::pair< bool, int > irlba::pca |
( |
const InputEigenMatrix_ & | matrix, |
|
|
bool | center, |
|
|
bool | scale, |
|
|
Eigen::Index | number, |
|
|
OutputEigenMatrix_ & | scores, |
|
|
OutputEigenMatrix_ & | rotation, |
|
|
EigenVector_ & | variances, |
|
|
const Options< EigenVector_ > & | options ) |
Perform a principal components analysis (PCA) using IRLBA for the underlying SVD. This applies deferred centering and scaling via the CenteredMatrix and ScaledMatrix classes, respectively.
- Template Parameters
-
| InputEigenMatrix_ | An Eigen matrix class containing the input data. |
| OutputEigenMatrix_ | A dense floating-point Eigen::Matrix class for the output. |
| EigenVector_ | A floating-point Eigen::Vector class for the output, typically of the same scalar type as OutputEigenMatrix_. |
- Parameters
-
| [in] | matrix | Input matrix where rows are observations and columns are features. |
| center | Should the matrix be centered by column? This should be set to true if the matrix is not already centered. |
| scale | Should the matrix be scaled to unit variance for each column? |
| number | Number of principal components to obtain. |
| [out] | scores | Output matrix for the principal component scores. Each row corresponds to an observation while each column corresponds to a principal component. On output, the number of rows is equal to the number of columns in matrix, while the number of columns is equal to number (or less, if Options::cap_number is applied). |
| [out] | rotation | Output matrix in which to store the rotation matrix. Each row corresponds to an feature while each column corresponds to a principal component. On output, the number of rows is equal to the number of rows in matrix, while the number of columns is equal to number (or less, if Options::cap_number is applied). |
| [out] | variances | Vector to store the variance explained by each principal component. On output, the length of this vector is equal to number (or less, if Options::cap_number is applied). |
| options | Further options. |
- Returns
- A pair where the first entry indicates whether the algorithm converged, and the second entry indicates the number of restart iterations performed.
Centering is performed by subtracting each element of center from the corresponding column of mat. Scaling is performed by dividing each column of mat by the corresponding element of scale (after any centering has been applied). Note that scale=true requires center=true to guarantee unit variance along each column. No scaling is performed when the variance of a column is zero, so as to avoid divide-by-zero errors.