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, default2).
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).
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";