Bird
0
0

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📝 Application Q15 of 15
SciPy - Sparse Matrices (scipy.sparse)
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?
AConvert to dense array and sum all values using <code>np.sum()</code>
BConvert to CSC format and sum non-zero values column-wise using <code>.sum(axis=0)</code>
CConvert to CSR format and sum non-zero values row-wise using <code>.sum(axis=1)</code>
DUse COO format and iterate over all entries to sum columns manually
Step-by-Step Solution
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]
Quick Trick: Use CSC and sum(axis=0) for fast column sums [OK]
Common Mistakes:
MISTAKES
  • Using CSR which is row-based for column sums
  • Converting to dense wastes memory
  • Manually iterating instead of vectorized sum

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes