Challenge - 5 Problems
Ufunc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numpy ufunc with broadcasting
What is the output of this code snippet using numpy universal functions and broadcasting?
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[1], [2], [3]]) result = np.add(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
Remember numpy broadcasts arrays to compatible shapes before applying ufuncs.
✗ Incorrect
The array arr1 has shape (3,) and arr2 has shape (3,1). Broadcasting expands arr1 to (3,3) by repeating its row, and arr2 to (3,3) by repeating its column. Adding element-wise results in [[1+1,2+1,3+1],[1+2,2+2,3+2],[1+3,2+3,3+3]] = [[2,3,4],[3,4,5],[4,5,6]].
❓ data_output
intermediate2:00remaining
Result of numpy sqrt ufunc on negative values
What is the output array when applying numpy.sqrt to this array containing negative values?
NumPy
import numpy as np arr = np.array([4, 9, -1, 16]) result = np.sqrt(arr) print(result)
Attempts:
2 left
💡 Hint
Square root of negative numbers is not defined in real numbers, numpy returns nan with a warning.
✗ Incorrect
Numpy sqrt returns nan for negative inputs and issues a runtime warning. So sqrt(-1) is nan, while sqrt(4), sqrt(9), sqrt(16) are 2, 3, 4 respectively.
❓ visualization
advanced3:00remaining
Visualizing numpy ufunc output with vectorize
Which plot correctly shows the output of applying a numpy ufunc to a range of values using np.vectorize?
NumPy
import numpy as np import matplotlib.pyplot as plt def custom_func(x): return np.sin(x) if x >= 0 else 0 vec_func = np.vectorize(custom_func) x = np.linspace(-np.pi, np.pi, 100) y = vec_func(x) plt.plot(x, y) plt.title('Custom function output') plt.show()
Attempts:
2 left
💡 Hint
The function returns sine only for non-negative x, zero otherwise.
✗ Incorrect
The vectorized function applies the condition element-wise. For negative x values, output is zero, for non-negative x, output is sine(x). The plot shows half sine wave starting at zero and zero on the negative side.
🧠 Conceptual
advanced2:00remaining
Understanding numpy ufunc reduce method
What is the output of this code using numpy's ufunc reduce method?
NumPy
import numpy as np arr = np.array([1, 2, 3, 4]) result = np.add.reduce(arr) print(result)
Attempts:
2 left
💡 Hint
ufunc reduce applies the operation cumulatively to the array elements.
✗ Incorrect
np.add.reduce sums all elements of the array: 1+2+3+4 = 10.
🔧 Debug
expert2:30remaining
Identify the error in this numpy ufunc usage
What error does this code raise when applying numpy's power ufunc with incompatible shapes?
NumPy
import numpy as np base = np.array([1, 2, 3]) exponent = np.array([[1, 2], [3, 4]]) result = np.power(base, exponent) print(result)
Attempts:
2 left
💡 Hint
Check if the shapes of base and exponent arrays can be broadcast together.
✗ Incorrect
The base array has shape (3,) and exponent has shape (2,2). These shapes are not compatible for broadcasting, so numpy raises a ValueError about broadcasting.