Get U, Sigma, V* matrix from Truncated SVD in scikit-learn Get U, Sigma, V* matrix from Truncated SVD in scikit-learn python python

Get U, Sigma, V* matrix from Truncated SVD in scikit-learn


Looking into the source via the link you provided, TruncatedSVD is basically a wrapper around sklearn.utils.extmath.randomized_svd; you can manually call this yourself like this:

from sklearn.utils.extmath import randomized_svdU, Sigma, VT = randomized_svd(X,                               n_components=15,                              n_iter=5,                              random_state=None)


One can use scipy.sparse.svds (for dense matrices you can use svd).

import numpy as npfrom scipy.sparse.linalg import svdsmatrix = np.random.random((20, 20))num_components = 2u, s, v = svds(matrix, k=num_components)X = u.dot(np.diag(s))  # output of TruncatedSVD

If you're working with really big sparse matrices (perhaps your working with natural text), even scipy.sparse.svds might blow up your computer's RAM. In such cases, consider the sparsesvd package which uses SVDLIBC, and what gensim uses under-the-hood.

import numpy as npfrom sparsesvd import sparsesvdX = np.random.random((30, 30))ut, s, vt = sparsesvd(X.tocsc(), k)projected = (X * ut.T)/s


Just as a note:

svd.transform(X)

and

svd.fit_transform(X)

generate U * Sigma.

svd.singular_values_

generates Sigma in vector form.

svd.components_

generates VT.Maybe we can use

svd.transform(X).dot(np.linalg.inv(np.diag(svd.singular_values_)))

to get U because U * Sigma * Sigma ^ -1 = U * I = U.