NumPy sum()
The numpy.sum()
function computes the sum of array elements over a specified axis. It can sum all elements or perform summation along a particular axis.
Syntax
</>
Copy
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
a | array_like | Input array containing elements to sum. |
axis | None, int, or tuple of ints, optional | Specifies the axis along which summation is performed. Default (None ) sums all elements. |
dtype | data-type, optional | Data type of the returned array and accumulator. |
out | ndarray, optional | Alternative output array where the result is stored. Must have the same shape as expected output. |
keepdims | bool, optional | If True , retains the reduced dimensions with size one for broadcasting. |
initial | scalar, optional | Starting value for the sum. |
where | array_like of bool, optional | Specifies which elements to include in the sum. |
Return Value
Returns the sum of array elements. If axis=None
, a scalar is returned; otherwise, an array with the specified axis removed is returned.
Examples
1. Summing All Elements in an Array
Computing the sum of all elements in a 1D NumPy array.
</>
Copy
import numpy as np
# Define a 1D array
arr = np.array([1, 2, 3, 4, 5])
# Compute sum of all elements
total_sum = np.sum(arr)
# Print the result
print("Sum of all elements:", total_sum)
Output:
Sum of all elements: 15

2. Summing Along an Axis
Computing the sum along different axes of a 2D array.
</>
Copy
import numpy as np
# Define a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Sum along axis 0 (columns)
sum_axis0 = np.sum(arr, axis=0)
# Sum along axis 1 (rows)
sum_axis1 = np.sum(arr, axis=1)
# Print the results
print("Sum along axis 0 (column-wise sum):", sum_axis0)
print("Sum along axis 1 (row-wise sum):", sum_axis1)
Output:
Sum along axis 0 (column-wise sum): [5 7 9]
Sum along axis 1 (row-wise sum): [ 6 15]

3. Using keepdims=True
to Preserve Dimensions
Preserving reduced dimensions for broadcasting.
</>
Copy
import numpy as np
# Define a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Sum along axis 1 while keeping dimensions
sum_keepdims = np.sum(arr, axis=1, keepdims=True)
# Print the result
print("Sum along axis 1 with keepdims=True:", sum_keepdims)
Output:
Sum along axis 1 with keepdims=True: [[ 6]
[15]]

4. Specifying an Initial Value
Using an initial value to start the summation from a specific number.
</>
Copy
import numpy as np
# Define an array
arr = np.array([1, 2, 3, 4, 5])
# Compute sum with an initial value of 10
sum_with_initial = np.sum(arr, initial=10)
# Print the result
print("Sum with initial value 10:", sum_with_initial)
Output:
Sum with initial value 10: 25

5. Using the where
Parameter
Summing only selected elements using a condition.
</>
Copy
import numpy as np
# Define an array
arr = np.array([1, 2, 3, 4, 5])
# Define a mask to sum only even numbers
mask = (arr % 2 == 0)
# Compute sum where mask is True
sum_with_condition = np.sum(arr, where=mask)
# Print the result
print("Sum of even numbers:", sum_with_condition)
Output:
Sum of even numbers: 6
