Skip to main content

TruncatedSVD

#include <Skigen/Decomposition>

template <typename Scalar = double>
class Skigen::TruncatedSVD(n_components=2)

Dimensionality reduction using truncated SVD (aka LSA).

This transformer performs linear dimensionality reduction by means of truncated singular value decomposition (SVD). Contrary to PCA, this estimator does not center the data before computing the singular value decomposition. This means it can work with sparse matrices efficiently.

Mirrors sklearn.decomposition.TruncatedSVD.


Parameters:

  • n_components : Eigen::Index, default=2 Desired dimensionality of output data (IndexType, default 2).

Attributes:

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

  • components : MatrixType The right singular vectors (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.


Methods

fit(X)

Fit the model on training data X (no centering).

Parameters:

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

Returns:

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

fit(X, random_state, n_oversamples, n_iter)

Fit the model on a sparse matrix without densifying.

Uses Halko-Martinsson-Tropp randomised SVD: draws a random Gaussian projection of width n_components + n_oversamples, runs n_iter power iterations with QR re-orthogonalisation for stability, then performs a small dense SVD on the projected matrix. All matrix products with X operate directly on the sparse representation — the input is never materialised as dense.

Mirrors sklearn's ``TruncatedSVD(algorithm="randomized") with the defaults n_oversamples=10, n_iter=5, power_iteration_normalizer="QR". Random state is consumed from random_state (defaults to a non-deterministic seed).


transform(X)

Apply the fitted dimensionality reduction to a sparse matrix without densifying. Returns a dense matrix of shape (n_samples, n_components) (sklearn parity — the output is always dense because it is low-rank).


transform(X)

Apply dimensionality reduction to X.

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.

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 2 components
Skigen::TruncatedSVD<double> svd(2);
svd.fit(X);

Eigen::MatrixXd X_reduced = svd.transform(X);

std::cout << "=== TruncatedSVD (8D → 2D) ===\n";
std::cout << "Explained variance ratio: "
<< svd.explained_variance_ratio().transpose() << "\n";
std::cout << "Total variance captured: "
<< svd.explained_variance_ratio().sum() * 100.0 << "%\n";
std::cout << "Singular values: "
<< svd.singular_values().transpose() << "\n";