NumPy Arithmetic Operations and Functions

Free NumPy course with real-time projects Start Now!!

Python has a wide range of standard arithmetic operations. These operations help perform normal functions of addition, subtraction, multiplication, and divisions. There are specific functions in NumPy for performing arithmetic operations. Let us learn about these NumPy Arithmetic Operations and Functions.

NumPy Arithmetic Operations and Functions

NumPy Arithmetic Operations

Arithmetic operations are possible only if the array has the same structure and dimensions. We carry out the operations following the rules of array manipulation. We have both functions and operators to perform these functions.

NumPy Add function

This function is used to add two arrays. If we add arrays having dissimilar shapes we get “Value Error”.

import numpy as np
a = np.array([10,20,100,200,500])
b = np.array([3,4,5,6,7])
np.add(a, b)
 

Output

array([ 13, 24, 105, 206, 507])

NumPy Add Operator

We can also use the add operator “+” to perform addition of two arrays.

import numpy as np
a = np.array([10,20,100,200,500])
b = np.array([3,4,5,6,7])
print(a+b)

Output

[ 13 24 105 206 507]

NumPy Subtract function

We use this function to output the difference of two arrays. If we subtract two arrays having dissimilar shapes we get “Value Error”.

import numpy as np
a = np.array([10,20,100,200,500])
b = np.array([3,4,5,6,7])
np.subtract(a, b)

Output

array([ 7, 16, 95, 194, 493])

NumPy Subtract Operator

We can also use the subtract operator “-” to produce the difference of two arrays.

import numpy as np
a = np.array([10,20,100,200,500])
b = np.array([3,4,5,6,7])
print(a-b)
 

Output

[ 7 16 95 194 493]

NumPy Multiply function

We use this function to output the multiplication of two arrays. We cannot work with dissimilar arrays.

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
np.multiply(a, b)
 

Output

array([21, 12, 20, 30, 7])

NumPy Multiply Operator

We can also use the multiplication operator “*” to get the product of two arrays.

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
print(a*b

Output

[21 12 20 30 7]

NumPy Divide Function

We use this function to output the division of two arrays. We cannot divide dissimilar arrays.

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
np.divide(a,b)

Output

array([2.33333333, 0.75 , 0.8 , 0.83333333, 0.14285714])

NumPy Divide Operator

We can also use the divide operator “/” to divide two arrays.

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
print(a/b)

Output

[2.33333333 0.75 0.8 0.83333333 0.14285714]

NumPy Mod and Remainder function

We use both the functions to output the remainder of the division of two arrays.

NumPy Remainder Function

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
np.remainder(a,b)
 

Output

array([1, 3, 4, 5, 1])

NumPy Mod Function

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
np.mod(a,b)
 

Output

array([1, 3, 4, 5, 1])

NumPy Power Function

This Function treats the first array as base and raises it to the power of the elements of the second array.

import numpy as np
a = np.array([7,3,4,5,1])
b = np.array([3,4,5,6,7])
np.power(a,b)

Output

array([ 343, 81, 1024, 15625, 1])

NumPy Reciprocal Function

This Function returns the reciprocal of all the array elements.

import numpy as np
a = np.array([7,3,4,5,1])
np.reciprocal(a)
 

Output

array([0, 0, 0, 0, 1])

Summary

There are additional functions to perform Numpy arithmetic operations on array elements. This is an addition to the inbuilt standard python functions. We need to follow a few rules of array manipulation for performing these operations.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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