0
0
SciPydata~10 mins

Why sparse matrices save memory in SciPy - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a sparse matrix from a dense matrix using SciPy.

SciPy
from scipy.sparse import [1]
dense_matrix = [[0, 0, 3], [4, 0, 0], [0, 0, 0]]
sparse_matrix = [1](dense_matrix)
Drag options to blanks, or click blank then click option'
Acsr_matrix
Blist
Cmatrix
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' or 'list' instead of a sparse matrix constructor.
Trying to create a sparse matrix without importing the correct function.
2fill in blank
medium

Complete the code to get the number of non-zero elements in a sparse matrix.

SciPy
from scipy.sparse import csr_matrix
sparse_matrix = csr_matrix([[0, 0, 3], [4, 0, 0], [0, 0, 0]])
non_zero_count = sparse_matrix.[1]
Drag options to blanks, or click blank then click option'
Ashape
Bsize
Ccount
Dnnz
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' which gives total elements, not just non-zero.
Using 'shape' which gives matrix dimensions.
3fill in blank
hard

Fix the error in the code to convert a sparse matrix back to a dense array.

SciPy
from scipy.sparse import csr_matrix
sparse_matrix = csr_matrix([[0, 0, 3], [4, 0, 0], [0, 0, 0]])
dense_array = sparse_matrix.[1]()
Drag options to blanks, or click blank then click option'
Atoarray
Btodense
Cto_list
Dto_numpy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'todense' which returns a matrix, not an array.
Using non-existent methods like 'to_list' or 'to_numpy'.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

SciPy
{word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the word in the condition instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if count is greater than 0.

SciPy
{ [1]: [2] for [3], [2] in data.items() if [2] > 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item' instead of 'k' and 'v'.
Not converting the key to uppercase.