eos 1.4.0
Loading...
Searching...
No Matches
Enumerations | Functions
eos::pca Namespace Reference

PCA and functionality to build statistical models. More...

Enumerations

enum class  Covariance { AtA , AAt }
 

Functions

std::pair< Eigen::MatrixXf, Eigen::VectorXf > pca (const Eigen::Ref< const Eigen::MatrixXf > data, Covariance covariance_type=Covariance::AtA)
 Compute PCA on a mean-centred data matrix, and return the eigenvectors and respective eigenvalues.
 
std::pair< Eigen::MatrixXf, Eigen::VectorXf > pca (const Eigen::Ref< const Eigen::MatrixXf > data, int num_eigenvectors_to_keep, Covariance covariance_type=Covariance::AtA)
 Performs PCA and returns num_eigenvectors_to_keep eigenvectors and eigenvalues.
 
std::pair< Eigen::MatrixXf, Eigen::VectorXf > pca (const Eigen::Ref< const Eigen::MatrixXf > data, float variance_to_keep, Covariance covariance_type=Covariance::AtA)
 Performs PCA and returns the number of eigenvectors and eigenvalues to retain variance_to_keep variance of the original data.
 
morphablemodel::PcaModel pca (const Eigen::Ref< const Eigen::MatrixXf > data, std::vector< std::array< int, 3 > > triangle_list, Covariance covariance_type=Covariance::AtA)
 Performs PCA on the given data (including subtracting the mean) and returns the built PcaModel.
 

Detailed Description

PCA and functionality to build statistical models.

Enumeration Type Documentation

◆ Covariance

enum class eos::pca::Covariance
strong

A flag specifying how to compute the covariance matrix in the PCA.

Enumerator
AtA 

Compute the traditional covariance matrix A^t*A.

AAt 

Use the inner product, A*A^t, for the covariance matrix.

Function Documentation

◆ pca() [1/4]

std::pair< Eigen::MatrixXf, Eigen::VectorXf > eos::pca::pca ( const Eigen::Ref< const Eigen::MatrixXf >  data,
Covariance  covariance_type = Covariance::AtA 
)
inline

Compute PCA on a mean-centred data matrix, and return the eigenvectors and respective eigenvalues.

Computes PCA (principal component analysis) on the given mean-centred data matrix. Note that you should subtract the mean from the data beforehand, this function will not do so. The function computes PCA based on an eigendecomposition of the covariance matrix. If the dimensionality of the data is high, the PCA can be computed on the inner-product matrix A*A^t instead of the covariance matrix A^t*A by setting the flag Covariance::AAt.

The function returns n-1 eigenvectors and eigenvalues, where n is the number of data samples given. The eigenvectors and eigenvalues are returned in descending order, with the largest (most significant) first.

Note: Changing the covariance_type may return eigenvectors with different signs, but otherwise equivalent. This is completely fine as the sign of eigenvectors is arbitrary anyway.

Developer notes: If you want to avoid a copy: myarray = np.array(source, order='F') (this will change the numpy array to colmajor, so Eigen can directly accept it. There is other ways how to avoid copies: See: https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html. http://pybind11.readthedocs.io/en/master/advanced/cast/eigen.html Also it would be nice if the function could accept any Eigen matrix types (e.g. a MatrixXf or MatrixXd).

Parameters
[in]dataMean-free data matrix, with each row being a training sample.
[in]covariance_typeSpecifies whether PCA is computed on the covariance matrix AtA (default) or the inner-product matrix AAt.
Returns
A pair containing the matrix of eigenvectors and a vector with the respective eigenvalues.

◆ pca() [2/4]

std::pair< Eigen::MatrixXf, Eigen::VectorXf > eos::pca::pca ( const Eigen::Ref< const Eigen::MatrixXf >  data,
float  variance_to_keep,
Covariance  covariance_type = Covariance::AtA 
)
inline

Performs PCA and returns the number of eigenvectors and eigenvalues to retain variance_to_keep variance of the original data.

See std::pair<Eigen::MatrixXf, Eigen::VectorXf> pca(const Eigen::Ref<const Eigen::MatrixXf>, Covariance).

variance_to_keep needs to be between 0.0 and 1.0.

Parameters
[in]dataMean-free data matrix, with each row being a training sample.
[in]variance_to_keepSpecifies how much of the variance to retain, in percent (between 0 and 1).
[in]covariance_typeSpecifies whether PCA is computed on the covariance matrix AtA (default) or the inner-product matrix AAt.
Returns
A pair containing the matrix of eigenvectors and a vector with the respective eigenvalues.

◆ pca() [3/4]

std::pair< Eigen::MatrixXf, Eigen::VectorXf > eos::pca::pca ( const Eigen::Ref< const Eigen::MatrixXf >  data,
int  num_eigenvectors_to_keep,
Covariance  covariance_type = Covariance::AtA 
)
inline

Performs PCA and returns num_eigenvectors_to_keep eigenvectors and eigenvalues.

See std::pair<Eigen::MatrixXf, Eigen::VectorXf> pca(const Eigen::Ref<const Eigen::MatrixXf>, Covariance).

num_eigenvectors_to_keep needs to be smaller or equal to n-1, where n is number of rows of data (i.e. number of data samples).

Parameters
[in]dataMean-free data matrix, with each row being a training sample.
[in]num_eigenvectors_to_keepSpecifies how many eigenvectors and eigenvalues to keep.
[in]covariance_typeSpecifies whether PCA is computed on the covariance matrix AtA (default) or the inner-product matrix AAt.
Returns
A pair containing the matrix of eigenvectors and a vector with the respective eigenvalues.

◆ pca() [4/4]

morphablemodel::PcaModel eos::pca::pca ( const Eigen::Ref< const Eigen::MatrixXf >  data,
std::vector< std::array< int, 3 > >  triangle_list,
Covariance  covariance_type = Covariance::AtA 
)
inline

Performs PCA on the given data (including subtracting the mean) and returns the built PcaModel.

See std::pair<Eigen::MatrixXf, Eigen::VectorXf> pca(const Eigen::Ref<const Eigen::MatrixXf>, Covariance) for the details on the PCA.

data should be a (num_training_samples x num_data_dimensions) matrix, i.e. each row one data instance (e.g. one 3D scan).

Parameters
[in]dataData matrix (orignal, without the mean subtracted), with each row being a training sample.
[in]triangle_listTriangle list to build the topology of the PcaModel mesh.
[in]covariance_typeSpecifies whether PCA is computed on the covariance matrix AtA (default) or the inner-product matrix AAt.
Returns
A PcaModel constructed from the given data.