0
0
NumPydata~10 mins

outer() for outer operations 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 compute the outer product of two arrays using numpy.

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.[1](arr1, arr2)
print(result)
Drag options to blanks, or click blank then click option'
Aouter
Bdot
Csum
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.dot instead of np.outer, which computes a dot product, not an outer product.
Using sum or mean which are aggregation functions, not for outer products.
2fill in blank
medium

Complete the code to calculate the outer product of two 1D numpy arrays and store it in 'result'.

NumPy
import numpy as np

x = np.array([2, 3])
y = np.array([5, 7])
result = np.[1](x, y)
print(result)
Drag options to blanks, or click blank then click option'
Ainner
Bcross
Cdot
Douter
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.dot or np.inner which compute dot and inner products, not outer.
Using np.cross which is for vector cross product, only for 3D vectors.
3fill in blank
hard

Fix the error in the code to correctly compute the outer product of arrays 'a' and 'b'.

NumPy
import numpy as np

a = np.array([1, 2])
b = np.array([3, 4])
result = np.[1](a, b)
print(result)
Drag options to blanks, or click blank then click option'
Amultiply
Bdot
Couter
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.dot which computes dot product, resulting in a scalar for 1D arrays.
Using np.multiply which does element-wise multiplication, not outer product.
4fill 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: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword > 3
Clen(word) > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length in the dictionary values.
Comparing the word string directly to a number instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word in uppercase to its length only if the length is less than or equal to 4.

NumPy
words = ['data', 'science', 'ai', 'ml']
result = { [1]: [2] for word in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) <= 4
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the key instead of uppercase.
Using the word instead of its length as the value.
Incorrect condition like comparing the word string directly to a number.