0
0
SciPydata~20 mins

CSC format (Compressed Sparse Column) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of CSC matrix data arrays
Given the following code creating a CSC matrix, what is the output of the data, indices, and indptr arrays?
SciPy
from scipy.sparse import csc_matrix
import numpy as np

matrix = np.array([[0, 0, 1],
                   [2, 0, 0],
                   [0, 3, 4]])
csc = csc_matrix(matrix)
print(csc.data)
print(csc.indices)
print(csc.indptr)
A
[2 3 1 4]
[1 2 0 2]
[0 1 2 4]
B
[1 2 3 4]
[2 0 2 2]
[0 1 2 4]
C
[1 2 3 4]
[1 0 2 2]
[0 1 2 4]
D
[2 3 4 1]
[2 0 1 2]
[0 1 2 4]
Attempts:
2 left
💡 Hint
Remember CSC stores data column-wise. Check each column's non-zero elements.
data_output
intermediate
1:30remaining
Number of non-zero elements in CSC matrix
What is the number of non-zero elements in the following CSC matrix?
SciPy
from scipy.sparse import csc_matrix
import numpy as np

matrix = np.array([[0, 5, 0],
                   [0, 0, 0],
                   [7, 0, 9]])
csc = csc_matrix(matrix)
print(csc.nnz)
A4
B2
C5
D3
Attempts:
2 left
💡 Hint
Count all non-zero values in the matrix.
🔧 Debug
advanced
2:30remaining
Identify the error in CSC matrix creation
Which option will cause an error when creating a CSC matrix from the given data arrays?
SciPy
from scipy.sparse import csc_matrix
import numpy as np

data = np.array([1, 2, 3])
indices = np.array([0, 1, 2])
indptr = np.array([0, 1, 2, 3])

csc = csc_matrix((data, indices, indptr), shape=(3, 3))
Acsc_matrix((data, indptr, indices), shape=(3, 3))
Bcsc_matrix((data, indices, indptr), shape=(3, 3))
Ccsc_matrix((data, indices), shape=(3, 3))
Dcsc_matrix((data, indices, indptr), shape=(3, 2))
Attempts:
2 left
💡 Hint
Check the order of the tuple arguments for CSC matrix constructor.
🚀 Application
advanced
2:00remaining
Extract column from CSC matrix
Given a CSC matrix, which code snippet correctly extracts the second column as a dense array?
SciPy
from scipy.sparse import csc_matrix
import numpy as np

matrix = np.array([[0, 1, 0],
                   [2, 0, 3],
                   [0, 4, 0]])
csc = csc_matrix(matrix)
Acsc[:, 1].toarray()
Bcsc.getcol(1).toarray().flatten()
Ccsc.getrow(1).toarray().flatten()
Dcsc[1, :].toarray()
Attempts:
2 left
💡 Hint
Use the method designed to get a column from a sparse matrix.
🧠 Conceptual
expert
3:00remaining
Memory efficiency of CSC vs CSR for column slicing
Which statement best explains why CSC format is more efficient than CSR format for extracting columns from a sparse matrix?
ACSC stores data row-wise, so column slicing is slower than CSR which stores data column-wise.
BCSR stores data row-wise, so column slicing requires scanning all rows, making it slower and less memory efficient.
CCSC stores data column-wise, so column slicing accesses contiguous memory, making it faster and more memory efficient.
DCSR and CSC have identical memory layouts, so there is no difference in efficiency for column slicing.
Attempts:
2 left
💡 Hint
Think about how data is stored and accessed in CSC vs CSR formats.