Numpy Element Wise Division featured image

Numpy Element Wise Division: How to do it using Numpy Divide

GET FREE AMZAON AUDIOBOOKS

Numpy is a very popular python module for doing array manipulation. In the last post, you have learned how to perform element wise multiplication in NumPy. In this entire tutorial, you will know how to do numpy element wise division. Here I will show you various methods and example to implement it.

Before going to coding demonstration and examples let’s import all the necessary libraries required. In this entire tutorial, I am using only the NumPy library lets import them.

import numpy as np

Element Wise Division of 1D Numpy numpy

Let’s create a 1D numpy array using the method numpy.array() for performing division on all the elements of it. I will use two approaches. One is the divisive (/) operator and the other is the np.divide() method. Let’s see it.

array_1d = np.array([10,20,30,40,50])
# using divisive operator
array_1d/10

Output

"<yoastmark

# using np.divide()
np.divide(array_1d,10)

Output

Division using numpy.divide() method
Division using numpy.divide() method

You can see, In both methods, the output will be the same.

Element Wise Division of 2D Numpy Array

Now let’s perform the division on the two-dimensional NumPy arrays. Here I am using the same methods that I have done in the 1D array.  But before it let’s create a 2d NumPy array.

array_2d = np.array([[10,20,30],[40,50,60]])
# using divisive operator
array_1d/10

Output

2D Division using divisive operator
2D Division using divisive operator
# using np.divide()
np.divide(array_1d,10)

Output

2D Numpy Division using numpy.divide() method
2D Numpy Division using numpy.divide() method

Again you are getting the same results in both cases. In the next section, you will look at other examples of the Numpy Element Wise Division.

Other Examples on Numpy Division

Element wise division using mean

In this example, I will divide each element of the NumPy array using the mean of the whole array. For the sake of simplicity, I am performing calculations on a 1D array only.

Execute the below lines of code.

import numpy as np
array_1d = np.array([10,20,30,40,50])
mean = np.mean(array_1d)
np.divide(array_1d,mean)

Output

Dividing each elements of array by mean
Dividing each elements of array by mean

Numpy element wise division using max and min

Now, let’s divide each array element with the max of the entire array. To do so you have to pass two arguments in the numpy.divide(). One is the input array and the other is the result of np.max(). Just execute the code give below to see the output.

import numpy as np
array_1d = np.array([10,20,30,40,50])
max = np.max(array_1d)
np.divide(array_1d,max)

Output

Dividing each elements of array by max
Dividing each element of the array by max

In the same way, you can do element-wise division with the min value.

import numpy as np
array_1d = np.array([10,20,30,40,50])
min = np.min(array_1d)
np.divide(array_1d,min)

Output

Dividing each elements of array by min
Dividing each element of the array by min

Conclusion

That’s all for now. These are the methods to do the Element Wise Division. The method numpy.divide() is the faster method than the simpler ones. Therefore I prefer to use it. Hope you have liked this tutorial. If you have any queries then you can contact us.

Source:

Numpy Divide

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner