Table of Contents
ToggleIn mathematics, a sum is the result of adding two or more numbers together. For example, the sum of 2 and 3 is 5.
It is often represented using the plus symbol (+). Summation can also involve adding a sequence of numbers, often using the Greek letter sigma (Σ) to denote the operation.
The sum() function in NumPy calculates the sum of array elements along a specified axis, providing flexibility to sum across rows, columns, or the entire array.
Following is the basic syntax of the sum() function in NumPy −
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)
Where,
If you have a one-dimensional array, you can use the numpy.sum() function to calculate the sum of all its elements. Following is an example −
# Open Compiler
import numpy as np
# Define a 1D array
arr = np.array([1, 2, 3, 4, 5])
# Calculate the sum of all elements
total_sum = np.sum(arr)
print("Total sum of the array:", total_sum)
Output:
Following is the output obtained −
Total sum of the array: 15
In a two-dimensional array, you can compute the sum along a specific axis. For example, summing along the rows or columns −
# Open Compiler
import numpy as np
# Define a 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Sum along rows (axis=1)
sum_rows = np.sum(arr_2d, axis=1)
# Sum along columns (axis=0)
sum_columns = np.sum(arr_2d, axis=0)
print("Sum along rows:", sum_rows)
print("Sum along columns:", sum_columns)
Output:
Following is the output obtained −
Sum along rows: [ 6 15 24]
Sum along columns: [12 15 18]
You can also specify the data type in which you want the sum to be computed. This is especially useful when dealing with large numbers or when you need the result in a specific precision (such as float64). Here is an example −
# Open Compiler
import numpy as np
# Define an array of integers
arr_int = np.array([10, 20, 30])
# Calculate the sum with a specified data type (float64)
sum_float = np.sum(arr_int, dtype=np.float64)
print("Sum with dtype float64:", sum_float)
Output:
Following is the output obtained −
Sum with dtype float64: 60.0
The keepdims parameter helps preserve the dimensionality of the original array after the sum operation. If set to True, the result will have the same number of dimensions as the input array, but the size of the summed axes will be reduced to one.
# Open Compiler
import numpy as np
# Define a 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Sum along columns while keeping dimensions
sum_keepdims = np.sum(arr_2d, axis=0, keepdims=True)
print("Sum with keepdims=True:", sum_keepdims)
Output:
Following is the output obtained −
Sum with keepdims=True: [[12 15 18]]
The numpy.sum() function has a wide range of applications in scientific computing, data analysis, and machine learning. Some common use cases are −
NumPy is optimized for fast array operations, and the numpy.sum() function is highly efficient. However, there are a few ways to further optimize your sum calculations −
out parameter, which avoids creating a new array and helps save memory.Key Takeaway: Master the NumPy sum function at Vista Academy!
