numpy.sum() in Python Last Updated : 28 Aug, 2024 Comments Improve Suggest changes Like Article Like Report This function returns the sum of array elements over the specified axis.Syntax: numpy.sum(arr, axis, dtype, out): Parameters: arr: Input array. axis: The axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along the column and axis = 1 means working along the row. out: Different array in which we want to place the result. The array must have the same dimensions as the expected output. The default is None. initial : [scalar, optional] Starting value of the sum. Return: Sum of the array elements (a scalar value if axis is none) or array with sum values along the specified axis.Example 1: This Python program uses numpy.sum() to calculate the sum of a 1D array. It demonstrates summing with different data types (uint8 and float32) and checks if the result's data type matches np.uint and np.float. The output shows how the sum can vary with different types. Python # Python Program illustrating # numpy.sum() method import numpy as np # 1D array arr = [20, 2, .2, 10, 4] print("\nSum of arr : ", np.sum(arr)) print("Sum of arr(uint8) : ", np.sum(arr, dtype = np.uint8)) print("Sum of arr(float32) : ", np.sum(arr, dtype = np.float32)) print ("\nIs np.sum(arr).dtype == np.uint : ", np.sum(arr).dtype == np.uint) print ("Is np.sum(arr).dtype == np.float : ", np.sum(arr).dtype == np.float) Output:Sum of arr : 36.2Sum of arr(uint8) : 36Sum of arr(float32) : 36.2Is np.sum(arr).dtype == np.uint : FalseIs np.sum(arr).dtype == np.float : TrueExample 2: This Python program uses NumPy to compute the sum of a 2D array arr with different data types. It demonstrates the use of np.sum() to calculate the sum of elements in arr and outputs results for different data types (uint8 and float32). It also checks if the sum's data type matches np.uint or np.float. Python # Python Program illustrating # numpy.sum() method import numpy as np # 2D array arr = [[14, 17, 12, 33, 44], [15, 6, 27, 8, 19], [23, 2, 54, 1, 4,]] print("\nSum of arr : ", np.sum(arr)) print("Sum of arr(uint8) : ", np.sum(arr, dtype = np.uint8)) print("Sum of arr(float32) : ", np.sum(arr, dtype = np.float32)) print ("\nIs np.sum(arr).dtype == np.uint : ", np.sum(arr).dtype == np.uint) print ("Is np.sum(arr).dtype == np.float : ", np.sum(arr).dtype == np.float) Output:Sum of arr : 279Sum of arr(uint8) : 23Sum of arr(float32) : 279.0Is np.sum(arr).dtype == np.uint : FalseIs np.sum(arr).dtype == np.float : FalseExample 3: This Python program uses numpy.sum() to compute the sum of elements in a 2D array. It calculates the total sum, sums along rows (axis=0), sums along columns (axis=1), and sums along columns while keeping the dimensions (keepdims=True). Python # Python Program illustrating # numpy.sum() method import numpy as np # 2D array arr = [[14, 17, 12, 33, 44], [15, 6, 27, 8, 19], [23, 2, 54, 1, 4,]] print("\nSum of arr : ", np.sum(arr)) print("Sum of arr(axis = 0) : ", np.sum(arr, axis = 0)) print("Sum of arr(axis = 1) : ", np.sum(arr, axis = 1)) print("\nSum of arr (keepdimension is True): \n", np.sum(arr, axis = 1, keepdims = True)) Output:Sum of arr : 279Sum of arr(axis = 0) : [52 25 93 42 67]Sum of arr(axis = 1) : [120 75 84]Sum of arr (keepdimension is True): [[120] [ 75] [ 84]] Comment More infoAdvertise with us Next Article numpy.sum() in Python mohit gupta_omg :) Follow Improve Article Tags : Python Python-numpy Python numpy-Mathematical Function Practice Tags : python Similar Reads Python | Numpy matrix.sum() With the help of matrix.sum() method, we are able to find the sum of values in a matrix by using the same method. Syntax : matrix.sum() Return : Return sum of values in a matrix Example #1 : In this example we are able to find the sum of values in a matrix by using matrix.sum() method. Python3 1=1 # 1 min read numpy.add() in Python NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays. numpy.add() SyntaxSyntax :Â numpy.add(arr1, arr2, /, out= 4 min read numpy.all() in Python The numpy.all() function tests whether all array elements along the mentioned axis evaluate to True. Syntax: numpy.all(array, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters :Â array :[array_like]Input array or object whose elements, we need to test. axis 3 min read numpy.any() in Python The numpy.any() function tests whether any array elements along the mentioned axis evaluate to True. Syntax :Â numpy.any(a, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters :Â array :[array_like]Input array or object whose elements, we need to test. axis : 3 min read Python NumPy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.Besides its obvious scientific uses, Numpy can also be used as an efficient m 6 min read Numpy size() function | Python numpy.size() function in Python is used to count the number of elements in a NumPy array. You can use it to get the total count of all elements, or to count elements along a specific axis, such as rows or columns in a multidimensional array. This makes it useful when quickly trying to understand the 2 min read NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read sum() function in Python The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict 3 min read numpy.nansum() in Python numpy.nansum()function is used when we want to compute the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. Syntax : numpy.nansum(arr, axis=None, dtype=None, out=None, keepdims='no value') Parameters : arr : [array_like] Array containing numbers whose sum is desired. If 3 min read numpy.cumsum() in Python numpy.cumsum() function is used to compute the cumulative sum of elements in an array. Cumulative sum refers to a sequence where each element is the sum of all previous elements plus itself. For example, given an array [1, 2, 3, 4, 5], the cumulative sum would be [1, 3, 6, 10, 15]. Let's implement t 3 min read Like