Skip to content

Python NumPy: How to Find the Sum and Product of NumPy Array Elements in Python

Learn how to compute sums and products of NumPy array elements using np.sum(), np.prod(), and their cumulative counterparts (np.cumsum(), np.cumprod()), with axis control and NaN handling.

NumPy provides highly optimized functions for computing sums and products of array elements - operations that are fundamental in data analysis, scientific computing, and machine learning. Whether you need the total sum of all elements, results along a specific axis, or cumulative calculations, NumPy has you covered.

This guide walks through the key functions for computing sums and products, with practical examples and explanations of their most useful parameters.

Computing the Sum of Array Elements

Using np.sum()

The np.sum() function returns the sum of array elements over a given axis. Without specifying an axis, it sums all elements in the array.

Syntax:

numpy.sum(array, axis=None, dtype=None, keepdims=False, initial=0, where=True)

Basic Usage

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print("Total sum:", np.sum(arr))

print("Row sums:", np.sum(arr, axis=1))

print("Column sums:", np.sum(arr, axis=0))

Output:

Total sum: 21
Row sums: [ 6 15]
Column sums: [5 7 9]

Using keepdims to Preserve Dimensions

The keepdims parameter keeps the reduced axis in the result as a dimension of size 1. This is useful for broadcasting in subsequent operations:

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print(np.sum(arr, axis=0))

print(np.sum(arr, axis=0, keepdims=True))

Output:

[5 7 9]
[[5 7 9]]

Using initial and where Parameters

  • initial - a starting value added to the sum.
  • where - a boolean mask that controls which elements are included.
import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print("Sum with initial=100:", np.sum(arr, initial=100))

print("Selective sum:", np.sum(arr, axis=0, where=[True, False, False]))

Output:

Sum with initial=100: 121
Selective sum: [5 0 0]

Using np.cumsum() for Cumulative Sums

np.cumsum() returns a running total - each element in the result is the sum of all preceding elements plus itself.

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print("Cumulative sum (flat):", np.cumsum(arr))

print("Cumulative sum (by row):")
print(np.cumsum(arr, axis=1))

Output:

Cumulative sum (flat): [ 1  3  6 10 15 21]
Cumulative sum (by row):
[[ 1  3  6]
 [ 4  9 15]]

Cumulative sums are particularly useful for computing running totals, prefix sums, or integration approximations.

Computing the Product of Array Elements

Using np.prod()

The np.prod() function works just like np.sum(), but computes the product instead. It accepts the same parameters: axis, keepdims, initial, and where.

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print("Total product:", np.prod(arr))

print("Row products:", np.prod(arr, axis=1))

print("Column products:", np.prod(arr, axis=0))

Output:

Total product: 720
Row products: [  6 120]
Column products: [ 4 10 18]

Using initial and where Parameters

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print("Product with initial=10:", np.prod(arr, initial=10))

print("Selective product:", np.prod(arr, axis=0, where=[True, False, False]))

Output:

Product with initial=10: 7200
Selective product: [4 1 1]

Using np.cumprod() for Cumulative Products

np.cumprod() computes the running product of array elements, analogous to how np.cumsum() computes running sums.

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

print("Cumulative product (flat):", np.cumprod(arr))

print("Cumulative product (by row):")
print(np.cumprod(arr, axis=1))

Output:

Cumulative product (flat): [  1   2   6  24 120 720]
Cumulative product (by row):
[[  1   2   6]
 [  4  20 120]]

Quick Reference

Function Description Returns
np.sum(arr) Sum of all elements Single value
np.sum(arr, axis=0) Sum per column 1D array
np.sum(arr, axis=1) Sum per row 1D array
np.nansum(arr) Sum ignoring NaN values Single value
np.cumsum(arr) Cumulative (running) sum Array
np.prod(arr) Product of all elements Single value
np.prod(arr, axis=0) Product per column 1D array
np.prod(arr, axis=1) Product per row 1D array
np.cumprod(arr) Cumulative (running) product Array

Practical Example: Combining Sum and Product

Here's a real-world example that computes both summary statistics for a dataset:

import numpy as np

sales = np.array([
    [120, 200, 340],
    [150, 180, 290],
    [130, 210, 310],
    [160, 190, 350]
])

print("Monthly totals (sum per row):", np.sum(sales, axis=1))
print("Product totals (sum per column):", np.sum(sales, axis=0))
print("Grand total:", np.sum(sales))
print("Cumulative monthly totals:", np.cumsum(np.sum(sales, axis=1)))

Output:

Monthly totals (sum per row): [660 620 650 700]
Product totals (sum per column): [ 560  780 1290]
Grand total: 2630
Cumulative monthly totals: [ 660 1280 1930 2630]

Conclusion

NumPy's sum and product functions provide a fast, flexible way to perform aggregation operations on arrays of any size.

Use np.sum() and np.prod() for straightforward aggregations, leverage the axis parameter to operate along specific dimensions, and turn to np.cumsum() and np.cumprod() when you need running totals or products.

For datasets containing missing values, always use np.nansum() to avoid unexpected NaN results.

Navigation