Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.sum

Sums elements of an array over the whole input or selected axes. It supports dtype control, dimension preservation, conditional selection, an initial value, and an optional output array.

Since
1.7.0

How it works

np.sum converts the input to an array-like sum operation, combining elements over all axes by default or only the selected axis or axes. The accumulator and result type can be controlled with dtype, while keepdims preserves reduced dimensions and where selects participating elements.

Syntax

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)

Parameters

NameTypeDescription
aarray_likeArray-like input whose elements are summed.
axisNone, int, or tuple of ints, optionalAxis or axes to reduce; None reduces all axes.
dtypedtype, optionalResult and accumulator data type.
outndarray, optionalArray receiving the result.
keepdimsbool, optionalWhether reduced axes remain as dimensions of size one.
initialscalar, optionalValue added to the reduction.
wherearray_like of bool, optionalBoolean condition selecting elements to include.

Returns

sum_along_axis: ndarray or scalar. The reduced result has the specified axes removed; with out supplied, a reference to out is returned.

Key points

  • Use np.sum to reduce array elements by addition over the whole input or selected axes.
  • Leaving axis as None produces one total for the entire input.
  • Negative axis values address dimensions from the trailing side, so axis=-1 means the final dimension.
  • A tuple lets one call reduce multiple dimensions at once.
  • dtype controls both the result type and the type used for accumulation.

Common mistakes

  • For small-integer inputs, the accumulator is promoted to the platform integer type when the input integer type has less precision; explicitly set dtype when a particular accumulation type is required.
  • Integer sums can overflow silently because integer arithmetic is modular; choose a wider dtype when the expected total may exceed the input type's range.
  • Floating-point results can depend on summation order and precision; use an appropriate dtype when numerical accuracy matters.

Example: Summing an array along selected axes

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sum(a))
print(np.sum(a, axis=0))
print(np.sum(a, axis=1, keepdims=True))
print(np.sum(a, where=a % 2 == 0))

This computes the total, column-wise, row-wise with a retained dimension, and conditionally over even elements.

When to use it

Whole-array totals

Aggregate every value in a vector, matrix, or higher-dimensional array into one total.

Axis-wise reductions

Compute per-feature, per-row, or multi-dimensional aggregates without explicit Python loops.

Masked aggregation

Sum only entries satisfying a boolean mask, such as valid or selected observations.

Broadcast-compatible or managed output

Keep reduced dimensions or write results into preallocated storage for downstream array operations.

Interview questions

QWhat does axis control in np.sum?

By default, axis=None reduces every element and returns a scalar for a 0-D input or when axis is None. Supplying an integer reduces that axis, while a tuple reduces all listed axes.

Follow-up: How can the reduced dimensions be retained? Set keepdims=True, which leaves dimensions of length one in the result.

QWhat is the purpose of dtype?

dtype specifies both the output array type and the accumulator type used during summation. It can therefore prevent unwanted low-precision accumulation or integer overflow when a wider type is selected.

Follow-up: What happens if dtype is omitted for a small integer array? NumPy promotes the accumulator to the default platform integer type.

QHow do where, initial, and out affect the operation?

where supplies a boolean condition identifying which elements participate in the reduction. initial adds a value to the reduction, and out provides an alternative array in which to store the result.

Follow-up: What does np.sum return when out is supplied? It returns a reference to out rather than a separate result array.

  • numpy.add.reduce
  • numpy.cumsum
  • numpy.prod
  • numpy.nansum

Source: numpy.sum. Checked 2026-10-15.