0
0
SciPydata~30 mins

Sparse SVD (svds) in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Sparse SVD with svds from SciPy
📖 Scenario: You work as a data analyst for a movie streaming service. You have a large matrix showing user ratings for movies, but most users have rated only a few movies, so the matrix is mostly empty (sparse). You want to find patterns in this data using a technique called Sparse Singular Value Decomposition (Sparse SVD).
🎯 Goal: Build a Python program that creates a sparse matrix of user ratings, configures the number of singular values to find, applies Sparse SVD using svds from SciPy, and prints the singular values.
📋 What You'll Learn
Create a sparse matrix using scipy.sparse.csr_matrix with given data
Set a variable k for the number of singular values to compute
Use svds from scipy.sparse.linalg to compute the sparse SVD
Print the singular values array
💡 Why This Matters
🌍 Real World
Sparse SVD is used in recommendation systems to find hidden patterns in large, sparse user-item rating data.
💼 Career
Data scientists and machine learning engineers use sparse matrix decompositions to reduce data size and improve model performance.
Progress0 / 4 steps
1
Create a sparse matrix of user ratings
Create a sparse matrix called ratings using scipy.sparse.csr_matrix with the exact data: rows = [0, 0, 1, 2, 2], cols = [0, 2, 2, 0, 1], and data = [5, 3, 4, 1, 2].
SciPy
Need a hint?

Use csr_matrix((data, (rows, cols)), shape=(3, 3)) to create the sparse matrix.

2
Set the number of singular values to compute
Create a variable called k and set it to 2 to specify the number of singular values to compute.
SciPy
Need a hint?

Just write k = 2 to set the number of singular values.

3
Compute Sparse SVD using svds
Import svds from scipy.sparse.linalg and use it to compute the sparse SVD of ratings with k singular values. Store the results in variables u, s, and vt.
SciPy
Need a hint?

Use from scipy.sparse.linalg import svds and then u, s, vt = svds(ratings, k=k).

4
Print the singular values
Write a print statement to display the singular values stored in the variable s.
SciPy
Need a hint?

Use print(s) to show the singular values.