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 TruncatedSVDTruncatedSVD(
n_components: int = 2,
algorithm: str = "randomized",
n_iter: int = 5,
random_state: Optional[int] = None,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n_components | int | 2 | Desired dimensionality of the output data. |
algorithm | {"randomized", "arpack", "exact"} | "randomized" | SVD solver strategy to use. |
n_iter | int | 5 | Number of power iterations for the "randomized" SVD solver. |
random_state | int or None | None | Random seed used during randomized SVD for reproducible results. |
device | str or None | None | Target compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type / Shape | Description |
|---|---|---|
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_ | int | Number of features seen during fit. |
device_ | str | Active resolved compute device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> TruncatedSVD | Fit the truncated SVD model on X. |
transform | transform(X) -> ndarray | Project X onto the learned singular vectors. |
fit_transform | fit_transform(X, y=None) -> ndarray | Fit the model on X and return the transformed matrix. |
inverse_transform | inverse_transform(X) -> ndarray | Transform reduced coordinates back to the original feature space. |
to | to(device) -> TruncatedSVD | Move execution target to a new hardware device. |
save | save(path) -> None | Save the fitted model to a binary .dit file. |
load | TruncatedSVD.load(path, device=None) -> TruncatedSVD | Load 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))