0
0
NumPydata~20 mins

Generalized ufuncs concept in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generalized ufuncs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple generalized ufunc call
What is the output of this code using a generalized ufunc with specified core dimensions?
NumPy
import numpy as np

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

my_gufunc = np.frompyfunc(my_func, 2, 1)

x = np.array([[1, 2], [3, 4]])
y = np.array([[10, 20], [30, 40]])

result = my_gufunc(x, y)
print(result)
A
[[1 2]
 [3 4]]
B
[[11 12]
 [13 14]]
C
[[10 20]
 [30 40]]
D
[[11 22]
 [33 44]]
Attempts:
2 left
💡 Hint
Think about what the function does element-wise on the arrays.
data_output
intermediate
1:30remaining
Shape of output from a generalized ufunc with core dimensions
Given the following generalized ufunc call, what is the shape of the output array?
NumPy
import numpy as np

def func(a, b):
    return a * b

my_gufunc = np.frompyfunc(func, 2, 1)

x = np.ones((3, 4))
y = np.ones((3, 4))

result = my_gufunc(x, y)
print(result.shape)
A(4,)
B(4, 3)
C(3, 4)
D(3,)
Attempts:
2 left
💡 Hint
The function is applied element-wise to arrays of the same shape.
🔧 Debug
advanced
2:00remaining
Identify the error in this generalized ufunc definition
What error does this code raise when trying to create a generalized ufunc with core dimensions?
NumPy
import numpy as np

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

# Incorrect core dimensions specification
my_gufunc = np.frompyfunc(my_func, 2, 1)

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

result = my_gufunc(x, y)
print(result)
ANo error, prints [5 7 9]
BValueError: operands could not be broadcast together
CSyntaxError: invalid syntax
DTypeError: frompyfunc does not support core dimensions
Attempts:
2 left
💡 Hint
Check the capabilities of np.frompyfunc regarding core dimensions.
🚀 Application
advanced
2:30remaining
Using a generalized ufunc to compute matrix multiplication
Which option correctly uses a generalized ufunc to perform matrix multiplication on two 2D arrays?
NumPy
import numpy as np

def matmul(a, b):
    return a @ b

# Assume my_gufunc is defined here

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

result = my_gufunc(A, B)
print(result)
Amy_gufunc = np.vectorize(matmul, signature='(m,n),(n,p)->(m,p)')
Bmy_gufunc = np.frompyfunc(matmul, 2, 1)
Cmy_gufunc = np.frompyfunc(lambda x, y: x * y, 2, 1)
Dmy_gufunc = np.vectorize(matmul)
Attempts:
2 left
💡 Hint
Generalized ufuncs with core dimensions require a signature to specify input/output shapes.
🧠 Conceptual
expert
1:30remaining
Understanding core dimensions in generalized ufuncs
Which statement best describes the role of core dimensions in a generalized ufunc?
ACore dimensions define the output data type of the ufunc.
BCore dimensions specify the fixed dimensions that the ufunc operates on element-wise, while broadcasting over other dimensions.
CCore dimensions are the dimensions that are ignored during the ufunc operation.
DCore dimensions determine the number of input arguments the ufunc accepts.
Attempts:
2 left
💡 Hint
Think about how generalized ufuncs handle arrays with different shapes.