ufunc Methods
Every NumPy universal function carries extra methods — reduce , accumulate , outer , reduceat , and at — that reshape how the operation is applied across an array.
Learn ufunc Methods in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll collapse arrays to a single value, build running totals, form outer products of every pair, reduce over custom slices, and scatter-add repeated indices in place — all from one operation.
What You'll Learn in This Lesson
1 reduce and accumulate
ufunc.reduce applies the operation across an array to collapse it to a single value: np.add.reduce sums, np.multiply.reduce multiplies, np.maximum.reduce finds the max. ufunc.accumulate keeps every intermediate result, so np.add.accumulate is a running total. Pass axis to reduce along rows or columns of a 2D array.
2 outer: Every Pair
ufunc.outer(x, y) applies the operation to every pair of elements, producing a 2D grid of shape (len(x), len(y)) . np.multiply.outer is the classic outer product; np.add.outer builds an addition table.
3 reduceat and at
ufunc.reduceat(a, indices) reduces over the slices defined by the index list — a fast way to sum segments of a flat array. ufunc.at(a, indices, value) performs an unbuffered, in-place update; unlike a[idx] += 1 , it correctly applies every repeated index (a scatter-add).
If you had written arr[idx] += 1 instead, the duplicate indices would each add only once — exactly the bug np.add.at exists to prevent.
🎯 YOUR TURN: Fill in the Blanks
Replace each ___ so the program sums an array and then builds its running total.
Expected output: 20 then [ 2 6 12 20] . (Answers: reduce , accumulate .)
⚠️ Common Errors & Quick Reference
Buffered fancy indexing only adds once per unique index, undercounting duplicates.
✅ Fix: use np.add.at(a, idx, 1) for a correct unbuffered scatter-add.
Without axis , reduce collapses the whole array to one number.
✅ Fix: pass axis=0 (down columns) or axis=1 (across rows) to keep the dimension you want.
Task
Code
Collapse to one value
np.add.reduce(a)
Running total
np.add.accumulate(a)
Every-pair grid
np.multiply.outer(x, y)
Reduce over slices
np.add.reduceat(a, [0, 2, 5])
Scatter-add in place
np.add.at(a, idx, 1)
🎯 Mini Challenge: Price Table and Event Counts
Build a price-by-quantity table with multiply.outer , then count repeated event codes with add.at .
❓ Frequently Asked Questions
Lesson complete — ufunc methods unlocked!
You can now collapse arrays with reduce , build running totals with accumulate , form grids with outer , segment with reduceat , and scatter-add safely with add.at .
🚀 Up next: Statistics — percentile, median, std, and var with the axis argument.
Practice quiz
What does np.add.reduce([1, 2, 3, 4, 5]) return?
- 15
Answer: 15. reduce collapses the array to a single value: 1+2+3+4+5 = 15.
What does np.add.accumulate([1, 2, 3, 4, 5]) return?
- 15
- 5
accumulate keeps every running total.
What does np.multiply.reduce([1, 2, 3, 4, 5]) return?
- 15
- 120
Answer: 120. multiply.reduce gives the product 1*2*3*4*5 = 120.
What does ufunc.outer(x, y) produce?
- A single scalar
- A 1D array
- A 2D grid combining every pair
- The element-wise sum
Answer: A 2D grid combining every pair. outer applies the op to every pair, making a (len(x), len(y)) grid.
What shape does np.multiply.outer(x, y) have for len(x)=3, len(y)=2?
- (3, 2)
- (2, 3)
- (6,)
- (5,)
Answer: (3, 2). outer produces shape (len(x), len(y)) = (3, 2).
What does np.add.reduceat(np.arange(8), [0, 2, 5]) return?
It sums slices [0:2], [2:5], [5:] -> 1, 9, 18.
Why use np.add.at(arr, idx, 1) instead of arr[idx] += 1?
- It is shorter to type
- It is unbuffered, so it counts every repeated index
- It sorts the array
- It returns a new array
Answer: It is unbuffered, so it counts every repeated index. np.add.at is unbuffered and correctly applies duplicate indices.
What does np.add.reduce(m, axis=0) do on a 2D array?
- Sums everything to one number
- Sums across each row
- Transposes m
- Sums down each column
Answer: Sums down each column. axis=0 reduces down columns, summing each column.
Which ufunc method gives a running total?
- reduce
- outer
- at
- accumulate
Answer: accumulate. accumulate keeps every intermediate result, a running total.
np.add.reduce(a) gives the same result as which function?
- np.prod(a)
- np.sum(a)
- np.cumsum(a)
- np.max(a)
Answer: np.sum(a). np.sum is essentially a friendly wrapper around np.add.reduce.
Continue this course
- Previous: Memory Layout & Strides
- Next: Statistics (percentile, std)