Skip to Content
OchreML
Decomposition & Manifold

TruncatedSVD

Linear dimensionality reduction using truncated Singular Value Decomposition (LSA) without centering input data.

TruncatedSVD performs linear dimensionality reduction via truncated Singular Value Decomposition (also known as Latent Semantic Analysis / LSA). Unlike PCA, it does not center the input matrix X before factorization, making it well suited for term-document matrices, count data, and high-dimensional embeddings. (*≧ω≦*)

Backend Support

  • Backend primitives: E1–E5 (dense matrix multiplication and projections), N5 (SVD / eigensolver).
  • Supported devices: "auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0". Falls back cleanly to multi-threaded Rayon CPU (f64) when an accelerator is unavailable.

Import & Signature

from ochreml.decomposition import TruncatedSVD
# Or via top-level lazy import:
from ochreml import TruncatedSVD
TruncatedSVD(
    n_components: int = 2,
    algorithm: str = "randomized",
    n_iter: int = 5,
    random_state: Optional[int] = None,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_componentsint2Desired dimensionality of the output data.
algorithm{"randomized", "arpack", "exact"}"randomized"SVD solver strategy to use.
n_iterint5Number of power iterations for the "randomized" SVD solver.
random_stateint or NoneNoneRandom seed used during randomized SVD for reproducible results.
devicestr or NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeType / ShapeDescription
components_ndarray of shape (n_components, n_features_in_)Right singular vectors of the input data.
explained_variance_ndarray of shape (n_components,)Variance of the training samples projected onto each component.
explained_variance_ratio_ndarray of shape (n_components,)Fraction of variance explained by each selected component.
singular_values_ndarray of shape (n_components,)Singular values corresponding to each selected component.
n_features_in_intNumber of features seen during fit.
device_strActive resolved compute device string.

Methods

MethodSignatureDescription
fitfit(X, y=None) -> TruncatedSVDFit the truncated SVD model on X.
transformtransform(X) -> ndarrayProject X onto the learned singular vectors.
fit_transformfit_transform(X, y=None) -> ndarrayFit the model on X and return the transformed matrix.
inverse_transforminverse_transform(X) -> ndarrayTransform reduced coordinates back to the original feature space.
toto(device) -> TruncatedSVDMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model to a binary .dit file.
loadTruncatedSVD.load(path, device=None) -> TruncatedSVDLoad a saved model from a .dit file (also supported via ochreml.load).

Example

import numpy as np
import ochreml
from ochreml.decomposition import TruncatedSVD

rng = np.random.default_rng(7)
X = np.abs(rng.normal(size=(150, 20)))

svd = TruncatedSVD(
    n_components=4,
    algorithm="randomized",
    n_iter=5,
    random_state=42,
    device="auto",
)
X_proj = svd.fit_transform(X)
X_back = svd.inverse_transform(X_proj)

print("Projected shape:", X_proj.shape)
print("Singular values:", np.round(svd.singular_values_, 4))
print("Explained variance ratio:", np.round(svd.explained_variance_ratio_, 4))

svd.save("tsvd_model.dit")
loaded = TruncatedSVD.load("tsvd_model.dit", device="cpu")
assert np.allclose(X_proj, loaded.transform(X))

On this page