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

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 =, Sigma)
# (U x Sigma) x V
U_Sigma_V = , 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

Join the inner circle

Join our 2.000+ members and receive exclusive
data science content 🧪