Skip to main content

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

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]])

# Sum of all elements
print("Total sum:", np.sum(arr))

# Sum along each row (axis=1)
print("Row sums:", np.sum(arr, axis=1))

# Sum along each column (axis=0)
print("Column sums:", np.sum(arr, axis=0))

Output:

Total sum: 21
Row sums: [ 6 15]
Column sums: [5 7 9]
Understanding the axis parameter
  • axis=None (default) - sums all elements into a single value.
  • axis=0 - sums down the rows (result has one value per column).
  • axis=1 - sums across the columns (result has one value per row).

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]])

# Without keepdims: result is 1D
print(np.sum(arr, axis=0))

# With keepdims: result stays 2D
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]])

# Add 100 to the total sum
print("Sum with initial=100:", np.sum(arr, initial=100))

# Only sum the first column (skip columns 2 and 3)
print("Selective sum:", np.sum(arr, axis=0, where=[True, False, False]))

Output:

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

If your array contains NaN values, np.sum() will return NaN:

import numpy as np

arr = np.array([1.0, 2.0, np.nan, 4.0])
print("np.sum:", np.sum(arr))
# Output: nan

Use np.nansum() instead, which treats NaN values as zero:

import numpy as np

arr = np.array([1.0, 2.0, np.nan, 4.0])
print("np.nansum:", np.nansum(arr))
# Output: 7.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]])

# Cumulative sum of all elements (flattened)
print("Cumulative sum (flat):", np.cumsum(arr))

# Cumulative sum along each row
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]])

# Product of all elements
print("Total product:", np.prod(arr))

# Product along each row
print("Row products:", np.prod(arr, axis=1))

# Product along each column
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]])

# Multiply the total product by 10
print("Product with initial=10:", np.prod(arr, initial=10))

# Only multiply the first column
print("Selective product:", np.prod(arr, axis=0, where=[True, False, False]))

Output:

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

When using where=[True, False, False], the excluded columns default to the identity element for multiplication, which is 1. Similarly, for np.sum(), excluded elements default to 0.

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]])

# Cumulative product of all elements (flattened)
print("Cumulative product (flat):", np.cumprod(arr))

# Cumulative product along each row
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

FunctionDescriptionReturns
np.sum(arr)Sum of all elementsSingle value
np.sum(arr, axis=0)Sum per column1D array
np.sum(arr, axis=1)Sum per row1D array
np.nansum(arr)Sum ignoring NaN valuesSingle value
np.cumsum(arr)Cumulative (running) sumArray
np.prod(arr)Product of all elementsSingle value
np.prod(arr, axis=0)Product per column1D array
np.prod(arr, axis=1)Product per row1D array
np.cumprod(arr)Cumulative (running) productArray

Practical Example: Combining Sum and Product

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

import numpy as np

# Monthly sales data for 3 products over 4 months
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.