Bird
Raised Fist0
SciPydata~20 mins

Sparse matrix operations in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sparse Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of sparse matrix multiplication
What is the output of this code that multiplies two sparse matrices?
SciPy
import numpy as np
from scipy.sparse import csr_matrix

A = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]])
B = csr_matrix([[1, 0, 0], [0, 3, 0], [4, 0, 0]])

C = A.dot(B)
print(C.toarray())
A
[[0 0 0]
 [1 0 0]
 [0 3 0]]
B
[[0 0 0]
 [0 6 0]
 [4 0 0]]
C
[[4 0 0]
 [0 0 0]
 [0 6 0]]
D
[[4 0 0]
 [1 0 0]
 [0 6 0]]
Attempts:
2 left
💡 Hint
Remember that sparse matrix multiplication follows the same rules as dense matrix multiplication.
❓ data_output
intermediate
1:30remaining
Number of non-zero elements after addition
Given two sparse matrices, what is the number of non-zero elements in their sum?
SciPy
from scipy.sparse import csc_matrix

X = csc_matrix([[0, 5, 0], [0, 0, 0], [7, 0, 0]])
Y = csc_matrix([[0, 0, 3], [0, 0, 0], [0, 0, 0]])

Z = X + Y
print(Z.nnz)
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Count unique non-zero positions after addition.
🔧 Debug
advanced
1:30remaining
Identify the error in sparse matrix slicing
What error does this code raise when slicing a sparse matrix?
SciPy
from scipy.sparse import lil_matrix

M = lil_matrix((3,3))
M[0, 0] = 1
M[1, 1] = 2

sub = M[0:2, 0:2]
print(sub)
AIndexError: index out of bounds
BTypeError: slicing with step is not supported
CNo error, prints a sparse matrix
DValueError: invalid slice
Attempts:
2 left
💡 Hint
Check if lil_matrix supports slicing.
❓ visualization
advanced
1:30remaining
Visualizing sparsity pattern
Which code snippet correctly visualizes the sparsity pattern of a CSR sparse matrix using matplotlib?
SciPy
import matplotlib.pyplot as plt
from scipy.sparse import csr_matrix

A = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]])

# Visualization code here
A
plt.spy(A)
plt.show()
B
plt.scatter(A)
plt.show()
C
plt.plot(A)
plt.show()
D
plt.imshow(A)
plt.show()
Attempts:
2 left
💡 Hint
Use the function designed to show sparsity patterns.
🚀 Application
expert
3:00remaining
Efficient sparse matrix row normalization
Which code snippet correctly normalizes each row of a CSR sparse matrix so that each row sums to 1, without converting to dense format?
SciPy
from scipy.sparse import csr_matrix
import numpy as np

M = csr_matrix([[0, 1, 2], [3, 0, 0], [0, 0, 4]])

# Normalize rows here
A
row_sums = np.array(M.sum(axis=1)).flatten()
row_indices, _ = M.nonzero()
M.data /= row_sums[row_indices]
BM = M / M.sum(axis=1)
C
for i in range(M.shape[0]):
    M[i] = M[i] / M[i].sum()
DM = M.multiply(1 / M.sum(axis=0))
Attempts:
2 left
💡 Hint
Use the sparse matrix data and indices to normalize efficiently.

Practice

(1/5)
1. What is the main advantage of using a sparse matrix in data science?
easy
A. It duplicates all elements to increase accuracy.
B. It automatically sorts data in ascending order.
C. It converts data into a dense format for faster processing.
D. It saves memory by storing only non-zero elements.

Solution

  1. Step 1: Understand sparse matrix storage

    Sparse matrices store only non-zero values to save memory space compared to dense matrices.
  2. Step 2: Compare options

    Only It saves memory by storing only non-zero elements. correctly describes this key advantage. Other options describe unrelated or incorrect features.
  3. Final Answer:

    It saves memory by storing only non-zero elements. -> Option D
  4. Quick Check:

    Sparse matrix = memory saving [OK]
Hint: Sparse means store only non-zero values to save memory [OK]
Common Mistakes:
  • Thinking sparse matrices sort data
  • Confusing sparse with dense matrix operations
  • Assuming sparse matrices duplicate data
