Reducing and accumulating numpy arrays

By Martin McBride, 2021-02-27
Tags: arrays data types vectorisation
Categories: numpy

A reducing function takes a collection of values and calculates a single representative value. Standard Pythion has a number of built-in reducing functions that work with lists and other sequences:

  • sum calculates the sum of all the elements (ie adds them all together).
  • max gives the largest element.
  • all is true if every element is true, false if one or more are false.

NumPy allows to reduce using any binary ufunc.


To explain how reduce works, we will take the example of calculating the sum of all elements in an array. To calculate this, you would add the first two numbers, then add the third number to the total, then the fourth number and so on.

Here is how we do this in NumPy:

a = np.array([1, 2, 3, 4])
b = np.add.reduce(a)

In this case, reduce is actually a method of the np.add function. That might seem odd at first, but remember that in Python, functions are objects, and a function object can have methods of its own.

The reduce method of the add function start with the value 0 and adds each element of the array, one by one. This calculates 0 + 1 + 2 + 3 + 4, the sum of all elements.

You can use reduce with many other ufuncs. For example, this code calculates the product of all the elements:

a = np.array([1, 2, 3, 4])
b = np.mul.reduce(a)

mul.reduce start with 1 and multiples the elements one by one, giving 1 * 1 * 2 * 3 * 4.

Some reductions are so commonly used that NumPy provides short names that you can use instead:

  • sum for add.reduce.
  • product for mul.reduce.
  • alltrue for and.reduce.
  • sometrue for or.reduce.

5.4.2 Accumulation

Accumulation is a bit like reduction, but the result is a running total array, rather than a single value. For example:

a = np.array([1, 2, 3, 4])
b = np.add.accumulate(a)


b = [ 1  3  6 10]

The values in the array are 1, 1+2, 1+2+3, 1+2+3+4.

You can use the short name cumsum for add.accumulate, and cumproduct for product.accumulate.

See also

If you found this article useful, you might be interested in the book NumPy Recipes or other books by the same author.

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest