SciPy - Sparse Matrices (scipy.sparse)
You have a large sparse matrix representing user-item ratings with many zeros. You want to efficiently compute the sum of all ratings for each user (row). Which approach using CSR format is best?
sum(axis=1) method to sum non-zero values per row efficiently. uses sum(axis=1) directly on CSR, which is fast and memory efficient. Convert the CSR matrix back to a dense array and use NumPy sum along axis 1. wastes memory converting to dense. Manually iterate over all elements including zeros to sum each row. is slow. Use the CSR matrix's sum(axis=0) method to sum rows. sums columns, not rows.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions