0
0
NumPydata~10 mins

Broadcasting performance implications in NumPy - 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 2D array of shape (3, 3) filled with ones using NumPy.

NumPy
import numpy as np
arr = np.[1]((3, 3))
Drag options to blanks, or click blank then click option'
Azeros
Bones
Cempty
Dfull
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.zeros creates an array of zeros, not ones.
Using np.empty creates uninitialized values.
2fill in blank
medium

Complete the code to add a 1D array to each row of a 2D array using broadcasting.

NumPy
import numpy as np
arr2d = np.ones((3, 3))
arr1d = np.array([1, 2, 3])
result = arr2d [1] arr1d
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' multiplies instead of adding.
Using '-' subtracts instead of adding.
3fill in blank
hard

Fix the error in the code to correctly broadcast a 1D array to a 2D array for multiplication.

NumPy
import numpy as np
arr2d = np.ones((3, 3))
arr1d = np.array([1, 2, 3])
result = arr2d * arr1d.[1]((3, 1))
Drag options to blanks, or click blank then click option'
Areshape
Bflatten
Ctranspose
Dravel
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten or ravel returns a 1D array, no shape change.
Using transpose on 1D array has no effect.
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.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dlen(words)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' as value instead of length.
Using 'len(words)' which is total words, not per word.
5fill in blank
hard

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

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