0
0
SciPydata~10 mins

Performance tips and vectorization 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 NumPy array from a Python list.

SciPy
import numpy as np
arr = np.[1]([1, 2, 3, 4])
Drag options to blanks, or click blank then click option'
Avector
Blist
Cmatrix
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' causes an error.
Using 'matrix' creates a different object type.
2fill in blank
medium

Complete the code to compute the element-wise square of a NumPy array.

SciPy
import numpy as np
arr = np.array([1, 2, 3])
squared = arr[1]2
Drag options to blanks, or click blank then click option'
A**
B*
C^
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' multiplies arrays element-wise but does not square.
Using '^' is a bitwise XOR, not exponentiation.
3fill in blank
hard

Fix the error in the code to compute the dot product of two arrays using SciPy.

SciPy
from scipy import [1]
import numpy as np
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
dot_product = [1].dot(arr1, arr2)
Drag options to blanks, or click blank then click option'
Alinalg
Bintegrate
Coptimize
Dstats
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stats' or 'optimize' modules which do not have dot product functions.
Not importing the correct module causes AttributeError.
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
words = ['data', 'ai', 'science', 'ml']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word > 3' compares strings to numbers, causing errors.
Not using len(word) for length calculation.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths if length is greater than 2.

SciPy
words = ['cat', 'to', 'dog', 'a']
result = { [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 2
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Filtering with incorrect length condition.