irlba
A C++ library for IRLBA
Loading...
Searching...
No Matches
pca.hpp File Reference

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"
Include dependency graph for pca.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  irlba::PcaResults< EigenMatrix_, EigenVector_ >
 Results of the IRLBA-based PCA by pca(). More...
 

Functions

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)
 

Detailed Description

Perform PCA with IRLBA.

Function Documentation

◆ 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]matrixInput matrix where rows are observations and columns are features.
centerShould the matrix be centered by column? This should be set to true if the matrix is not already centered.
scaleShould the matrix be scaled to unit variance for each column?
numberNumber of singular triplets to obtain.
optionsFurther 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]matrixInput matrix where rows are observations and columns are features.
centerShould the matrix be centered by column? This should be set to true if the matrix is not already centered.
scaleShould the matrix be scaled to unit variance for each column?
numberNumber of principal components to obtain.
[out]scoresOutput 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]rotationOutput 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]variancesVector 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).
optionsFurther 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.