Challenge - 5 Problems
Ufunc Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numpy add.reduce on array
What is the output of this code snippet using
numpy.add.reduce?NumPy
import numpy as np arr = np.array([1, 2, 3, 4]) result = np.add.reduce(arr) print(result)
Attempts:
2 left
💡 Hint
Reduce sums all elements into a single value.
✗ Incorrect
np.add.reduce sums all elements of the array into one number. Here, 1+2+3+4 = 10.
❓ Predict Output
intermediate2:00remaining
Output of numpy multiply.accumulate on array
What is the output of this code snippet using
numpy.multiply.accumulate?NumPy
import numpy as np arr = np.array([1, 2, 3, 4]) result = np.multiply.accumulate(arr) print(result)
Attempts:
2 left
💡 Hint
Accumulate multiplies elements step by step.
✗ Incorrect
np.multiply.accumulate returns the cumulative product: [1, 1*2=2, 2*3=6, 6*4=24].
❓ data_output
advanced2:00remaining
Result shape after reduce on 2D array
Given this 2D array, what is the shape of the result after applying
np.add.reduce along axis 1?NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.add.reduce(arr, axis=1) print(result.shape)
Attempts:
2 left
💡 Hint
Reducing along axis 1 sums each row into one value.
✗ Incorrect
Reducing along axis 1 collapses columns, so each row becomes one sum. The result shape is (2,).
🧠 Conceptual
advanced2:00remaining
Difference between reduce and accumulate
Which statement correctly describes the difference between
reduce and accumulate ufunc methods?Attempts:
2 left
💡 Hint
Think about how many values each method returns.
✗ Incorrect
reduce combines all elements into one value. accumulate shows the step-by-step results as an array.
🔧 Debug
expert2:00remaining
Identify error in ufunc accumulate usage
What error will this code raise and why?
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = np.add.accumulate(arr, axis=1) print(result)
Attempts:
2 left
💡 Hint
Check the array dimensions and axis argument.
✗ Incorrect
The array is 1D, so axis=1 is invalid. This causes an AxisError.