LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science NumPy ufunc Summations
What is the difference between summation and addition?
Addition is done between two arguments whereas summation happens over n elements.
Example of Summation: newarr = np.sum([arr1, arr2])
Example of Addition: newarr = np.add(arr1, arr2)
Example 1: Add the values in arr1 to the values in arr2.
Code
import numpy as np
arr1 = np.array([2, 3, 4])
arr2 = np.array([5, 6, 3])
newarr = np.add(arr1, arr2)
print(newarr)
the output will be
[7 9 7]
Example 2: Sum the values in arr1 and the values in arr2.
Code
import numpy as np
arr1 = np.array([2, 3, 4])
arr2 = np.array([5, 6, 3])
newarr = np.sum([arr1, arr2])
print(newarr)
the output will be
23
Summation Over an Axis: If you specify axis=1, NumPy will sum the numbers in each array.
Example 3: Perform summation in the following array over 1st axis.
Code
import numpy as np
arr1 = np.array([2, 3, 4])
arr2 = np.array([5, 6, 3])
newarr = np.sum([arr1, arr2], axis=1)
print(newarr)
the output will be
[ 9 14]
Cummulative Sum: Cummulative sum means partially adding the elements in array.
E.g. The partial sum of [1, 2, 3, 4, 5] would be [1, 1+2, 1+2+3, 1+2+3+4, 1+2+3+4+5] = [1, 3, 6, 10, 15].
Perfom partial sum with the cumsum() function.
Example 4: Perform cummulative summation in the following array.
Code
import numpy as np
arr = np.array([1, 2, 3, 4])
newarr = np.cumsum(arr)
print(newarr)
the output will be
[ 1 3 6 10]
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.