Bird
Raised Fist0
SciPydata~10 mins

CSC format (Compressed Sparse Column) in SciPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the CSC matrix class from scipy.

SciPy
from scipy.sparse import [1]
Drag options to blanks, or click blank then click option'
Acsr_matrix
Bdok_matrix
Ccsc_matrix
Dcoo_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Importing csr_matrix instead of csc_matrix
Using coo_matrix which is a different sparse format
2fill in blank
medium

Complete the code to create a 3x3 CSC matrix with data [1, 2, 3] at positions (0,0), (1,1), and (2,2).

SciPy
from scipy.sparse import csc_matrix
import numpy as np
row = np.array([0, 1, 2])
col = np.array([0, 1, 2])
data = np.array([1, 2, 3])
matrix = csc_matrix(([1], (row, col)), shape=(3, 3))
Drag options to blanks, or click blank then click option'
Arow
Bdata
Ccol
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing row or col arrays instead of data
Passing a list instead of the data variable
3fill in blank
hard

Fix the error in the code to convert a dense numpy array to a CSC matrix.

SciPy
import numpy as np
from scipy.sparse import csc_matrix
dense = np.array([[0, 1], [2, 0]])
sparse = [1](dense)
Drag options to blanks, or click blank then click option'
Acsr_matrix
Bdok_matrix
Ccoo_matrix
Dcsc_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using csr_matrix instead of csc_matrix
Trying to call a method that does not exist
4fill in blank
hard

Fill both blanks to create a CSC matrix and then get its shape.

SciPy
from scipy.sparse import [1]
import numpy as np
data = np.array([4, 5, 6])
row = np.array([0, 1, 2])
col = np.array([0, 1, 2])
matrix = [2]((data, (row, col)), shape=(3, 3))
shape = matrix.shape
Drag options to blanks, or click blank then click option'
Acsc_matrix
Bcsr_matrix
Ccoo_matrix
Ddok_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing import and constructor classes
Using csr_matrix or other formats
5fill in blank
hard

Fill all three blanks to create a CSC matrix, convert it to dense, and print the result.

SciPy
from scipy.sparse import [1]
data = [7, 8, 9]
row = [0, 1, 2]
col = [0, 1, 2]
matrix = [2]((data, (row, col)), shape=(3, 3))
dense_matrix = matrix.[3]()
print(dense_matrix)
Drag options to blanks, or click blank then click option'
Acsc_matrix
Ctoarray
Dtodense
Attempts:
3 left
💡 Hint
Common Mistakes
Using toarray() instead of todense()
Importing wrong sparse matrix class

Practice

(1/5)
1. What does the CSC format in scipy.sparse primarily store for a sparse matrix?
easy
A. Only non-zero values and their row indices, organized by columns
B. All values including zeros, organized by rows
C. Only zero values and their column indices
D. All values including zeros, organized by columns

Solution

  1. Step 1: Understand CSC format storage

    CSC stands for Compressed Sparse Column, which stores only non-zero values and their row indices, grouped by columns.
  2. Step 2: Compare options with CSC definition

    Only non-zero values and their row indices, organized by columns matches the CSC format definition exactly, while others include zeros or wrong organization.
  3. Final Answer:

    Only non-zero values and their row indices, organized by columns -> Option A
  4. Quick Check:

    CSC stores non-zero values by columns = A [OK]
Hint: CSC stores non-zero values by columns only [OK]
Common Mistakes:
  • Thinking CSC stores zero values
  • Confusing CSC with CSR (row-based)
  • Assuming all values are stored
2. Which of the following is the correct way to create a CSC matrix from a dense NumPy array arr in Python using SciPy?
easy
A. scipy.sparse.csc_matrix(arr)
B. scipy.sparse.csr_matrix(arr)
C. scipy.sparse.dense_matrix(arr)
D. scipy.sparse.csc(arr)

