0
0
NumPydata~10 mins

Broadcasting for outer products 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 an outer product of two 1D arrays using broadcasting.

NumPy
import numpy as np

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
outer = x[:, [1]] * y
print(outer)
Drag options to blanks, or click blank then click option'
Areshape(3, 1)
Bnp.newaxis
CNone
Dreshape(1, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape(1, 3) instead of adding a new axis.
Trying to multiply without changing dimensions, causing shape errors.
2fill in blank
medium

Complete the code to compute the outer product of two arrays using broadcasting with shape manipulation.

NumPy
import numpy as np

a = np.array([2, 3])
b = np.array([5, 7, 11])
result = a.[1] * b
print(result)
Drag options to blanks, or click blank then click option'
Areshape(2, 1)
Breshape(1, 2)
Creshape(3, 1)
Dreshape(1, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Reshaping a incorrectly as (1, 2) which makes it a row vector.
Not reshaping a at all, causing shape mismatch.
3fill in blank
hard

Fix the error in the code to correctly compute the outer product using broadcasting.

NumPy
import numpy as np

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
outer = x.[1] * y
print(outer)
Drag options to blanks, or click blank then click option'
Areshape(3, 1)
Breshape(3, 3)
Creshape(1, 3)
Dflatten()
Attempts:
3 left
💡 Hint
Common Mistakes
Using reshape(3, 3) which changes the data shape incorrectly.
Using flatten() which does not add a new axis.
4fill in blank
hard

Fill both blanks to create an outer product using broadcasting with reshaping.

NumPy
import numpy as np

p = np.array([7, 8])
q = np.array([1, 2, 3])
outer_product = p.[1] * q.[2]
print(outer_product)
Drag options to blanks, or click blank then click option'
Areshape(2, 1)
Breshape(1, 3)
Cflatten()
Dreshape(3, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using flatten() which removes dimensions instead of reshaping.
Swapping the shapes causing broadcasting errors.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: {BLANK_2}} for word in words if {{BLANK_2}}
Drag options to blanks, or click blank then click option'
Blen(word)
Clen(word) > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a colon after the word key in the dictionary comprehension.
Using the word itself as the value instead of its length.
Not filtering words by length.