Predictive Hacks

SVD with Scikit Learn

We can apply the SVD decomposition in Scikit Learn. Let’s see how we can get the U the Sigma and the V matrices.

Example of SVD in Python 1
from sklearn.decomposition import TruncatedSVD
import numpy as np

np.random.seed(0)
X = np.random.rand(100, 100)

# four components
svd = TruncatedSVD(n_components=4, n_iter=10, random_state=5)

U = svd.fit_transform(X)
Sigma = np.diag(svd.singular_values_)
V = svd.components_

In case we want to do the matrix multiplication, we can do it as follows:

# in case we want to do the multiplication

# U x Sigma
U_x_Sigma = np.dot(U, Sigma)
 
# (U x Sigma) x V
U_Sigma_V = np.dot(U_x_Sigma , V)

You can have a look at the SVD with SciPy.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Get Started with Hugging Face Auto Train

Hugging Face has launched the auto train, which is a new way to automatically train, evaluate and deploy state-of-the-art Machine