Solution

  1. Step 1: Identify correct SciPy function for CSC

    The function to create a CSC matrix is scipy.sparse.csc_matrix().
  2. Step 2: Check options for syntax correctness

    scipy.sparse.csc_matrix(arr) uses the correct function and syntax. scipy.sparse.csr_matrix(arr) creates CSR, not CSC. Options C and D are invalid functions.
  3. Final Answer:

    scipy.sparse.csc_matrix(arr) -> Option A
  4. Quick Check:

    Use csc_matrix() to create CSC format [OK]
Hint: Use csc_matrix() to create CSC sparse matrix [OK]
Common Mistakes:
  • Using csr_matrix instead of csc_matrix
  • Trying non-existent functions like dense_matrix
  • Missing _matrix suffix in function name
3. Given the following code, what will be the output of print(csc_matrix.indptr)?
import numpy as np
from scipy.sparse import csc_matrix

arr = np.array([[0, 0, 3],
                [4, 0, 0],
                [0, 5, 6]])
csc_matrix = csc_matrix(arr)
medium
A. [0 2 3 4]
B. [0 1 2 3]
C. [0 1 3 4]
D. [0 1 2 4]

Solution

  1. Step 1: Understand indptr in CSC format

    indptr array shows column start indices in data arrays. For 3 columns, length is 4.
  2. Step 2: Count non-zero elements per column

    Column 0 has 1 non-zero (4), column 1 has 1 non-zero (5), column 2 has 2 non-zeros (3,6). So indptr = [0,1,2,4].
  3. Final Answer:

    [0 1 2 4] -> Option D
  4. Quick Check:

    indptr counts cumulative non-zeros per column = B [OK]
Hint: Count non-zeros per column cumulatively for indptr [OK]
Common Mistakes:
  • Counting rows instead of columns
  • Mixing up indptr with indices array
  • Off-by-one errors in cumulative counts
4. Identify the error in this code snippet that tries to create a CSC matrix:
from scipy.sparse import csc_matrix

arr = [[1, 0], [0, 2]]
csc = csc_matrix(arr, shape=(3,3))
print(csc.toarray())
medium
A. toarray() method does not exist for csc_matrix
B. Input array must be a NumPy array, not a list
C. Shape parameter is larger than input array size causing mismatch
D. csc_matrix requires data type float, not int

Solution

  1. Step 1: Check input array and shape parameter

    Input array is 2x2 but shape is set to 3x3, causing dimension mismatch.
  2. Step 2: Understand effect of shape mismatch

    Specifying a larger shape than data size is invalid and raises an error.
  3. Final Answer:

    Shape parameter is larger than input array size causing mismatch -> Option C
  4. Quick Check:

    Shape must match input size or smaller [OK]
Hint: Shape must match or be smaller than input array size [OK]
Common Mistakes:
  • Assuming lists are invalid input
  • Thinking data type must be float
  • Believing toarray() is missing
5. You have a large sparse matrix representing user-item ratings with mostly zeros. You want to efficiently compute the sum of ratings for each item (column). Which approach using CSC format is best?
hard
A. Convert to dense array and sum all values using np.sum()
B. Convert to CSC format and sum non-zero values column-wise using .sum(axis=0)
C. Convert to CSR format and sum non-zero values row-wise using .sum(axis=1)
D. Use COO format and iterate over all entries to sum columns manually

Solution

  1. Step 1: Understand CSC format advantage

    CSC stores data column-wise, making column operations like sum efficient.
  2. Step 2: Choose best method for column sums

    Using .sum(axis=0) on CSC matrix efficiently sums columns without converting to dense.
  3. Final Answer:

    Convert to CSC format and sum non-zero values column-wise using .sum(axis=0) -> Option B
  4. Quick Check:

    CSC + sum(axis=0) = fast column sums [OK]
Hint: Use CSC and sum(axis=0) for fast column sums [OK]
Common Mistakes:
  • Using CSR which is row-based for column sums
  • Converting to dense wastes memory
  • Manually iterating instead of vectorized sum
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