0
0
NumPydata~20 mins

np.frompyfunc() for ufunc creation in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.frompyfunc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple ufunc created with np.frompyfunc()
What is the output of this code snippet that creates a ufunc to add 10 to each element?
NumPy
import numpy as np

def add_ten(x):
    return x + 10

ufunc_add_ten = np.frompyfunc(add_ten, 1, 1)
result = ufunc_add_ten(np.array([1, 2, 3]))
print(result)
ATypeError
B[1 2 3 10]
C[11 12 13]
D['11' '12' '13']
Attempts:
2 left
💡 Hint
np.frompyfunc returns an object array by default.
data_output
intermediate
2:00remaining
Result shape and type from a ufunc with multiple outputs
Given this ufunc that returns two values per input, what is the shape and type of the output when applied to a 1D array of length 3?
NumPy
import numpy as np

def square_and_cube(x):
    return x**2, x**3

ufunc_sc = np.frompyfunc(square_and_cube, 1, 2)
result = ufunc_sc(np.array([2, 3, 4]))
print(type(result))
print(len(result))
print(result[0])
AType: tuple, Length: 2, First element: [4 9 16]
BTypeError
CType: tuple, Length: 2, First element: [4 9 16] as object array
DType: numpy.ndarray, Length: 3, First element: 4
Attempts:
2 left
💡 Hint
np.frompyfunc returns object arrays even for multiple outputs.
🔧 Debug
advanced
2:00remaining
Identify the error in ufunc creation with np.frompyfunc()
What error will this code raise and why?
NumPy
import numpy as np

def faulty_func(x, y):
    return x + y

ufunc_faulty = np.frompyfunc(faulty_func, 1, 1)
ATypeError: Number of input arguments does not match function signature
BNo error, ufunc created successfully
CValueError: Invalid number of inputs for frompyfunc
DSyntaxError
Attempts:
2 left
💡 Hint
Check the number of input arguments specified vs the function definition.
🚀 Application
advanced
2:00remaining
Using np.frompyfunc() to vectorize a string operation
You want to create a ufunc that converts strings to uppercase. Which option correctly creates and applies this ufunc to a numpy array of strings?
NumPy
import numpy as np

# Your code here

result = ufunc_upper(np.array(['apple', 'banana', 'cherry']))
print(result)
Aufunc_upper = np.frompyfunc(lambda s: s.upper(), 1, 1)
Bufunc_upper = np.frompyfunc(str.upper, 1, 1)
Cufunc_upper = np.frompyfunc(lambda s: s.upper, 1, 1)
Dufunc_upper = np.frompyfunc(lambda s: s.upper, 0, 1)
Attempts:
2 left
💡 Hint
Remember to call the method with parentheses to execute it.
🧠 Conceptual
expert
2:00remaining
Understanding output types of np.frompyfunc() ufuncs
Which statement about the output of ufuncs created with np.frompyfunc() is TRUE?
AThey return object arrays regardless of input types, because they wrap Python functions.
BThey always return numpy arrays with the same dtype as the input arrays.
CThey return lists instead of numpy arrays.
DThey return arrays with dtype inferred from the function's return type.
Attempts:
2 left
💡 Hint
np.frompyfunc wraps Python functions and cannot infer fixed dtypes.