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
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
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
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
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
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
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
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
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
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
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
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
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
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