Bird
0
0

You have a large 10000x10000 matrix with 99% zeros. Which approach saves the most memory and why?

hard📝 Application Q15 of 15
SciPy - Sparse Matrices (scipy.sparse)
You have a large 10000x10000 matrix with 99% zeros. Which approach saves the most memory and why?
Option A: Store as a dense numpy array
Option B: Store as a scipy sparse matrix
Option C: Store as a list of lists including zeros
Option D: Store as a dictionary with all elements
AOption A, because numpy arrays are always memory efficient
BOption B, because sparse matrices store only non-zero values and positions
COption C, because lists are flexible and fast
DOption D, because dictionaries store keys and values explicitly
Step-by-Step Solution
Solution:
  1. Step 1: Analyze memory use of dense numpy arrays

    Dense arrays store every element, so 10000x10000 elements use a lot of memory, including zeros.
  2. Step 2: Analyze sparse matrix memory use

    Sparse matrices store only non-zero elements and their positions, greatly reducing memory when zeros dominate.
  3. Step 3: Compare lists and dictionaries

    Lists of lists store all elements including zeros, using much memory. Dictionaries store keys and values, but overhead is high and all elements must be stored.
  4. Final Answer:

    Option B, because sparse matrices store only non-zero values and positions -> Option B
  5. Quick Check:

    Sparse matrices save memory with many zeros [OK]
Quick Trick: Sparse matrices save memory by ignoring zeros [OK]
Common Mistakes:
MISTAKES
  • Assuming numpy arrays are always memory efficient
  • Thinking lists or dictionaries save memory with many zeros
  • Ignoring overhead of storing keys in dictionaries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes