mojolearn.randomized_svd#
- mojolearn.randomized_svd(M, n_components, *, n_oversamples=10, n_iter='auto', power_iteration_normalizer='auto', transpose='auto', flip_sign=True, random_state=None, numeric_mode=None)[source]#
Truncated SVD by randomized range finding.
Follows scikit-learn’s
utils.extmath.randomized_svd(Halko et al.). Every power iteration re-orthonormalizes with a QR step whateverpower_iteration_normalizersays (all choices span the same subspace), and the random test matrix comes from mojolearn’s own generator, so results differ slightly from scikit-learn’s for the same seed.- Parameters:
M (array-like or scipy.sparse matrix of shape (n_samples, n_features))
n_components (int) – Number of singular values and vectors; between 1 and
min(n_samples, n_features).n_oversamples (int, default 10) – Extra random vectors used to sample the range.
n_iter (int or “auto”, default “auto”) – Power iterations.
"auto"uses 7 whenn_components < 0.1 * min(M.shape), else 4.power_iteration_normalizer ({“auto”, “QR”, “LU”, “none”}, default “auto”) – Accepted for scikit-learn compatibility; see above.
transpose (bool or “auto”, default “auto”) – Work on
M.T;"auto"does so when M has more columns than rows.flip_sign (bool, default True) – Make the largest-magnitude entry of each column of U positive, as scikit-learn’s
svd_flip.random_state (int or None, default None) – Seed of the random test matrix; None means 0.
numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this call. None uses the package default (
"identical"unless changed)."identical"gives the same bits on NVIDIA and AMD GPUs;"fast"applies on Apple GPUs.
- Returns:
U (ndarray of shape (n_samples, n_components), float32)
s (ndarray of shape (n_components,), float32) – Singular values, descending.
Vt (ndarray of shape (n_components, n_features), float32)
- Raises:
ValueError – If
n_componentsis out of range,power_iteration_normalizeris unknown, or M is numerically zero.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 50), dtype=np.float32) >>> U, s, Vt = mojolearn.randomized_svd(X, 5, random_state=0)