0
0
NumPydata~20 mins

Understanding ufunc methods (reduce, accumulate) in NumPy - Practice Questions & Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ufunc Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A10
B[1 3 6 10]
C24
D[1 2 3 4]
Attempts:
2 left
💡 Hint
Reduce sums all elements into a single value.
Predict Output
intermediate
2: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)
A[1 3 6 10]
B24
C[1 2 6 24]
D[1 2 3 4]
Attempts:
2 left
💡 Hint
Accumulate multiplies elements step by step.
data_output
advanced
2: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)
A(3,)
B(2,)
C(2, 3)
D(1, 3)
Attempts:
2 left
💡 Hint
Reducing along axis 1 sums each row into one value.
🧠 Conceptual
advanced
2:00remaining
Difference between reduce and accumulate
Which statement correctly describes the difference between reduce and accumulate ufunc methods?
A<code>reduce</code> returns a single aggregated value; <code>accumulate</code> returns intermediate results as an array.
B<code>reduce</code> returns intermediate results; <code>accumulate</code> returns a single value.
CBoth return the same output but differ in speed.
D<code>reduce</code> works only on 1D arrays; <code>accumulate</code> works on any dimension.
Attempts:
2 left
💡 Hint
Think about how many values each method returns.
🔧 Debug
expert
2: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)
ANo error, prints [1 3 6]
BTypeError: 'int' object is not iterable
CValueError: operands could not be broadcast together
DAxisError: axis 1 is out of bounds for array of dimension 1
Attempts:
2 left
💡 Hint
Check the array dimensions and axis argument.