Skip to main content

PCA

#include <Skigen/Decomposition>

template <typename Scalar = double>
class Skigen::PCA(n_components=0)

Principal component analysis (PCA).

Linear dimensionality reduction using Singular Value Decomposition of the data to project it to a lower dimensional space. The input data is centered but not scaled for each feature before applying the SVD.

Mirrors sklearn.decomposition.PCA.


Parameters:

  • n_components : Eigen::Index, default=0 Number of components to keep (IndexType, default 0). 0 means all components are kept.

Attributes:

  • n_components : Eigen::Index The actual number of components after fitting.

  • components : MatrixType Principal axes in feature space (n_components × n_features).

  • explained_variance : VectorType Variance explained by each selected component.

  • explained_variance_ratio : VectorType Percentage of variance explained by each selected component.

  • singular_values : VectorType Singular values corresponding to each component.

  • mean : RowVectorType Per-feature empirical mean (1 × n_features).


Methods

fit(X)

Fit the model with X by computing the SVD.

Centers the data and performs full SVD (JacobiSVD) to compute principal components, explained variance, and singular values.

Parameters:

  • X : MatrixType Training data of shape (n_samples, n_features).

Returns:

  • result : PCA Reference to the fitted transformer (*this).

transform(X)

Apply dimensionality reduction to X.

Projects data onto the first n_components principal axes: Z=(Xμ)VZ = (X - \mu) V^\top.

Parameters:

  • X : MatrixType Data matrix of shape (n_samples, n_features).

Returns:

  • result : MatrixType Transformed data of shape (n_samples, n_components).

Throws:

  • std::runtime_error — if the model has not been fitted.

inverse_transform(X)

Transform data back to its original space.

Approximately reconstructs: X^=ZV+μ\hat{X} = Z V + \mu.

Parameters:

  • X : MatrixType Transformed data of shape (n_samples, n_components).

Returns:

  • result : MatrixType Reconstructed data of shape (n_samples, n_features).

Throws:

  • std::runtime_error — if the model has not been fitted.

Example

// Reduce to 3 components
Skigen::PCA<double> pca(3);
pca.fit(X_scaled);

Eigen::MatrixXd X_reduced = pca.transform(X_scaled);

std::cout << "=== PCA (10D → 3D) ===\n";
std::cout << "Explained variance ratio: "
<< pca.explained_variance_ratio().transpose() << "\n";
std::cout << "Total variance captured: "
<< pca.explained_variance_ratio().sum() * 100.0 << "%\n";
std::cout << "Singular values: "
<< pca.singular_values().transpose() << "\n";
std::cout << "Reduced shape: " << X_reduced.rows() << " x "
<< X_reduced.cols() << "\n\n";

// Inverse transform — reconstruct approximate original
Eigen::MatrixXd X_approx = pca.inverse_transform(X_reduced);