Summations – NumPy uFuncs (Python Tutorial)

Summations NumPy UFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Summations Universal Functions. Learn the usage of these functions with the help of examples.

NumPy Summations

The summation is the addition of a sequence of any kind of element or numbers where these elements and numbers are addends or summands. The result of this summation is their sum or total.

There is a difference between Addition and Summation as in addition it happens between two arguments or numbers whereas summation happens over n elements.

We will take an example for both cases of addition and summation, which help us in understanding in a better way.

First, we will try adding the values:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to add up
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
#we will use the add() function here
c=np.add(a, b)
#now we will print the added list
print(c)

Output.

[ 3 7 11 15]

Now in the second example, we will apply summation:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to sum up
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
#we will use the sum() function here
c=np.sum([a, b])
#now we will print the array
print(c)

Output.

36

We see in these examples, and we get to know the difference as when we add the corresponding values are added. Whereas if we sum up, it will give us some of all the values present in the two arrays.

Summation Over an Axis

When we want to add up the values as per the array, they are present in we use axis. In this, if the value for the axis is given as one, then NumPy will sum the numbers in each array.

let us take an example to get a better understanding:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to sum up
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
#we will use the sum() function here
c=np.sum([a, b],axis=1)
#now we will print the array
print(c)

Output.

[16 20]

Here it is giving us the sum of each array individually. let us take another example:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to sum up
a=np.array([1, 3, 5, 7])
b=np.array([2, 4, 6, 8])
c=np.array([1, 3, 5, 7])
d=np.array([2, 4, 6, 8])
#we will use the sum() function here
e=np.sum([a, b],axis=1)
d=np.sum([c, d],axis=1)
#now we will print the array
print(e,d)

Output.

[16 20] [16 20]

As a result, we get the sums of the arrays individually.

Cumulative sum

In the cumulative sum, we add up the elements in an array but partially. Which means every number in the result will be sum the numbers before it. This can be done with the help of the cumsum() Function.

Let us take an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to sum up
a=np.array([1, 3, 5, 7])
#we will use the cumsum() function here
c=np.cumsum(a)
#now we will print the array
print(c)

Output.

[ 1 4 9 16]

Here we are getting the sum of values partially.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *