The division is one of the rudimentary arithmetic operations which is used to find out which multiple of a given number is another. It ain’t something that can be done with our fingers as we were taught to carry out addition & subtraction. So, it becomes all the more tedious when it comes to analyzing truckloads of data.
This article sets out to explore the different variances to carry out division using the divide( ) function from the numpy library, listed as follows.
- Dividing two scalars
- Dividing two arrays
- Dividing one array & a scalar
- Dividing arrays of different sizes
Also read: NumPy Sum – A Complete Guide
Syntax of numpy.divide( )
Before getting on with solving examples, let us first understand the syntax of this function.
numpy.divide(x1, x2, where=True, dtype=None)
- x1 – is the scalar or one-dimensional array or two-dimensional array which is the dividend
- x2 – is the scalar or one-dimensional array or two-dimensional array which is the divisor
- where – used to specify the location within the array which is only to be subjected to division by typing TRUE leaving the other entities exempted from being divided
- dtype – used to specify the type of data that is to be returned as the result
Use the following code to import the numpy library before getting started with dividing the entities.
import numpy as np
Using numpy.divide( ) for Dividing two scalars
Scalar quantities are those which are only a number, unlike an array which possesses a collection of numbers. Let us assign some scalars to a couple of variables & use the divide( ) function.
a = 10 b = 5 np.divide(a,b)
Using numpy.divide( ) for Dividing two arrays
In this section, we shall get on with dividing a pair of one-dimensional arrays of the same size. You have seen it right! It is a mandate that the arrays subjected to a division be of the same size to use the divide( ) function. The arrays can be fed using the array( ) function as shown below.
ar_1 = np.array([2,1,0]) ar_2 = np.array([4,3,5])
Now, ar_1 will be divided by ar_2 using the following code.
The division can also be carried out using the slash operator (/) using the below code.
Using numpy.divide( ) for Dividing one array & a scalar
Python provides us with the flexibility to divide an array using a scalar quantity. What this does is send the scalar quantity to the denominator, thereby dividing each element of the array and the result will be provided. Let’s get started by creating an array, this time it’s two-dimensional.
ar_3 = np.array([[2,1,0],[3,4,5],[6,7,8]])
After that use the divide( ) function to divide each element in the above array by ‘10’.
Using numpy.divide( ) for Dividing arrays of different sizes
Earlier it was stated in this article that two arrays ought to be of the same size for using divide( ) function. Well, it’s true but to the extent that it only applies to the size of the columns. Python allows one to divide two arrays of different sizes provided that the number of columns in both arrays is equal. So, let’s create two arrays that comply with just that.
ar_2 = np.array([4,3,5]) ar_3 = np.array([[2,1,0],[3,4,5],[6,7,8]])
Now one can use the divide( ) function to do its thing.
Note: One ought to make sure that the divisor does not contain zero as any of its array elements, else the following error shall appear.
Now that we have reached the end of this article, hope it has elaborated on how to divide entities using Python programming. Here’s another article that details how to subtract entities using numpy in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, adios!