mojolearn.TruncatedSVD#

class mojolearn.TruncatedSVD(n_components=2, *, algorithm='covariance_eigh', n_iter=5, n_oversamples=10, power_iteration_normalizer='auto', random_state=None, tol=0.0)[source]#

Bases: NumericModeMixin

Dimensionality reduction by truncated singular value decomposition.

Unlike PCA, the data is not centered, so this works on raw term counts or TF-IDF matrices (a scipy.sparse input is densified). Input is converted to C-order float32. The default algorithm is the exact "covariance_eigh" solver, not scikit-learn’s "randomized".

Parameters:
  • n_components (int, default 2) – Number of components, in [1, n_features].

  • algorithm ({“covariance_eigh”, “jacobi”, “randomized”}, default “covariance_eigh”) – "covariance_eigh" and "jacobi" run the exact solver (an eigendecomposition of the Gram matrix). "randomized" runs randomized_svd(). "arpack" and any other value raise NotImplementedError.

  • n_iter (int, default 5) – Power iterations for "randomized".

  • n_oversamples (int, default 10) – Extra random vectors for "randomized".

  • power_iteration_normalizer (str, default “auto”) – Normalizer between power iterations for "randomized".

  • random_state (int or None, default None) – Seed for "randomized".

  • tol (float, default 0.0) – Only 0.0 is accepted (it is ARPACK’s tolerance); any other value raises NotImplementedError.

Variables:
  • components_ (Array of shape (n_components, n_features), float32) – Right singular vectors.

  • singular_values_ (Array of shape (n_components,), float32) – Singular values.

  • explained_variance_ (Array of shape (n_components,), float32) – Variance of each transformed column (scikit-learn’s definition).

  • explained_variance_ratio_ (Array of shape (n_components,), float32) – explained_variance_ divided by the total variance of X.

  • n_components_ (int) – Number of components.

  • n_features_in_ (int) – Number of features seen during fit.

  • input_copied_ (bool) – Whether X had to be copied to C-order float32.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> svd = mojolearn.TruncatedSVD(n_components=3).fit(X)
>>> Z = svd.transform(X)
__init__(n_components=2, *, algorithm='covariance_eigh', n_iter=5, n_oversamples=10, power_iteration_normalizer='auto', random_state=None, tol=0.0)[source]#

Methods

fit(X[, y])

Fit the model to X.

fit_transform(X[, y])

Fit to X and return its projection.

get_params([deep])

inverse_transform(X)

Map reduced data back to the original feature space.

load(path)

Load a model written by save.

numeric_mode_used()

The numeric mode this estimator runs in.

save(path)

Save the fitted model to path as an .npz file.

set_params(**params)

Set constructor parameters.

transform(X)

Project X onto the components.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None)[source]#

Fit the model to X.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data; at least 2 rows and 2 features.

  • y (ignored)

Returns:

self (TruncatedSVD) – The fitted estimator.

Raises:
transform(X)[source]#

Project X onto the components.

Parameters:

X (array-like of shape (n_samples, n_features)) – Data with the training feature count.

Returns:

Z (Array of shape (n_samples, n_components), float32) – The reduced data.

Raises:

ValueError – If the model is not fitted or the feature count differs.

fit_transform(X, y=None)[source]#

Fit to X and return its projection.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (ignored)

Returns:

Z (Array of shape (n_samples, n_components), float32) – The reduced data.

inverse_transform(X)[source]#

Map reduced data back to the original feature space.

Parameters:

X (array-like of shape (n_samples, n_components)) – Reduced data, as returned by transform.

Returns:

X_original (Array of shape (n_samples, n_features), float32) – The reconstruction X @ components_.

Raises:

ValueError – If the component count differs from the fit.

save(path)[source]#

Save the fitted model to path as an .npz file.

A model restored with TruncatedSVD.load or mojolearn.host_model(path) transforms, including on a CPU-only install.

Parameters:

path (str or path-like) – Destination file.

Raises:

RuntimeError – If the model is not fitted.

classmethod load(path)[source]#

Load a model written by save.

The loaded model transforms; it does not refit.

Parameters:

path (str or path-like) – File written by TruncatedSVD.save.

Returns:

model (TruncatedSVD) – The restored model.

Raises:

ValueError – If the file is not a TruncatedSVD model or its arrays are inconsistent.