0
0
SciPydata~10 mins

Sparse matrix operations in SciPy - Interactive Code Practice

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

Complete the code to create a 3x3 sparse matrix with scipy.

SciPy
from scipy.sparse import [1]

matrix = [1]([[0, 0, 1], [1, 0, 0], [0, 0, 0]])
print(matrix)
Drag options to blanks, or click blank then click option'
Adok_matrix
Bcsc_matrix
Ccsr_matrix
Dlil_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using dense matrix instead of sparse format.
Choosing a sparse format not suitable for arithmetic.
2fill in blank
medium

Complete the code to convert a dense numpy array to a sparse matrix.

SciPy
import numpy as np
from scipy.sparse import csr_matrix

dense = np.array([[0, 2, 0], [3, 0, 0], [0, 0, 4]])
sparse = csr_matrix([1])
print(sparse)
Drag options to blanks, or click blank then click option'
Adense
Bsparse
Cnp.array
Dnp.zeros
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the sparse matrix variable to the constructor again.
Passing a function instead of the array.
3fill in blank
hard

Fix the error in the code to add two sparse matrices correctly.

SciPy
from scipy.sparse import csr_matrix

A = csr_matrix([[1, 0], [0, 2]])
B = csr_matrix([[0, 3], [4, 0]])

result = A [1] B
print(result.toarray())
Drag options to blanks, or click blank then click option'
A*
B-
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition.
Using subtraction or division which are not intended here.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

SciPy
words = ['data', 'science', 'ai', 'ml', 'python']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary with uppercase keys and values greater than 2.

SciPy
data = {'a': 1, 'b': 3, 'c': 5, 'd': 2}
filtered = { [1]: [2] for k, v in data.items() if v [3] 2 }
print(filtered)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting keys to uppercase.
Using wrong comparison operator.
Using key instead of value in the dictionary values.