0
0
SciPydata~5 mins

Sparse matrix operations in SciPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a sparse matrix?
A sparse matrix is a matrix mostly filled with zeros. It saves memory by storing only the non-zero values and their positions.
Click to reveal answer
beginner
Name two common formats for storing sparse matrices in SciPy.
Two common formats are CSR (Compressed Sparse Row) and CSC (Compressed Sparse Column). They store data efficiently for different operations.
Click to reveal answer
beginner
How do you convert a dense NumPy array to a sparse CSR matrix in SciPy?
Use `scipy.sparse.csr_matrix(your_dense_array)` to convert a dense array to a CSR sparse matrix.
Click to reveal answer
intermediate
What is the advantage of using sparse matrix multiplication over dense matrix multiplication?
Sparse matrix multiplication is faster and uses less memory because it only processes non-zero elements, unlike dense multiplication which processes all elements.
Click to reveal answer
beginner
How can you find the number of non-zero elements in a sparse matrix?
Use the `.nnz` attribute of a sparse matrix to get the count of non-zero elements.
Click to reveal answer
Which SciPy sparse matrix format is best for fast row slicing?
ALIL (List of Lists)
BCSC (Compressed Sparse Column)
CCSR (Compressed Sparse Row)
DDOK (Dictionary of Keys)
What does the `.tocsc()` method do on a sparse matrix?
AConverts the matrix to CSC format
BConverts the matrix to CSR format
CConverts the matrix to dense format
DReturns the transpose of the matrix
How do you create a sparse identity matrix of size 4x4 in SciPy?
Ascipy.sparse.identity(4)
BAll of the above
Cscipy.sparse.eye(4)
Dscipy.sparse.diags([1,1,1,1])
Which attribute gives the number of non-zero elements in a sparse matrix?
A.count_nonzero()
B.shape
C.size
D.nnz
Why use sparse matrices instead of dense matrices?
ATo save memory and speed up calculations when many elements are zero
BTo make matrices look nicer
CBecause dense matrices are deprecated
DTo increase matrix size
Explain what a sparse matrix is and why it is useful in data science.
Think about storing mostly zeros efficiently.
You got /3 concepts.
    Describe how to convert a dense matrix to a sparse matrix and perform multiplication using SciPy.
    Use SciPy functions and explain benefits.
    You got /3 concepts.