0
0
NumPydata~10 mins

Understanding ufunc methods (reduce, accumulate) in NumPy - Visual Explanation

Choose your learning style9 modes available
Concept Flow - Understanding ufunc methods (reduce, accumulate)
Start with array
Choose ufunc method
reduce
Apply op
Single value
Output
Start with an array, pick a ufunc method (reduce or accumulate), apply the operation either fully (reduce) or stepwise (accumulate), then get the output.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
red = np.add.reduce(arr)
acc = np.add.accumulate(arr)
print(red)
print(acc)
This code sums all elements using reduce and shows partial sums using accumulate.
Execution Table
StepMethodInput ArrayOperationIntermediate ResultOutput
1reduce[1, 2, 3, 4]1 + 23
2reduce[1, 2, 3, 4]3 + 36
3reduce[1, 2, 3, 4]6 + 41010
1accumulate[1, 2, 3, 4]start with 111
2accumulate[1, 2, 3, 4]1 + 233
3accumulate[1, 2, 3, 4]3 + 366
4accumulate[1, 2, 3, 4]6 + 41010
💡 reduce ends after combining all elements into one value; accumulate ends after computing partial sums for all elements.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
redundefined361010
accundefined13610[1, 3, 6, 10]
Key Moments - 3 Insights
Why does reduce return a single number but accumulate returns an array?
reduce combines all elements step-by-step into one final value (see execution_table rows 1-3 for reduce), while accumulate keeps all intermediate results (see rows 4-7 for accumulate).
Does accumulate start with the first element or zero?
accumulate starts with the first element as the initial value (see execution_table row 4), not zero.
Can reduce and accumulate be used with any ufunc?
Yes, both methods work with numpy ufuncs like add, multiply, maximum, etc., applying the operation in reduce or accumulate style.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of reduce after step 3?
A3
B6
C10
D1
💡 Hint
Check the 'Output' column for reduce at step 3 in the execution_table.
At which step does accumulate first output the value 6?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Intermediate Result' column for accumulate in the execution_table.
If the input array was [2, 2, 2], what would be the final reduce output?
A8
B6
C4
D2
💡 Hint
Sum all elements: 2 + 2 + 2 = 6, similar to reduce output in execution_table.
Concept Snapshot
ufunc methods like reduce and accumulate apply operations on arrays.
reduce combines all elements into one result.
accumulate returns all intermediate results.
Both use the same operation but differ in output shape.
Useful for sums, products, and other element-wise operations.
Full Transcript
We start with an array and choose a numpy ufunc method: reduce or accumulate. Reduce applies the operation step-by-step combining all elements into one final value. Accumulate applies the operation stepwise but keeps all intermediate results, returning an array of partial results. For example, using np.add.reduce on [1, 2, 3, 4] sums all elements to 10. Using np.add.accumulate on the same array returns [1, 3, 6, 10], showing sums after each step. Reduce ends after one final value; accumulate ends after computing all partial sums. This helps understand how numpy ufuncs can be used for different aggregation styles.