0
0
NumPydata~10 mins

Why advanced broadcasting matters in NumPy - 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 2D array of shape (3, 3) filled with ones.

NumPy
import numpy as np
array = np.[1]((3, 3))
Drag options to blanks, or click blank then click option'
Aempty
Bzeros
Cones
Dfull
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.zeros instead of np.ones
Using np.empty which 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.array([[1, 2, 3], [4, 5, 6]])
arr1d = np.array([10, 20, 30])
result = arr2d + [1]
Drag options to blanks, or click blank then click option'
Aarr1d.reshape(3, 1)
Barr1d.T
Carr1d.reshape(1, 3)
Darr1d
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape(3, 1) which is incompatible
Using transpose which does not change shape as needed
3fill in blank
hard

Fix the error in the code that tries to add arrays with incompatible shapes.

NumPy
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([10, 20, 30])
result = arr1 + [1]
Drag options to blanks, or click blank then click option'
Aarr2.reshape(3, 1)
Barr2
Carr2.reshape(2, 3)
Darr2.reshape(1, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape(3, 1) which mismatches dimensions
Not reshaping at all causing shape error
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', '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 instead of length
Using word > 3 which compares strings incorrectly
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 3.

NumPy
words = ['data', 'ai', 'science', 'ml']
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) > 3
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase
Using word instead of length for values