Whole-array totals
Aggregate every value in a vector, matrix, or higher-dimensional array into one total.
NumPy · Function
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.
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.
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)| Name | Type | Description |
|---|---|---|
a | array_like | Array-like input whose elements are summed. |
axis | None, int, or tuple of ints, optional | Axis or axes to reduce; None reduces all axes. |
dtype | dtype, optional | Result and accumulator data type. |
out | ndarray, optional | Array receiving the result. |
keepdims | bool, optional | Whether reduced axes remain as dimensions of size one. |
initial | scalar, optional | Value added to the reduction. |
where | array_like of bool, optional | Boolean condition selecting elements to include. |
sum_along_axis: ndarray or scalar. The reduced result has the specified axes removed; with out supplied, a reference to out is returned.
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.
Aggregate every value in a vector, matrix, or higher-dimensional array into one total.
Compute per-feature, per-row, or multi-dimensional aggregates without explicit Python loops.
Sum only entries satisfying a boolean mask, such as valid or selected observations.
Keep reduced dimensions or write results into preallocated storage for downstream array operations.
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.
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.
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.reducenumpy.cumsumnumpy.prodnumpy.nansum