Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
numpy.add() in Python
Next article icon

This function returns the sum of array elements over the specified axis.

Syntax: numpy.sum(arr, axis, dtype, out):

Parameters: 

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() methodimportnumpyasnp# 1D arrayarr=[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.2
Sum of arr(uint8) : 36
Sum of arr(float32) : 36.2

Is np.sum(arr).dtype == np.uint : False
Is np.sum(arr).dtype == np.float : True

Example 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() methodimportnumpyasnp# 2D arrayarr=[[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 :  279
Sum of arr(uint8) : 23
Sum of arr(float32) : 279.0

Is np.sum(arr).dtype == np.uint : False
Is np.sum(arr).dtype == np.float : False

Example 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() methodimportnumpyasnp# 2D arrayarr=[[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 :  279
Sum 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]]

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp