NumPy Sum
Submitted by moazkhan on Tuesday, July 14, 2020 - 21:14.
In this tutorial you will learn:
- What is difference between summation and addition?
- Implementation of summation using different functions in Python
- Python Syntax
Summation
Summation is the sum of all the elements of an array, if we are adding up two arrays it would be the index wise addition of elements which will result in another array having the size equal to the size of arrays being added up. Summation and addition are commonly used in mathematics and sciences to carry out basic tasks. Summation is mostly carried out on more than one elements which are contained in some data structures like list, tuple, array etc and it produces a single value.Implementation of Summation
Inorder to enhance the understanding, we will first see the result of adding up two 1D arrays having size 4, here you can observe that the result produced is an array having index wise addition.- #importing the NumPy library
- import numpy as np
- #declaring a first array
- arr_1= np.array([1,2,3,4])
- print('1st array being added up:',arr_1)
- #declaring a second array
- arr_2=np.array([5,6,7,8])
- print('2nd array being added up:',arr_2)
- #adding up array using add() function
- res = np.add(arr_1,arr_2)
- #printing the result
- print('Printing the result of add() function:', res)
- #importing the NumPy library
- import numpy as np
- #declaring an array
- arr_1= np.array([1,2,3,4])
- print('Array being summed up:', arr_1)
- #summing up the contents of an array
- res = np.sum(arr_1)
- #printing the result
- print('The result of sum() function:', res)
- #importing the NumPy library
- import numpy as np
- #declaring an array
- arr= np.array([10,20,30,40])
- print('Array being cummulative summed:', arr)
- #summing up the contents of an array
- res = np.cumsum(arr)
- #printing the result
- print('The result of cumsum() function:', res)
Add new comment
- 189 views