Introduction to NumPy Summations in Python

In This Article, You Will Learn About Numpy Summations.

Numpy Summations – Before moving ahead, let’s know a bit of Python NumPy Log Functions

Summations

A view to Addition & Summation

Addition is the process of adding two arguments whereas summations take place over n number of elements.    

Example – Adding the value of one array to another array.

import numpy as np

array_1 = np.array([11, 12, 13])
array_2 = np.array([14, 15, 16])

updated_array = np.add(array_1, array_2)

print(updated_array)
Output - 

[25 27 29]

As a result, it returns an array adding each element of the array_1 to array_2 in vertical form.

Example – Summing the value of one array to another array.

import numpy as np

array_1 = np.array([11, 12, 13])
array_2 = np.array([14, 15, 16])

updated_array = np.sum([array_1, array_2])

print(updated_array)
Output -

[81] 

As a result, it returns an array adding both arrays.

Summation through Axis

Numpy will sum the numbers of each array in itself if we specify axis parameter.

Example – Summation in the array with 1st axis.

import numpy as np

array_1 = np.array([11, 12, 13])
array_2 = np.array([14, 15, 16])

updated_array = np.sum([array_1, array_2], axis=1)

print(updated_array)
Output -

[36 45] 

As a result, it returns an array adding each element of the array in itself.

Cummulative Sum

Cummulative Sum means adding each element of the array in itself one by one or step by step. In other words, array will add its previous numbers to its next number to get sum of current number.

E.g. Sum of [1, 2, 3] one by one would be [1, 1+2, 1+2+3] = [1, 3, 6].

In Numpy, it performs with cumsum() function.

Example – Finding cummulative summation in the array.

import numpy as np

array = np.array([11, 12, 13])

updated_array = np.cumsum(array)

print(updated_array)
numpy-summations

As shown above, it returns an array obtaining number adding one by one.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

1 thought on “Introduction to NumPy Summations in Python”

Leave a Comment

Stay in the loop

codingstreets