0
0
NumPydata~10 mins

np.vectorize() for custom functions 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 a vectorized version of the function square.

NumPy
import numpy as np

def square(x):
    return x ** 2

vectorized_square = np.vectorize([1])

result = vectorized_square(np.array([1, 2, 3]))
print(result)
Drag options to blanks, or click blank then click option'
Alambda x: x*2
Bnp.square
Cnp.array
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a NumPy function like np.square instead of the custom function.
Passing a lambda that does not match the intended operation.
Passing np.array instead of a function.
2fill in blank
medium

Complete the code to vectorize a function that returns 'even' or 'odd' for each number.

NumPy
import numpy as np

def even_or_odd(x):
    if x % 2 == 0:
        return 'even'
    else:
        return 'odd'

vec_func = np.vectorize([1])

result = vec_func(np.array([1, 2, 3, 4]))
print(result)
Drag options to blanks, or click blank then click option'
Anp.vectorize
Beven_or_odd
Clambda x: x % 2
Dnp.array
Attempts:
3 left
💡 Hint
Common Mistakes
Passing np.vectorize itself instead of the function.
Passing a lambda that returns a number instead of 'even' or 'odd'.
Passing np.array instead of a function.
3fill in blank
hard

Fix the error in the code by filling the blank to correctly vectorize the function add_one.

NumPy
import numpy as np

def add_one(x):
    return x + 1

vec_add_one = np.vectorize([1])

arr = np.array([10, 20, 30])
result = vec_add_one(arr)
print(result)
Drag options to blanks, or click blank then click option'
Aadd_one
Badd_one()
Cnp.add
Dlambda x: x + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using add_one() instead of add_one.
Passing np.add which is a different function.
Passing a lambda instead of the named function.
4fill in blank
hard

Fill both blanks to create a vectorized function that returns True if a number is greater than 5 and False otherwise.

NumPy
import numpy as np

def greater_than_five(x):
    return x [1] 5

vec_func = np.vectorize(greater_than_five)

arr = np.array([3, 6, 9])
result = vec_func(arr) == [2]
print(result)
Drag options to blanks, or click blank then click option'
A>
BTrue
CFalse
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the function.
Comparing the result to False instead of True.
Using '==' inside the function instead of '>'.
5fill in blank
hard

Fill all three blanks to create a vectorized function that returns the length of a string if it is longer than 3 characters, else returns 0.

NumPy
import numpy as np

def length_if_long(word):
    return len(word) if len(word) [1] 3 else [2]

vec_length = np.vectorize([3])

words = np.array(['cat', 'elephant', 'dog'])
result = vec_length(words)
print(result)
Drag options to blanks, or click blank then click option'
A>
B0
Clength_if_long
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>' in the condition.
Returning the word instead of 0 in the else part.
Passing a lambda instead of the function name to np.vectorize.