Bird
Raised Fist0
SciPydata~20 mins

Creating sparse matrices in SciPy - Practice Exercises

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 COO sparse matrix data arrays
What will be the output of the following code snippet that creates a COO sparse matrix and prints its data, row, and column arrays?
SciPy
from scipy.sparse import coo_matrix
import numpy as np

row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])

matrix = coo_matrix((data, (row, col)), shape=(4, 4))
print(matrix.data)
print(matrix.row)
print(matrix.col)
A
[4 9 7 5]
[0 0 1 3]
[0 2 1 3]
B
[9 7 5 4]
[3 1 0 0]
[2 1 3 0]
C
[4 5 7 9]
[0 3 1 0]
[0 3 1 2]
D
[4 5 7 9]
[0 1 2 3]
[0 1 2 3]
Attempts:
2 left
💡 Hint
Remember that COO matrix stores data in the order you provide the row and column indices.
❓ data_output
intermediate
2:00remaining
Number of non-zero elements in CSR matrix
Given the following CSR sparse matrix creation code, how many non-zero elements does the matrix contain?
SciPy
from scipy.sparse import csr_matrix

values = [10, 20, 30, 40]
col_indices = [0, 2, 2, 0]
row_ptr = [0, 2, 3, 4]

csr = csr_matrix((values, col_indices, row_ptr), shape=(3, 3))
print(csr.nnz)
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
The nnz attribute gives the count of stored non-zero elements.
❓ visualization
advanced
2:00remaining
Visualizing a sparse matrix pattern
Which option shows the correct matplotlib visualization code to display the sparsity pattern of a CSR matrix named 'csr'?
SciPy
import matplotlib.pyplot as plt
from scipy.sparse import csr_matrix

csr = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]])
A
plt.imshow(csr)
plt.show()
B
plt.spy(csr)
plt.show()
C
plt.plot(csr)
plt.show()
D
plt.scatter(csr)
plt.show()
Attempts:
2 left
💡 Hint
Use the function designed to visualize sparse matrix structure.
🧠 Conceptual
advanced
2:00remaining
Memory efficiency of sparse matrix formats
Which sparse matrix format is generally most memory efficient for matrices with many rows but very few non-zero elements per row?
ACSR (Compressed Sparse Row)
BCSC (Compressed Sparse Column)
CCOO (Coordinate format)
DDOK (Dictionary of Keys)
Attempts:
2 left
💡 Hint
Think about fast row slicing and compact storage of row pointers.
🔧 Debug
expert
2:00remaining
Identify the error in sparse matrix creation
What error will this code raise when trying to create a CSR matrix?
SciPy
from scipy.sparse import csr_matrix

values = [1, 2, 3]
col_indices = [0, 1]
row_ptr = [0, 2, 3]

csr = csr_matrix((values, col_indices, row_ptr), shape=(2, 3))
AIndexError: index out of bounds
BNo error, matrix created successfully
CTypeError: unsupported operand type(s)
DValueError: array length mismatch
Attempts:
2 left
💡 Hint
Check if the lengths of values and column indices match.

Practice

(1/5)
1. What is the main advantage of using sparse matrices in data science?
easy
A. They make data sorting faster
B. They save memory by storing only non-zero values
C. They convert data into images
D. They increase the size of the data

Solution

  1. Step 1: Understand sparse matrix storage

    Sparse matrices store only the non-zero elements instead of all elements.
  2. Step 2: Identify the benefit

    By storing only non-zero values, sparse matrices save memory space significantly.
  3. Final Answer:

    They save memory by storing only non-zero values -> Option B
  4. Quick Check:

    Sparse matrices = memory saving [OK]
Hint: Sparse matrices store only non-zero values to save memory [OK]
Common Mistakes:
  • Thinking sparse matrices store all values
  • Confusing sparse matrices with data sorting
  • Assuming sparse matrices increase data size
2. Which of the following is the correct way to create a COO sparse matrix using scipy?
easy
A. scipy.sparse.coo_matrix((data, (row, col)))
B. scipy.sparse.csr_matrix(data, row, col)
C. scipy.sparse.csc_matrix(data, row, col)
D. scipy.sparse.dense_matrix((data, (row, col)))

