The function square_plus_one returns x**2 + 1. For inputs 1, 2, 3, the outputs are 1^2+1=2, 2^2+1=5, 3^2+1=10.
Step 2: Apply vectorized function on array
Using np.vectorize, the function applies element-wise on the array [1, 2, 3], producing [2, 5, 10].
Final Answer:
[2 5 10] -> Option A
Quick Check:
Element-wise square plus one = [2 5 10] [OK]
Hint: Apply function to each element, then collect results [OK]
Common Mistakes:
Forgetting to add 1 after squaring
Confusing output with input array
Assuming vectorize changes function logic
4. Identify the error in this code snippet:
import numpy as np
def add_five(x):
return x + 5
vfunc = np.vectorize(add_five)
arr = [1, 2, 3]
result = vfunc(arr)
print result
medium
A. np.vectorize cannot be used with functions that add numbers
B. The function add_five should return x * 5 instead of x + 5
C. The print statement is missing parentheses
D. The input array should be a numpy array, not a list
Solution
Step 1: Check the print statement syntax
In Python 3, print is a function and requires parentheses like print(result). print result causes a SyntaxError.
Step 2: Verify other parts of the code
The function logic is correct, np.vectorize works fine with addition functions, and input lists are accepted by np.vectorize.
Final Answer:
The print statement is missing parentheses -> Option C
Quick Check:
print requires parentheses in Python 3 [OK]
Hint: print requires parentheses in Python 3 [OK]
Common Mistakes:
Forgetting parentheses on print statement
Assuming list input is invalid for vectorize
Changing function logic unnecessarily
5. You want to apply a custom function that returns 'even' if a number is even and 'odd' if it is odd, on a numpy array. Which code correctly uses np.vectorize() to achieve this?
def even_odd(x):
if x % 2 == 0:
return 'even'
else:
return 'odd'
arr = np.array([1, 2, 3, 4])
# What next?
hard
A. result = np.apply_along_axis(even_odd, 1, arr)
print(result)
B. result = even_odd(arr)
print(result)
C. vfunc = np.vectorize(even_odd)
result = even_odd(arr.tolist())
print(result)
D. vfunc = np.vectorize(even_odd)
result = vfunc(arr)
print(result)
Solution
Step 1: Vectorize the custom function
Use np.vectorize(even_odd) to create a vectorized version that works element-wise on arrays.
Step 2: Apply vectorized function on numpy array
Call the vectorized function with the numpy array arr to get the result array of 'even'/'odd' strings.
Step 3: Check other options
result = even_odd(arr)
print(result) fails because even_odd does not support array input directly. vfunc = np.vectorize(even_odd)
result = even_odd(arr.tolist())
print(result) fails because even_odd does not support list input. result = np.apply_along_axis(even_odd, 1, arr)
print(result) fails with axis out of bounds for 1D array.
Final Answer:
vfunc = np.vectorize(even_odd)
result = vfunc(arr)
print(result) -> Option D
Quick Check:
Vectorize then apply on numpy array = vfunc = np.vectorize(even_odd)
result = vfunc(arr)
print(result) [OK]
Hint: Vectorize first, then apply on numpy array directly [OK]
Common Mistakes:
Calling function directly on array without vectorize
Calling non-vectorized function on list input
Using invalid axis in apply_along_axis
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.