0
0
NumPydata~10 mins

Why custom ufuncs matter 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 NumPy array.

NumPy
import numpy as np
arr = np.[1]([1, 2, 3, 4])
Drag options to blanks, or click blank then click option'
Atuple
Blist
Carray
Dmatrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python list instead of np.array.
Trying to use np.list which does not exist.
2fill in blank
medium

Complete the code to define a simple custom ufunc using NumPy's vectorize.

NumPy
import numpy as np
@np.vectorize
def square(x):
    return x [1] x
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division or subtraction which do not square the number.
3fill in blank
hard

Fix the error in the custom ufunc that adds 10 to each element.

NumPy
import numpy as np
@np.vectorize
def add_ten(x):
    return x [1] 10
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division which changes the value differently.
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 = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
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 value instead of its length.
Using < instead of > in the condition.
5fill in blank
hard

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

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if len(w) [3] 4 }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Using < instead of > in the condition.
Mapping words instead of their lengths.