2. Which of the following is the correct way to create a CSR sparse matrix from a dense NumPy array arr?
easy
A. csr = scipy.sparse.csr(arr)
B. csr = scipy.sparse.csr_matrix(arr)
C. csr = scipy.sparse.array(arr)
D. csr = scipy.sparse.dense_matrix(arr)

Solution

  1. Step 1: Recall CSR matrix creation syntax

    The correct function to create a CSR sparse matrix is csr_matrix() from scipy.sparse.
  2. Step 2: Check options

    Only csr = scipy.sparse.csr_matrix(arr) uses the correct function name and syntax. Others are invalid or do not exist.
  3. Final Answer:

    csr = scipy.sparse.csr_matrix(arr) -> Option B
  4. Quick Check:

    Use csr_matrix() to create CSR sparse matrix [OK]
Hint: Use csr_matrix() to create CSR sparse matrix from array [OK]
Common Mistakes:
  • Using non-existent functions like dense_matrix or csr
  • Confusing array() with csr_matrix()
  • Missing scipy.sparse prefix
3. What will be the output of the following code?
import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[0, 1], [2, 0]])
sparse_mat = csr_matrix(arr)
result = sparse_mat.dot(np.array([1, 2]))
print(result.toarray())
medium
A. [[2] [2]]
B. [[2 4] [1 2]]
C. [[2 0] [0 1]]
D. [[2] [1]]

Solution

  1. Step 1: Understand matrix and vector multiplication

    The sparse matrix is [[0,1],[2,0]]. Multiplying by vector [1,2] means:
    Row 1: 0*1 + 1*2 = 2
    Row 2: 2*1 + 0*2 = 2
  2. Step 2: Check the printed output

    The result is a 2x1 matrix with values [[2],[2]]. But the code prints result.toarray(), which shows a 2D array.
    So output is:
    [[2]
    [2]]
  3. Final Answer:

    [[2] [2]] -> Option A
  4. Quick Check:

    Dot product result = [[2],[2]] [OK]
Hint: Multiply rows by vector elements and sum for dot product [OK]
Common Mistakes:
  • Mixing element-wise and dot product multiplication
  • Confusing shape of output array
  • Incorrectly summing elements
4. The following code throws an error. What is the most likely cause?
from scipy.sparse import csr_matrix

mat1 = csr_matrix([[1, 0], [0, 2]])
mat2 = csr_matrix([[3, 4]])
result = mat1 + mat2
medium
A. Shapes of mat1 and mat2 do not match for addition.
B. csr_matrix cannot be added using + operator.
C. mat2 must be converted to dense before addition.
D. mat1 contains zeros which cause errors in addition.

Solution

  1. Step 1: Check shapes of matrices

    mat1 shape is (2,2), mat2 shape is (1,2). Addition requires same shape.
  2. Step 2: Understand addition rules for sparse matrices

    Adding matrices with different shapes causes a ValueError. The + operator works fine if shapes match.
  3. Final Answer:

    Shapes of mat1 and mat2 do not match for addition. -> Option A
  4. Quick Check:

    Matrix addition requires matching shapes [OK]
Hint: Check matrix shapes before adding sparse matrices [OK]
Common Mistakes:
  • Assuming + operator is invalid for sparse matrices
  • Ignoring shape mismatch errors
  • Thinking zeros cause addition errors
5. You have two CSR sparse matrices A and B representing user-item ratings. You want to compute the element-wise product to find items rated by both users. Which method is the most efficient and correct way to do this?
hard
A. Convert both to dense arrays, multiply, then convert back to sparse.
B. Use A.dot(B) for element-wise multiplication.
C. Use A.multiply(B) to get element-wise multiplication.
D. Use A + B and then subtract zeros.

Solution

  1. Step 1: Identify element-wise multiplication method

    For sparse matrices, multiply() performs element-wise multiplication efficiently without converting to dense.
  2. Step 2: Evaluate other options

    dot() is matrix multiplication, not element-wise.
    Converting to dense wastes memory.
    Addition and subtracting zeros is incorrect for element-wise product.
  3. Final Answer:

    Use A.multiply(B) to get element-wise multiplication. -> Option C
  4. Quick Check:

    Element-wise sparse multiply = multiply() method [OK]
Hint: Use multiply() for element-wise sparse matrix product [OK]
Common Mistakes:
  • Using dot() instead of multiply()
  • Converting sparse to dense unnecessarily
  • Trying to add and subtract for element-wise product
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard