Header-only C++ principal components analysis with Eigen (3.4.1 and newer).
#include "pca/eigen-pca.hpp"
// Define your data in the format
// [p0d0, p0d1, ..., p1d0, p1d1, ..., pNd0, pNd1, ..., pNdM]
// with p0 being point 0 and d0 dimension 0, up to point N and dimension M
std::vector<float> data_in;
size_t num_dims = 50;
// Compute first two principle components
std::vector<float> pca_out;
size_t num_comp = 2;
math::pca(data_in, num_dims, pca_out, num_comp);
Two normalization procedures are implemented: Other normalization steps before this centering are possible as well:
- Mean normalization (
math::DATA_NORM::MEAN
) - Rescaling (min-max normalization) (
math::DATA_NORM::MINMAX
) - No normalization (
math::DATA_NORM::NONE
)
Each normalization centers the data such that each dimension/channel has zero mean.
This project implements two PCA computation algorithms:
- Explicitly computing the eigenvectors of the covariance matrix (
math::PCA_ALG::COV
) - Singular value decomposition (
math::PCA_ALG::SVD
).
The default settings are:
math::pca(data_in, num_dims, pca_out, num_comp, math:PCA_ALG::SVD, math:DATA_NORM::MINMAX);
To run some test, follow the setup instructions, which include creating reference data and ensuring that Eigen is available.