Bird
0
0

Given the following code, what will be the output of print(csc_matrix.indptr)?

medium📝 Predict Output Q13 of 15
SciPy - Sparse Matrices (scipy.sparse)
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)
A[0 2 3 4]
B[0 1 2 3]
C[0 1 3 4]
D[0 1 2 4]
Step-by-Step Solution
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]
Quick Trick: Count non-zeros per column cumulatively for indptr [OK]
Common Mistakes:
MISTAKES
  • Counting rows instead of columns
  • Mixing up indptr with indices array
  • Off-by-one errors in cumulative counts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes