0
0
NumPydata~10 mins

Broadcasting compatibility check 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 check if two arrays can be broadcast together using numpy.

NumPy
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[1], [2], [3]])
can_broadcast = np.[1](arr1, arr2)
print(can_broadcast.shape)
Drag options to blanks, or click blank then click option'
Abroadcast_arrays
Bbroadcast
Cshape
Dbroadcast_shapes
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.broadcast_arrays instead of np.broadcast.
Trying to use np.shape to check broadcasting.
2fill in blank
medium

Complete the code to get the broadcasted shape of two arrays using numpy.

NumPy
import numpy as np
shape = np.[1]((3, 1), (1, 4))
print(shape)
Drag options to blanks, or click blank then click option'
Abroadcast_shapes
Bbroadcast_arrays
Cbroadcast
Dshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.broadcast instead of np.broadcast_shapes.
Trying to broadcast actual arrays instead of shapes.
3fill in blank
hard

Fix the error in the code to check if arrays of shapes (2,3) and (3,) can be broadcast together.

NumPy
import numpy as np
arr1 = np.zeros((2, 3))
arr2 = np.zeros((3,))
can_broadcast = np.broadcast(arr1, [1])
print(can_broadcast.shape)
Drag options to blanks, or click blank then click option'
Aarr1
B(3,)
C(3, 2)
Darr2
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the same array twice to np.broadcast.
Passing shape tuples instead of arrays.
4fill in blank
hard

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

NumPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 4}
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 as the dictionary value.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 3 characters.

NumPy
words = ['sun', 'moon', 'star', 'sky']
result = { [1]: [2] for w in words if len(w) [3] 3 }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using original words as keys instead of uppercase.
Using '<' instead of '>' in the condition.