Solution

  1. Step 1: Recall COO matrix creation syntax

    COO format uses tuple (data, (row, col)) inside coo_matrix constructor.
  2. Step 2: Check options

    Only scipy.sparse.coo_matrix((data, (row, col))) matches the correct syntax for COO matrix creation.
  3. Final Answer:

    scipy.sparse.coo_matrix((data, (row, col))) -> Option A
  4. Quick Check:

    COO matrix = coo_matrix((data, (row, col))) [OK]
Hint: COO matrix needs (data, (row, col)) tuple [OK]
Common Mistakes:
  • Using csr_matrix or csc_matrix syntax for COO
  • Passing row and col as separate arguments
  • Using non-existent dense_matrix function
3. What will be the output of this code?
import numpy as np
from scipy.sparse import coo_matrix
row = np.array([0, 1, 2])
col = np.array([1, 2, 0])
data = np.array([4, 5, 6])
sparse_mat = coo_matrix((data, (row, col)), shape=(3, 3))
print(sparse_mat.toarray())
medium
A. [[0 4 0] [0 0 5] [6 0 0]]
B. [[4 0 0] [0 5 0] [0 0 6]]
C. [[0 0 6] [4 0 0] [0 5 0]]
D. [[0 6 0] [5 0 0] [0 0 4]]

Solution

  1. Step 1: Map data to positions

    Data 4 at (0,1), 5 at (1,2), 6 at (2,0) in a 3x3 matrix.
  2. Step 2: Construct full matrix

    Positions without data are zero, so matrix is [[0 4 0], [0 0 5], [6 0 0]].
  3. Final Answer:

    [[0 4 0] [0 0 5] [6 0 0]] -> Option A
  4. Quick Check:

    Data at (row,col) = matrix entries [OK]
Hint: Match data with (row, col) indices to build matrix [OK]
Common Mistakes:
  • Mixing row and column indices
  • Assuming data fills diagonal
  • Confusing CSR/CSC formats with COO
4. Identify the error in this code snippet:
from scipy.sparse import coo_matrix
row = [0, 1]
col = [1, 2]
data = [3, 4]
sparse = coo_matrix(data, (row, col))
medium
A. coo_matrix does not accept data with length 2
B. row and col should be numpy arrays, not lists
C. The shape parameter is missing
D. The data and indices should be passed as a single tuple

Solution

  1. Step 1: Check coo_matrix constructor usage

    coo_matrix expects a single tuple (data, (row, col)) as first argument.
  2. Step 2: Identify mistake in code

    Code passes data and (row, col) as separate arguments, causing error.
  3. Final Answer:

    The data and indices should be passed as a single tuple -> Option D
  4. Quick Check:

    coo_matrix((data, (row, col))) required [OK]
Hint: Pass data and indices as one tuple to coo_matrix [OK]
Common Mistakes:
  • Passing data and indices as separate arguments
  • Forgetting to wrap indices in a tuple
  • Assuming lists cause error instead of argument structure
5. You have a large dataset with many zero values. You want to create a sparse matrix for fast arithmetic operations. Which format should you choose and why?

Options:
A) CSR format, because it is efficient for arithmetic and slicing
B) COO format, because it is efficient for arithmetic
C) DOK format, because it uses dictionary storage
D) LIL format, because it is best for matrix multiplication
hard
A. DOK format, because it uses dictionary storage
B. COO format, because it is efficient for arithmetic
C. CSR format, because it is efficient for arithmetic and slicing
D. LIL format, because it is best for matrix multiplication

Solution

  1. Step 1: Understand sparse matrix formats

    COO is good for building matrices, CSR is efficient for arithmetic and slicing.
  2. Step 2: Choose format for fast arithmetic

    CSR format supports fast arithmetic operations and efficient row slicing, ideal for large data.
  3. Final Answer:

    CSR format, because it is efficient for arithmetic and slicing -> Option C
  4. Quick Check:

    CSR = fast arithmetic and slicing [OK]
Hint: Use CSR for fast math and slicing on sparse data [OK]
Common Mistakes:
  • Choosing COO for arithmetic instead of CSR
  • Confusing DOK with arithmetic efficiency
  • Assuming LIL is best for multiplication
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